Best practice - combine html css and php

Hello,
maybe this was discussed some times, but I am still not sure what would be best practice using phpStorm.
Let´s assume the following two line sof code:

1)

if ($a === true) {

$image = 'image1.png';
} else {
$image = 'image2.png';
}
echo "<input type='image' src='$image' class='button_class' alt='blabla'>";



So further let´s assume that the $image-variable must be set dynamically in the code; so there is no possibility to "hardcode" it, because, different from this example I used here there are let´s say about 100 different images to load dependent on a special case. I only used the two images here for simplicity.
Using this style of coding I get the following:
PRO´s:     + phpStorm finds the code between the < > as html and src, class and so on correctly as properties
                + phpStorm will color all the things between < > correctly.
                + and the best: phpStorm will find 'button_class' as an existing class name, and if you put the cursor on button_class,
                    you can go to the defintion by pressing CMD+B (or something like that)

BUT! CON´s: + there will still an error message in phpStorm becuase the src $image cannot be resolved (Cannot resolve file path).

Ok, so if I do not want the error message, I could do it like this:

2)

if ($a === true) {

$image = 'image1.png';
} else {
$image = 'image2.png';
}
echo "<input type='image' src='".$image."' class='button_class' alt='blabla'>";

Ok, so the error message disapears, but all the benefits I had before too: The string between < > is not just text, so phpStorm does not any analyzation of css-class names etc.

Hmm, so becuase I do create my website dynamically, I am very often in this situation. I would really have my error messages down so I do not have to inspect it always and always again. A possibility for me would be switch of error messaging for this one line of code, but that is not possible.

So, what would you recommend as "best practice" - NOT in general, but in this special case using phpStorm.
Sometimes I think I am the only one with this problem, because that is discussed really rarly and I do not have a solution for this problem. Or am I doing something wrong with coding. The thing is, I often have this "dynamic" settings.

It would be good to here your opinions. Thanks a lot.

4 comments
Comment actions Permalink

Related Ticket:


Other tickets for Language Injections:


As to "best practices" -- I'm building HTML first which has no dynamically generated  (by PHP) data, so it's fully validated; and then convert it into Smarty template(s) and have no such issues there (as I understand most of the HTML/CSS/JS/etc inspections are turned off or much relaxed there -- hard to say with my workflow). I use "echo HTML" approach very rarely so cannot advice much here.

0
Comment actions Permalink

Hm, I think smarty is no option for me, bacuase I have a project with about 100.000 lines of code in Object oriented style and becuase I learned php on my own it is more C-style than php should be. What I want to say is that I totally had to write the code new, and that is right now no option.
So I hoped that I simply could switch off that errors...

0
Comment actions Permalink

Well ... Settings | Inspections -- disable HTML/CSS/XML etc inspections (you can have multiple profiles and switch between them when needed -- for example when using "Code | Inspect Code"). Obviously, it's not ideal, but at least something to check out.

0
Comment actions Permalink

The thing is: I really want all that inspections. So it wouldn´t be so good to switch it off.
But there are a lot of @noinspection attributes. Why would it be so difficult do create a @noinspections start and a @noinspections stop command to switch on / off inspection in several parts of a document?

0

Please sign in to leave a comment.