1
votes

I would like to define a twig object that contains two properties :

  • The first one is a short text
  • The second one is a large text (so, i need to use {% set %}{% endset %} to keep readability of the template)

I first tried like this :

{% block principal %}
        {% set a_form_help = {'help_toggle_label': 'See the big form field tip', 'help_content': ''} %}
        {% set a_form_help.help_content %}>
        <h1>This is rules for the field</h1>
        <p>A looonng text that will be contained in a collapsed block</p>
        <p>And several lines of tips for this field</p>
        {% endset %}
{% endblock %}

But Twig complains about the use of the dot in the second {% set %} tag (for setting the large text content in the help_content property).

I've found this message (Twig - Why does it not allow us to set object / array values?) and then done this workaround by using a temporary variable:

{% block principal %}
        {% set tmp_help_content %}>
        <h1>This is rules for the field</h1>
        <p>A looonng text that will be contained in a collapsed block</p>
        {% endset %}
        {% set a_form_help = {'help_toggle_label': 'See the big form field tip', 'help_content': tmp_help_content} %}

        {{ dump(a_form_help) }}
{% endblock %}

I've used a a temporary variable since using the merge() filter would give the same result.

Is this workaroud a good practice or is there a better/cleaner way ?

Thanks in advance for any tip|advice ! :-)

1
Your topic title is/was so misleading.. - DarkBee

1 Answers

0
votes

There is no clean way to do it in twig.

Is this workaroud a good practice or is there a better/cleaner way ?

It is not a good practice. As twig is a templating language its responsability is only to display data, not to structure it. Your View Object should have been created elsewhere