Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h2>The quick solution</h2> <p>Based on your comments about what you really want, I believe the "block version" of the <a href="http://twig.sensiolabs.org/doc/tags/set.html" rel="noreferrer">set tag</a> is what you need, i.e.:</p> <pre><code>{% set variableName %} &lt;p&gt; Content block with &lt;i&gt;line-breaks&lt;/i&gt; and {{ whatever }} else you need. &lt;/p&gt; {% endset %} </code></pre> <p>&nbsp;</p> <p>Then whatever chunks of markup that you need can be passed to a macro (as others have suggested):</p> <pre><code>{% macro input_wrapper(label, name, controls) %} &lt;div class="control-group"&gt; &lt;label class="control-label" for="{{ name }}"&gt;{{ label }}&lt;/label&gt; &lt;div class="controls"&gt;{{ controls }}&lt;/div&gt; &lt;/div&gt; {% endmacro %} {% set label, name = "Age", "age" %} {% set controls %} &lt;select name="age"&gt; &lt;option&gt;...&lt;/option&gt; &lt;/select&gt; {% endset %} {% import _self as inline %} {{ inline.input_wrapper(label, name, controls) }} </code></pre> <p>&nbsp;</p> <h2>The real solution</h2> <p>As for the original question, I've done some research and found out that you can <strong>define</strong> inline templates with a combination of the <a href="http://twig.sensiolabs.org/doc/tags/set.html" rel="noreferrer">set</a> and <a href="http://twig.sensiolabs.org/doc/tags/verbatim.html" rel="noreferrer">verbatim</a> tags plus the <a href="http://twig.sensiolabs.org/doc/functions/template_from_string.html" rel="noreferrer">template_from_string</a> function.</p> <p>The <em>content</em> of the template, however, depends on how you want to <strong>use</strong> it:</p> <ol> <li>If you're happy with setting <em>variables</em> using the block syntax, use the include <a href="http://twig.sensiolabs.org/doc/tags/include.html" rel="noreferrer">tag</a> or <a href="http://twig.sensiolabs.org/doc/functions/include.html" rel="noreferrer">function</a>.</li> <li>If you need to use <em>blocks</em>, as in your original example, you will have to use the <a href="http://twig.sensiolabs.org/doc/tags/embed.html" rel="noreferrer">embed</a> tag.</li> </ol> <p>&nbsp;</p> <h3>Example using variables and include tag</h3> <pre><code>{# Defining the template #} {% set input_wrapper_string %} {% verbatim %} &lt;div class="control-group"&gt; &lt;label class="control-label" for="{{ name }}"&gt;{{ label }}&lt;/label&gt; &lt;div class="controls"&gt;{{ controls }}&lt;/div&gt; &lt;/div&gt; {% endverbatim %} {% endset %} {% set input_wrapper_tpl = template_from_string(input_wrapper_string) %} {# Setting the variables #} {% set label, name = "Age3", "age" %} {% set controls %} &lt;select name="age"&gt; &lt;option&gt;...&lt;/option&gt; &lt;/select&gt; {% endset %} {# "Rendering" the template #} {% include input_wrapper_tpl %} </code></pre> <p>&nbsp;</p> <h3>Example using blocks and embed tag</h3> <pre><code>{# Defining the template #} {% set input_wrapper_string %} {% verbatim %} &lt;div class="control-group"&gt; &lt;label class="control-label" for="{% block name %}{% endblock %}"&gt;{% block label %}{% endblock %}&lt;/label&gt; &lt;div class="controls"&gt;{% block controls %}{% endblock %}&lt;/div&gt; &lt;/div&gt; {% endverbatim %} {% endset %} {% set input_wrapper_tpl = template_from_string(input_wrapper_string) %} {# "Rendering" the template and overriding the blocks #} {% embed input_wrapper_tpl %} {% block label %}Age{% endblock %} {% block name %}age{% endblock %} {% block controls %} &lt;select name="age"&gt; &lt;option&gt;...&lt;/option&gt; &lt;/select&gt; {% endblock %} {% endembed %} </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload