Run a PHP function as an html attribute?

Asked

Viewed 106 times

0

What I need is this, I have this html code:

<div php-if="{{valor}}>0">CONTEUDO QUE SERÁ MOSTRADO DINAMICAMENTE</div>

The variable "{{value}}" I can already return dynamically. What I would like to do now is to run a php function that identifies that I am making an IF comparison and if it returns me false, this entire tag would be removed from the string. The result would be an html string, but without this example tag.

NOTE: It’s a bit like what Vue.js does, but I want to do it on the server side via php.

  • You want to compare a value that comes from Vue by php?

  • Hi good night and thanks for the return. I am not using Vue.js, I put as an example just for better understanding.

1 answer

-1

Solução do problema.

After many attempts, follow the code that solved my problem. With it I can hide or display an html element using a php "IF" as html attr. Remembering that this variable "{{value}}" comes from my template via controller, so its value is dynamic.

  • $dom->loadHTML(mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8')); I modified this line to fix problems with result encoding

  • Walison, do not post code in image form. The site has support for source code, just copy it in the editor, selected and press the link Ctrl+K, or use the button {} of the same.

  • Your variable isValid It’s no use, you’re wearing it on if but you’ll never get into that if because isValid will always be true in the code that is in the image

  • On the isValid variable, I disagree. As said isValid will be the result of {{value}}>0 and this variable {{value}} is fed dynamically, so the if block if {{value}} = 0, makes my test false and if 1, for example, makes the code true. I tested and solved my problem.

  • Miguel, see also that isValid is inside an Eval.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.