Voog.com

Using variables in custom tags

Some custom tags support variables as parameter values, enabling some cool tricks, especially when combining it with Data objects.

Content and contentblock

Using a simple for loop, layouts can render content areas with dynamic names. Essentially, you can do stuff like this:
{% assign count = page.data.count %}
{% for i in (1..count) %}
...
{% endfor %}
Expanding on this, you can use the Settings Editor to create a simple interface to choose how many columns are rendered. The number of columns is saved to page.data and used to render the content areas in the layout.

Here's the full source code for the above solution: advanced example.


blogcontext

In cases where there are multiple blog pages on one site, it might be useful to show a specific blog's articles on the front page, for example. Now, if the site is multilingual, you would need to know the page.path in each language. Instead of this, you can use page.data to set a boolean for those specific blog pages and then find the blog that has that data attribute set to true, making it unnecessary to hardcode any paths to the template code.

{% assign featured_blog_path = '' %}

{% for blog in site.blogs %}
{% if blog.data.featured %}
{% assign featured_blog_path = blog.path %}
{% break %}
{% endif %}
{% endfor %}

{% blogcontext featured_blog_path %}
{% for featured_article in articles %}
...
{% endfor %}
{% endblogcontext %}

elementscontext

Together with the filtering syntax, variables can be used to generate dynamic element lists. For example, let's say you have a complex page structure with nested category pages that contain elements:

  • Front page (front page)
    • Products (common page)
      • Category 1 (common page)
        • Subcategory 1 (elements page)
          • Product 1 (element)
          • Product 2 (element)
        • Subcategory 2 (elements page)
          • Product 3 (element)
      • Category 2
        • Subcategory 3 (elements page)
          • Product 4 (element)
          • Product 5 (element)
        • Subcategory 4 (elements page)
          • Product 6 (element)
          • Product 7 (element)
    • About us
    • Contact
Now, each "subcategory" page has multiple elements, but you would also like to show them on the parent pages, so that elements from "subcategory 1" and "subcategory 2" are shown on the "category 1" page and all elements from all "category" pages are listed on the "products" page. Doing this with variables is easy:

{% elementscontext edicy_model="Products" q.page.path.$starts=page.path %}