2

How do comments work in Craft? I mean inline and multiline comments, like:

HTML: <!--xxx--> m
  JS:   /*xxx*/  m   //xxx i
 PHP:   /*xxx*/  m   //xxx i   #xxx i
7

There's only one comment syntax for Craft / Twig:

{# ... #}

Anything in between won't be parsed by Twig, so you can comment out multiple lines of Twig and HTML like so:

{# 
    {% set var = value %}
    {% for item in seq %}
        <p>{{ item }}</p>
    {% endfor %}
#}

As with many other languages you can't nest comments! So if you want to comment out some lines of code that already have comments, you can't do that like so:

{# 
    {#
     # Lots of comments going on here
     #}
    {% set var = value %}

    {# I warned you #}
    {% for item in seq %}
        <p>{{ item }}</p>
    {% endfor %}
#}

The best workaround I found is to use HTML comments in combination with Twigs verbatim tags (disables Twig parsing). I added a snippet to my editor for this and use it regularly since:

{% verbatim %}
<!--
    {#
     # Lots of comments going on here
     #}
    {% set var = value %}

    {# I warned you #}
    {% for item in seq %}
    {% endfor %}
-->
{% endverbatim %}
| improve this answer | |
  • 3
    You should better comment out with Twig. HTML comments don't stop the Twig code from being executed, it just makes the HTML (that your Twig generates) not being rendered by the browser! – carlcs May 5 '15 at 16:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.