Is it possible to define inline templates in Twig?
php, twig
Solution
The quick solution
Based on your comments about what you really want, I believe the "block version" of the set tag is what you need, i.e.:
{% set variableName %}
<p>
Content block with <i>line-breaks</i>
and {{ whatever }} else you need.
</p>
{% endset %}
Then whatever chunks of markup that you need can be passed to a macro (as others have suggested):
{% macro input_wrapper(label, name, controls) %}
<div class="control-group">
<label class="control-label" for="{{ name }}">{{ label }}</label>
<div class="controls">{{ controls }}</div>
</div>
{% endmacro %}
{% set label, name = "Age", "age" %}
{% set controls %}
<select name="age">
<option>...</option>
</select>
{% endset %}
{% import _self as inline %}
{{ inline.input_wrapper(label, name, controls) }}
The real solution
As for the original question, I've done some research and found out that you can define inline templates with a combination of the set and verbatim tags plus the template_from_string function.
The content of the template, however, depends on how you want to use it:
- If you're happy with setting variables using the block syntax, use the include tag or function.
- If you need to use blocks, as in your original example, you will have to use the embed tag.
Example using variables and include tag
{# Defining the template #}
{% set input_wrapper_string %}
{% verbatim %}
<div class="control-group">
<label class="control-label" for="{{ name }}">{{ label }}</label>
<div class="controls">{{ controls }}</div>
</div>
{% endverbatim %}
{% endset %}
{% set input_wrapper_tpl = template_from_string(input_wrapper_string) %}
{# Setting the variables #}
{% set label, name = "Age3", "age" %}
{% set controls %}
<select name="age">
<option>...</option>
</select>
{% endset %}
{# "Rendering" the template #}
{% include input_wrapper_tpl %}
Example using blocks and embed tag
{# Defining the template #}
{% set input_wrapper_string %}
{% verbatim %}
<div class="control-group">
<label class="control-label" for="{% block name %}{% endblock %}">{% block label %}{% endblock %}</label>
<div class="controls">{% block controls %}{% endblock %}</div>
</div>
{% 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 %}
<select name="age">
<option>...</option>
</select>
{% endblock %}
{% endembed %}
Problem
I would like to do something similar to this in Twig: ``` {% inlinetemplate input_wrapper %} <div class="control-group"> <label class="control-label" for="{% block name %}{% endblock name %}"> {% block label %}{% endblock label %} </label> <div class="controls"> {% block controls %}{% endblock controls %} </div> </div> {% endinlinetemplate %} {% extendinline input_wrapper %} {% block label %}Age {% endblock label %} {% block name %}age{% endblock name %} {% block controls %} <select name="age"> <option ...>...</option> ... </select> {% endblock controls %} {% endextendline input_wrapper %} ``` Is this possible?