PHPStorm: JS Language Injection with custom tags inside the template

If I needed html templates with embedded javascript in a PHP project, I was not satisfied with the solution 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 to configure PHPStorm to process any code inside <@@> and <@ = @> as Javascript (syntax highlighting, code completion, etc.) when processing external content as HTML?

Note:

The code inside should be considered as HTML, and the <@@> parts should be processed only as javascript if inside this tag type.

+4
source share
1 answer

Alt+F7 Language Injections , .

.

Edit:

RegExr (/<@((?:\s|.)*?)@>/gm). , PHPStorm...

+3

Source: https://habr.com/ru/post/1606984/


All Articles