4

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?

Marc
  • 6,749
  • 9
  • 47
  • 78

1 Answers1

4

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.

Community
  • 1
  • 1
Tim Jones
  • 1,766
  • 12
  • 17
  • 2
    Oh an answer from the author himself, thanks! May I say that I really like DotLiquid? :) I will implement the FileSystem, wasn't aware of that, and see if I can make it. – Marc Oct 05 '12 at 00:43
  • Actually after implementing the LocalFileSystem to load the template in DotLiquid is `{% include MyTemplate %}` instead of `{{ include 'menu_item' with menu_items }}` as listed in the related question. I know, its for the Ruby version, I just wanted to mention it here for other people ;) – Marc Jan 30 '13 at 00:58
  • @Tim Jones Is it possible to pass an object to included template from disk file? – Pit Digger Aug 21 '13 at 14:20
  • @PitDigger Yes, it is - actually Marc's comment is correct. You can do something like `{{ include 'product' with products[0] }}, and that will pass the `products[0]` object to the `product` template. The variable name you use to access that object inside the included template is the name of the template itself - i.e. `product` in this example. – Tim Jones Aug 23 '13 at 09:20