4

In needing html templates with embedded javascript on a PHP project I was not satisfied with any solutions I found, so, with Lodash and some regex I created the following solution :

Template:

<script id="profile_greeting" type="text/template">
    <h1>Hello</h1><br>
    My name is <b><@=this.name@></b><br>
    <@ var today = "" + new Date(); @>
    The date is : <@= today @>
</script>

Lodash settings :

_.templateSettings = {
    interpolate: /\<\@\=([\s\S]+?)\@\>/g,
    escape: /\<\@\-([\s\S]+?)\@\>/g,
    evaluate: /\<\@([\s\S]+?)\@\>/g
};

Question:

How do I setup PHPStorm to treat any code inside <@ @> and <@= @> as Javascript (syntax highlight, code completion, etc) while treating the outside as HTML ?

Note:

Code inside should be treated as HTML, and <@ @> pieces should only be treated as javascript when inside this type of tag.

1 Answer 1

3
+25

Press Alt+F7 and go to Language Injections and create a new injection that is customized to your wishes.

Maybe this can help.

Edit:

I made a RegExr with the regexp to match your wishes (/<@((?:\s|.)*?)@>/gm). Only trouble is how it should be fed to PHPStorm...

Sign up to request clarification or add additional context in comments.

15 Comments

This is what I tried for several hours before I asked the question. However, I cannot get it to work. At the time, this is what I came up with (obviously does not work) : puu.sh/kuLMa/125327746e.png
@JoaoCarlos okay, i'm going to try and fix it. So, if you enter <@ it must inject JS until @> except the first =?
i must go now, i'll try to help tomorrow
anything inside <@ and @> is JS, anything inside <@= @> is also JS but as straight evaluation like a PHP <?php echo 'bla'; ?> can be shortened with <?='bla'?>
Due to the awesomeness of stackoverflow your answer was awarded the bounty, not because it was correct but because it was the only one haha. Free rep HoopHoop!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.