Ignoring Unclosed HTML in PHP Conditionals

Is there a way to set PHP Storm to ignore the fact there is improperly closed HTML elements?

I have the following code for a WordPress theme:

 

<?php if ( get_header_image() ) { ?>
    <header id="masthead" class="site-header" style="background-image: url(<?php header_image(); ?>)" role="banner">
<?php } else { ?>
    <header id="masthead" class="site-header" role="banner">
<?php } ?>
...
</header>

Because of the two <header> elements and only one closing tag, PHPStorm kind of freaks out because it is seeing invalid HTML. Is there some setting that will get it to treat these two <header> elements as one? I can't really use the program when it does this because it starts messing up my code and whatnot.

0
2 comments

Hi there,

AFAIK -- nope.

The best solution here (from all angles) -- have only one tag and adjust its' parts inside PHP code, something like this:

<header id="masthead" class="site-header" <?php if ( get_header_image() ) { ?>style="background-image: url(<?php header_image(); ?>)" <?php } ?>role="banner">
...
</header>

At least this is kind of approach I'm using in my projects (be it Twig/Smarty as template engine or just plain PHP (in WordPress theme)).

The reason is rather simple: HTML parser is not really aware of PHP syntax (if/else branches) -- e.g. this comment.

Similar kind of stuff for Blade files: https://youtrack.jetbrains.com/issue/WI-26903

1
Avatar
Permanently deleted user

Thanks.

0

Please sign in to leave a comment.