Search code examples
javascriptphpphpstorm

PhpStorm highlight part of file as JavaScript with custom start and end lines


I have inherited a PHP project that contains several HTML files with the following structure

<?php $this->placeholder('dom.ready')->captureStart(); ?>

    var some = javascript.goesHere();

<?php $this->placeholder('dom.ready')->captureEnd(); ?>

<div class="some html goes here">

   <h1> <?php echo "with some php in the middle"; ?> </h1>

</div>

Would it be possible to configure Phpstorm to interpret everything between the placeholder lines as JS and the rest of the file as HTML with some PHP?

I know the file structure is very unfortunate, but I can not refactor it for now...

Thanks


Solution

  • Probably not really what you want but if you surround the javascript with a script tab phpstorm will interpret it as js. You would need to change your code to look like:

    <script>
    <?php $this->placeholder('dom.ready')->captureStart(); ?>
    
        var some = javascript.goesHere();
    
    <?php $this->placeholder('dom.ready')->captureEnd(); ?>
    </script>
    <div class="some html goes here">
    
       <h1> <?php echo "with some php in the middle"; ?> </h1>
    
    </div>
    

    This article says you can use two file extentions to accomplish something similar without using the script tags.

    https://confluence.jetbrains.com/display/PhpStorm/Syntax+highlighting+of+PHP+inside+JavaScript+(and+other+languages)