ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
# Coding Standards When writing Twig templates, we recommend you to follow these official codingstandards: - Put one (and only one) space after the start of a delimiter (`{{`, `{%`,and `{#`) and before the end of a delimiter (`}}`, `%}`, and `#}`): <table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1 2 3</pre></div></td><td class="code"><div class="highlight"><pre>{{ foo }} {# comment #} {% if foo %}{% endif %} </pre></div></td></tr></table> When using the whitespace control character, do not put any spaces betweenit and the delimiter: <table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1 2 3</pre></div></td><td class="code"><div class="highlight"><pre>{{- foo -}} {#- comment -#} {%- if foo -%}{%- endif -%} </pre></div></td></tr></table> - Put one (and only one) space before and after the following operators:comparison operators (`==`, `!=`, `<`, `>`, `>=`, `<=`), mathoperators (`+`, `-`, `/`, `*`, `%`, `//`, `**`), logicoperators (`not`, `and`, `or`), `~`, `is`, `in`, and the ternaryoperator (`?:`): ~~~ {{ 1 + 2 }} {{ foo ~ bar }} {{ true ? true : false }} ~~~ - Put one (and only one) space after the `:` sign in hashes and `,` inarrays and hashes: <table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1 2</pre></div></td><td class="code"><div class="highlight"><pre>{{ [1, 2, 3] }} {{ {'foo': 'bar'} }} </pre></div></td></tr></table> - Do not put any spaces after an opening parenthesis and before a closingparenthesis in expressions: <table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1</pre></div></td><td class="code"><div class="highlight"><pre>{{ 1 + (2 * 3) }} </pre></div></td></tr></table> - Do not put any spaces before and after string delimiters: <table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1 2</pre></div></td><td class="code"><div class="highlight"><pre>{{ 'foo' }} {{ "foo" }} </pre></div></td></tr></table> - Do not put any spaces before and after the following operators: `|`,`.`, `..`, `[]`: <table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1 2 3 4</pre></div></td><td class="code"><div class="highlight"><pre>{{ foo|upper|lower }} {{ user.name }} {{ user[name] }} {% for i in 1..12 %}{% endfor %} </pre></div></td></tr></table> - Do not put any spaces before and after the parenthesis used for filter andfunction calls: <table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1 2</pre></div></td><td class="code"><div class="highlight"><pre>{{ foo|default('foo') }} {{ range(1..10) }} </pre></div></td></tr></table> - Do not put any spaces before and after the opening and the closing of arraysand hashes: <table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1 2</pre></div></td><td class="code"><div class="highlight"><pre>{{ [1, 2, 3] }} {{ {'foo': 'bar'} }} </pre></div></td></tr></table> - Use lower cased and underscored variable names: <table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1 2</pre></div></td><td class="code"><div class="highlight"><pre>{% set foo = 'foo' %} {% set foo_bar = 'foo' %} </pre></div></td></tr></table> - Indent your code inside tags (use the same indentation as the one used forthe target language of the rendered template): <table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1 2 3 4 5</pre></div></td><td class="code"><div class="highlight"><pre>{% block foo %} {% if true %} true {% endif %} {% endblock %} </pre></div></td></tr></table>