I've been trying to figure out how I'm gonna do recursion with Liquid. I have an app where I want to give users full control over the rendering of the menu. However, the menu is defined by a tree, thus the need for recursive functions in Liquid.
How would I go about this? After doing some reading, I was thinking I'd tackle it this way:
include
I was thinking of using include
this way:
<ul id='site_nav' class='nav'>
{{ include 'menu_item' with menu_items }}
</ul>
And the menu_item partial is this:
<li id='{{menu_item.dom_id}}' class='{{menu_item.css_menu_class}}'>
{{ menu_item.name }}
<ul>
{{ include 'menu_item' with menu_item.children }}
</ul>
</li>
However, since it's user editable, I'll need to hack Liquid to make it load partials from a database. Since that will take a lot more time to study, I wanted to ask first if anyone has tackled this problem before.
Thanks in advance!
You don't actually need to 'hack' Liquid to load partials from the DB, simply extend the provided file system classes, read the following for more info:
https://github.com/Shopify/liquid/blob/master/lib/liquid/file_system.rb
I've implemented Liquid this way before myself, and I can vouch for it not being particularly difficult if you know you're way around Ruby.
As for the recursion, Liquid probably won't limit it (your template examples should just work), but I'd wrap your rendering process with a Timeout::timeout (see link below) to ensure it doesn't go on forever.
http://www.ruby-doc.org/stdlib-1.9.3/libdoc/timeout/rdoc/Timeout.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With