coding_standards.rst 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. Coding Standards
  2. ================
  3. When writing Twig templates, we recommend you to follow these official coding
  4. standards:
  5. * Put one (and only one) space after the start of a delimiter (``{{``, ``{%``,
  6. and ``{#``) and before the end of a delimiter (``}}``, ``%}``, and ``#}``):
  7. .. code-block:: jinja
  8. {{ foo }}
  9. {# comment #}
  10. {% if foo %}{% endif %}
  11. When using the whitespace control character, do not put any spaces between
  12. it and the delimiter:
  13. .. code-block:: jinja
  14. {{- foo -}}
  15. {#- comment -#}
  16. {%- if foo -%}{%- endif -%}
  17. * Put one (and only one) space before and after the following operators:
  18. comparison operators (``==``, ``!=``, ``<``, ``>``, ``>=``, ``<=``), math
  19. operators (``+``, ``-``, ``/``, ``*``, ``%``, ``//``, ``**``), logic
  20. operators (``not``, ``and``, ``or``), ``~``, ``is``, ``in``, and the ternary
  21. operator (``?:``):
  22. .. code-block:: jinja
  23. {{ 1 + 2 }}
  24. {{ foo ~ bar }}
  25. {{ true ? true : false }}
  26. * Put one (and only one) space after the ``:`` sign in hashes and ``,`` in
  27. arrays and hashes:
  28. .. code-block:: jinja
  29. {{ [1, 2, 3] }}
  30. {{ {'foo': 'bar'} }}
  31. * Do not put any spaces after an opening parenthesis and before a closing
  32. parenthesis in expressions:
  33. .. code-block:: jinja
  34. {{ 1 + (2 * 3) }}
  35. * Do not put any spaces before and after string delimiters:
  36. .. code-block:: jinja
  37. {{ 'foo' }}
  38. {{ "foo" }}
  39. * Do not put any spaces before and after the following operators: ``|``,
  40. ``.``, ``..``, ``[]``:
  41. .. code-block:: jinja
  42. {{ foo|upper|lower }}
  43. {{ user.name }}
  44. {{ user[name] }}
  45. {% for i in 1..12 %}{% endfor %}
  46. * Do not put any spaces before and after the parenthesis used for filter and
  47. function calls:
  48. .. code-block:: jinja
  49. {{ foo|default('foo') }}
  50. {{ range(1..10) }}
  51. * Do not put any spaces before and after the opening and the closing of arrays
  52. and hashes:
  53. .. code-block:: jinja
  54. {{ [1, 2, 3] }}
  55. {{ {'foo': 'bar'} }}
  56. * Use lower cased and underscored variable names:
  57. .. code-block:: jinja
  58. {% set foo = 'foo' %}
  59. {% set foo_bar = 'foo' %}
  60. * Indent your code inside tags (use the same indentation as the one used for
  61. the main language of the file):
  62. .. code-block:: jinja
  63. {% block foo %}
  64. {% if true %}
  65. true
  66. {% endif %}
  67. {% endblock %}