小胡子:从模板中检索标签列表/哈希? [英] Mustache: Retrieve list/hash of tags from a template?

查看:90
本文介绍了小胡子:从模板中检索标签列表/哈希?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看过的所有Mustache文档和示例都显示了如何使用哈希填充模板.我有兴趣朝另一个方向发展. EG,如果我有这个话:

All the documentation and examples of Mustache I've seen show how to use a hash to populate a template. I'm interested in going the other direction. EG, if I have this:

Hello {{name}}

胡子可以生成此(伪代码)吗?

Can mustache generate this (pseudo-code):

tags = 'name'

我使用的是PHP风格的Mustache,但是我不太在意这种语言.我正在尝试建立一个系统,使人们可以使用Mustache标签创建模板,而另一个开发人员可以快速查看模板需要哪些数据.这是小胡子可以做的事情,还是我必须做一些有趣的正则表达式魔术?

I'm using the PHP flavor of Mustache, but I'm not too particular about the language. What I'm trying to do is build a system where people can create templates with Mustache tags, and another developer can quickly see what data the template will need. Is this something Mustache can do, or am I going to have to do some fun regex magic?

推荐答案

您可以使用 Hogan.js 例如在nodejs上运行,并使用scan函数:

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, '{{ }}'​)​​​​;

这返回的是一个对象数组.每个对象条目都有两个要查找的键: n 代表标签名称, tag 代表标签类型. 我认为标签类型没有明确记录,但是作为参考,_v表示纯文本,#是节的开头,而/是节的结尾.

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.

这篇关于小胡子:从模板中检索标签列表/哈希?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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