液体标记/液体模板中的递归 [英] Recursion in Liquid Markup / Liquid Templates

查看:76
本文介绍了液体标记/液体模板中的递归的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试找出如何使用液体进行递归.我有一个应用程序,我想让用户完全控制菜单的呈现.但是,菜单是由树定义的,因此需要在Liquid中使用递归函数.

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>

menu_item部分是这样的:

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>

但是,由于它是用户可编辑的,因此我需要破解Liquid以使其从数据库中加载部分内容.由于那将花费更多的时间来研究,因此我想先问一下是否有人解决过这个问题.

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.

  • 如果您之前已经解决了这个问题,那么您如何递归渲染某些内容并使其可被用户编辑?
  • 如果您以前没有解决过这个问题,您会建议我采取什么方式?我上面详述的方式?

提前谢谢!

推荐答案

您实际上并不需要破解" Liquid来从DB加载部分文件,只需扩展提供的文件系统类,请阅读以下内容以了解更多信息:

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

我在自己之前就已经实现了Liquid,如果您知道您会使用Ruby,我可以证明它并不是特别困难.

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.

对于递归,Liquid可能不会对其进行限制(您的模板示例应该可以使用),但是我将用Timeout :: timeout包装您的呈现过程(请参见下面的链接),以确保它不会消失永远.

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

这篇关于液体标记/液体模板中的递归的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆