3

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

1 Answer 1

8

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 %}
1
  • 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
    Commented May 5, 2015 at 16:06

Your Answer

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

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