Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a capture template tag #170

Closed
ellmetha opened this issue Feb 23, 2024 · 0 comments
Closed

Add a capture template tag #170

ellmetha opened this issue Feb 23, 2024 · 0 comments
Assignees
Milestone

Comments

@ellmetha
Copy link
Member

ellmetha commented Feb 23, 2024

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:

{% 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 %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

1 participant