8

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.

  • If you have tackled this problem before, how did you render something recursively and allow it to be user editable?
  • If you have not tackled this before, what way would you recommend I take? The way I detailed above?

Thanks in advance!

Ramon Tayag
  • 15,224
  • 9
  • 43
  • 69

1 Answers1

2

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

Ryan Townsend
  • 1,015
  • 1
  • 9
  • 15
  • 1
    I'm just getting familiar with Jekyll so pardon me if this is a stupid questions.....should I just be able to copy/past that file_system.rb file into the plugin folder? – Jack Hanna Mar 04 '20 at 18:13