nystudio107 / craft-templatecomments

Adds a HTML comment with performance timings to demarcate `{% block %}`s and each Twig template that is included or extended.
https://nystudio107.com/
MIT License
20 stars 1 forks source link

Twig {% comments %} tags making their way into the browser #5

Closed jalendport closed 5 years ago

jalendport commented 5 years ago

I just installed templatecomments from the plugin store so pretty sure I have the latest version...

In my templates I have the following code to grab some JS files to be inlined:

<script>
    {{ source("_inlinejs/loadjs.min.js") }}
    {{ source("_inlinejs/tiny-cookie.min.js") }}
    {{ source("_inlinejs/tab-handler.min.js") }}
</script>

With templatecomments installed, I am now getting errors in my browser console like Unexpected token %. Digging into the source code there, I see the above code block now looks like this:

<script>
    {% comments '_inlinejs/loadjs.min.js' %}
        {# JS code here #}
    {% endcomments %}

    {% comments '_inlinejs/tiny-cookie.min.js' %}
        {# JS code here #}
    {% endcomments %}

    {% comments '_inlinejs/tab-handler.min.js' %}
        {# JS code here #}
    {% endcomments %}
</script>   

Everywhere else I'm getting the usual <!-- >>> TEMPLATE BEGIN >>> _layouts/site --> but seems like it's having troubles converting {% comments %}{% endcomments %} when it's inside a <script></script> tag maybe?

khalwat commented 5 years ago

So what I think is going on here is that I wrap templates that are loaded in:

    {% comments %}
    {% endcomments %}

But templates that are loaded via source() are not parsed at all... so it's returning the raw tags, unparsed.

It's an interesting problem; I'm not sure how to tell if we're being loaded in an unparsed context via source().

khalwat commented 5 years ago

Well, that was fun. Fixed in https://github.com/nystudio107/craft-templatecomments/commit/f9435aeefc95c388f4ab38e359f8c019724d01e8

Released as 1.0.6

jalendport commented 5 years ago

Thanks again @khalwat! 🙌