Mustache: Retrieve list/hash of tags from a template?

我只是一个虾纸丫 提交于 2019-12-06 14:17:55

You could use Hogan.js running on nodejs for example, and use the scan function:

var template = "{{foo}}{{#bar}}{{baz}}{{/bar}}{{#array}}{{.}}{{/array}}"'

var parsedTree = Hogan.scan(template, '{{ }}'​)​​​​;

What this returns is an array of objects. Each object entry has two keys that you want to look for: n represents the tag name, tag represents the tag type. I don't think the tag types are clearly documented, but as a reference _v means plain text, # is a section start and / is section end.

I know I'm late to this question but I stumbled upon it when looking for a recommendation on how to do the same thing, in Ruby. Since I found a solution that's working well for me, I thought I would share:

Create a custom renderer, subclassed from Mustache, and track the requests for each partial or context there. You're going to want/need the normal rendering behavior anyway, since you'll want to catch contexts/partials referenced from other partials.

In Ruby, this is really easy to do -- hope it's still helpful for the PHP devs out there, too. :)

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!