You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Presently, the only way to assign new variables inside a template is to make use of the assign template tag, which works as follows:
{% assign my_var = "Hello World!" %}
This mechanism works fine from defining new variables from existing variables (to which some filters are applied) or from simple literal values. That being said, it is sometimes necessary to assign larger contents (that span over many lines) or contents that require more complex rendering logics to variables.
Proposition
In order to make this possible, let's introduce a new capture template tag that will allow assigning the rendered value of a block content to a variable. For example:
{% capture title %}
{% if show_large_title %}
A large title
{% else %}
A small title
{% endif %}
{% endcapture %}
Depending on the value of the show_large_title, the content of the title variable will be either A large title or A small title.
It is worth mentioning that the capture template tag should also support the unless defined modifier in order to make it possible to capture the content of the block in the specified variable unless it is already defined in the context:
{% capture title unless defined %}
{% if show_large_title %}
A large title
{% else %}
A small title
{% endif %}
{% endcapture %}
The text was updated successfully, but these errors were encountered:
Description
Context
Presently, the only way to assign new variables inside a template is to make use of the
assign
template tag, which works as follows:This mechanism works fine from defining new variables from existing variables (to which some filters are applied) or from simple literal values. That being said, it is sometimes necessary to assign larger contents (that span over many lines) or contents that require more complex rendering logics to variables.
Proposition
In order to make this possible, let's introduce a new
capture
template tag that will allow assigning the rendered value of a block content to a variable. For example:Depending on the value of the
show_large_title
, the content of thetitle
variable will be eitherA large title
orA small title
.It is worth mentioning that the
capture
template tag should also support theunless defined
modifier in order to make it possible to capture the content of the block in the specified variable unless it is already defined in the context:The text was updated successfully, but these errors were encountered: