小胡子:从模板中检索标签列表/哈希?

发布于 2025-01-07 03:45:07 字数 323 浏览 0 评论 0原文

我见过的所有 Mustache 文档和示例都展示了如何使用哈希来填充模板。我有兴趣走向另一个方向。 EG,如果我有这个:

Hello {{name}}

Mustache 可以生成这个(伪代码):

tags = 'name'

我正在使用 Mustache 的 PHP 风格,但我对这种语言不太挑剔。我想做的是构建一个系统,人们可以在其中创建带有 Mustache 标签的模板,而其他开发人员可以快速查看模板需要哪些数据。这是 Mustache 可以做的事情,还是我必须做一些有趣的正则表达式魔法?

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'

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

雪若未夕 2025-01-14 03:45:07

例如,您可以使用在 Nodejs 上运行的 Hogan.js ,并使用 scan< /code> function:

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

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

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

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.

抽个烟儿 2025-01-14 03:45:07

Mustacheparse 方法可以在 JavaScript 中使用来获取给定模板内使用的标签树:

const template = "{{foo}}{{#bar}}{{baz}}{{/bar}}{{#array}}{{.}}{{/array}}"
const tokens = Mustache.parse(template)
console.log(JSON.stringify(tokens, null, 4))

这将产生以下结果:

[
    [
        "name",
        "foo",
        0,
        7
    ],
    [
        "#",
        "bar",
        7,

        15,
        [
            [
                "name",
                "baz",
                15,
                22
            ]
        ],
        22
    ],
    [
        "#",
        "array",
        30,
        40,
        [
            [
                "name",
                ".",
                40,
                45
            ]
        ],
        45
    ]
]

parse method of Mustache can be used in JavaScript to get the tree of tags used inside a given template:

const template = "{{foo}}{{#bar}}{{baz}}{{/bar}}{{#array}}{{.}}{{/array}}"
const tokens = Mustache.parse(template)
console.log(JSON.stringify(tokens, null, 4))

This will produce below:

[
    [
        "name",
        "foo",
        0,
        7
    ],
    [
        "#",
        "bar",
        7,

        15,
        [
            [
                "name",
                "baz",
                15,
                22
            ]
        ],
        22
    ],
    [
        "#",
        "array",
        30,
        40,
        [
            [
                "name",
                ".",
                40,
                45
            ]
        ],
        45
    ]
]
浅忆流年 2025-01-14 03:45:07

基于@Sanjay 的答案,我认为以下是人们可能正在寻找的内容,因为它将递归地自行提取所有部分标签。如果您希望它返回常规标签,而不是部分,请检查 current[0]==='name' 是否标识其他标签:

const template = "{{foo}}{{#bar}}{{baz}}{{/bar}}{{#array}}{{.}}{{/array}}"
const tokens = filterTokens(Mustache.parse(template))

function filterTokens(toParse, tokens=[]){
  for(let x=0; x<toParse.length; x++){
      const current = toParse[x];
      if(current[0] === '#' && !tokens.includes(current[1])){
          tokens.push(current[1]);
          if(toParse[4]){
              tokens.concat(filterTokens(current[4], tokens));
          }
      }
  }
  return tokens;

}

Building on @Sanjay's answer, I think the following is what people are likely looking for, as it will recursively pull out all of the section tags on their own. If you instead, or in addition to, want it to return regular tags, not sections, check if current[0]==='name' to identify the other tags:

const template = "{{foo}}{{#bar}}{{baz}}{{/bar}}{{#array}}{{.}}{{/array}}"
const tokens = filterTokens(Mustache.parse(template))

function filterTokens(toParse, tokens=[]){
  for(let x=0; x<toParse.length; x++){
      const current = toParse[x];
      if(current[0] === '#' && !tokens.includes(current[1])){
          tokens.push(current[1]);
          if(toParse[4]){
              tokens.concat(filterTokens(current[4], tokens));
          }
      }
  }
  return tokens;

}
所有深爱都是秘密 2025-01-14 03:45:07

我知道我迟到了这个问题,但我在寻找有关如何在 Ruby 中做同样事情的建议时偶然发现了它。因为我找到了一个适合我的解决方案,所以我想我会分享:

创建一个自定义渲染器,从 Mustache 子类化,并跟踪其中每个部分或上下文的请求。无论如何,您将想要/需要正常的渲染行为,因为您将希望捕获从其他部分引用的上下文/部分。

在 Ruby 中,这确实很容易做到——希望它对 PHP 开发人员仍然有帮助。 :)

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. :)

你如我软肋 2025-01-14 03:45:07

我自己用 Ruby 解决了这个问题,因为我无法通过 Google 找到任何真正有用的东西。事实证明它很简单:

Mustache.templateify("{{name}} was {{location}}").tags

这将返回一个标签名称数组:

["name", "位置”]

Just figured this one out myself in Ruby, since I couldn't find anything really useful through Google. Turns out it's simple:

Mustache.templateify("{{name}} was {{location}}").tags

This will return an array of tag names:

["name", "location"]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文