Search code examples
c#templatesliquiddotliquid

Write a recursive loop in DotLiquid


I have this loop in DotLiquid:

{% for page in Page -%}
    {{ page.Title }}
    <ul>
       {% for subpage in page.Pages -%}
           <li>{{ subpage.Title }}</li>
           <!-- subpage.Pages has more pages and so on... -->
       {% endfor -%}
    </ul>
{% endfor -%}

Every subpage object has a Pages property with other subpages in a list (like the first Page object.

How to I write a recursive iteration over these subpages to create the complete tree?


Solution

  • Move your code into a separate file, and use the include tag.

    This related question includes some example template code - it's for the Ruby version of Liquid, but it should be directly portable.

    Depending on what you need to do, you can either set Template.FileSystem to the built-in LocalFileSystem to resolve includes, or create your own. See the source code for LocalFileSystem for an example implementation of IFileSystem.