使用MustacheRubyAPI提取密钥名?

使用MustacheRubyAPI提取密钥名?,ruby,mustache,Ruby,Mustache,Ruby Mustache API是否有从模板返回密钥名的方法 例如: require 'mustache' m = Mustache.new m.template = "Hello {{first_name}} {{last_name}}" 我想进行一个API调用——但我不知道它是什么——返回键名: [:first_name, :last_name] tokens = Mustache::Template.new('Hello {{first_name}} {{person.last_na

Ruby Mustache API是否有从模板返回密钥名的方法

例如:

require 'mustache'
m = Mustache.new
m.template = "Hello {{first_name}} {{last_name}}"
我想进行一个API调用——但我不知道它是什么——返回键名:

[:first_name, :last_name]
tokens = Mustache::Template.new('Hello {{first_name}} {{person.last_name}}').tokens
pp tokens.find_all{|token| token.is_a?(Array) && token[0] == :mustache }.map{|token| token[2][2]}

或者类似的东西。

在这一点上,我不认为API支持这一点。我已经在源代码中搜索了0.99.4版的mustache。我找到的最接近的方法是中的
has_key?
fetch
方法。阅读这些方法之后,密钥似乎没有被缓存。

在这一点上,我认为API不支持这一点。我已经在源代码中搜索了0.99.4版的mustache。我找到的最接近的方法是中的
has_key?
fetch
方法。阅读这些方法之后,密钥似乎没有被缓存。

巧合的是,我已经这样做了

IIRC我所需要做的就是实现一个
tokens
方法,如下所示:

class Mustache::Template
  def tokens(src = @source)
    p = ::Mustache::Parser.new
    p.otag = PMustache::DEFAULT_MUSTACHE_OTAG
    p.ctag = PMustache::DEFAULT_MUSTACHE_CTAG
    p.compile(src)
  end
end
(您可以忽略
PMustache::DEFAULT\u MUSTACHE\u xTAG
s,它们将设置默认分隔符。)

向其提供这样的模板:

[1] pry(main)> require 'pmustache'
=> true
[2] pry(main)> f = Mustache.new
=> #<Mustache:0x007fce2192b520>
[3] pry(main)> f.template = "this is {{a}} test"
=> "this is {{a}} test"
从那里我想你基本上想要
:mustache
标签:

[6] pry(main)> p_toks = lambda { |tok| (tok.instance_of? Array) && (tok[0] == :mustache) }      
=> #<Proc:0x007fce228b0b08@(pry):6 (lambda)>
[7] pry(main)> f.template.tokens.find_all(&p_toks)      
=> [[:mustache, :etag, [:mustache, :fetch, ["a"]]]]
[6]pry(main)>p|u-toks=lambda{tok |(tok.instance_of?Array)&&(tok[0]=:mustache)}
=> #
[7] 撬(主)>f.template.tokens.find_all(&p_toks)
=>[[:mustache,:etag,[:mustache,:fetch,[“a”]]]
我也有一些其他黑客在那里;我们有点分隔的模板变量,并在用户界面上显示它们(可能或多或少是您正在做的),这样我们就可以按功能对它们进行分组,从JSON对象加载它们,等等等等


您可能只需要某些令牌类型;从编译好的模板中提取令牌是很简单的,所以一旦你向它抛出一些模板,你就会看到你需要做什么。

巧合的是,我已经这样做了

IIRC我所需要做的就是实现一个
tokens
方法,如下所示:

class Mustache::Template
  def tokens(src = @source)
    p = ::Mustache::Parser.new
    p.otag = PMustache::DEFAULT_MUSTACHE_OTAG
    p.ctag = PMustache::DEFAULT_MUSTACHE_CTAG
    p.compile(src)
  end
end
(您可以忽略
PMustache::DEFAULT\u MUSTACHE\u xTAG
s,它们将设置默认分隔符。)

向其提供这样的模板:

[1] pry(main)> require 'pmustache'
=> true
[2] pry(main)> f = Mustache.new
=> #<Mustache:0x007fce2192b520>
[3] pry(main)> f.template = "this is {{a}} test"
=> "this is {{a}} test"
从那里我想你基本上想要
:mustache
标签:

[6] pry(main)> p_toks = lambda { |tok| (tok.instance_of? Array) && (tok[0] == :mustache) }      
=> #<Proc:0x007fce228b0b08@(pry):6 (lambda)>
[7] pry(main)> f.template.tokens.find_all(&p_toks)      
=> [[:mustache, :etag, [:mustache, :fetch, ["a"]]]]
[6]pry(main)>p|u-toks=lambda{tok |(tok.instance_of?Array)&&(tok[0]=:mustache)}
=> #
[7] 撬(主)>f.template.tokens.find_all(&p_toks)
=>[[:mustache,:etag,[:mustache,:fetch,[“a”]]]
我也有一些其他黑客在那里;我们有点分隔的模板变量,并在用户界面上显示它们(可能或多或少是您正在做的),这样我们就可以按功能对它们进行分组,从JSON对象加载它们,等等等等


您可能只需要某些令牌类型;从编译的模板中取出令牌是直截了当的,所以一旦你在它上面丢了一些模板,你就会看到你需要做什么。

没有具体的方法去做,但是作为一个开始,你可以考虑以下内容:

>> pp Mustache::Template.new('Hello {{first_name}} {{person.last_name}}').tokens
[:multi,
 [:static, "Hello "],
 [:mustache, :etag, [:mustache, :fetch, ["first_name"]]],
 [:static, " "],
 [:mustache, :etag, [:mustache, :fetch, ["person", "last_name"]]]]

编写一个遍历提取相关关键字应该是相当容易的。

没有具体的方法来做这件事,但是作为一个开始,你可以考虑以下内容:

>> pp Mustache::Template.new('Hello {{first_name}} {{person.last_name}}').tokens
[:multi,
 [:static, "Hello "],
 [:mustache, :etag, [:mustache, :fetch, ["first_name"]]],
 [:static, " "],
 [:mustache, :etag, [:mustache, :fetch, ["person", "last_name"]]]]

编写提取相关键的遍历应该相当容易。

当您只有简单的键名时,类似的一些遍历看起来可以正常工作:

[:first_name, :last_name]
tokens = Mustache::Template.new('Hello {{first_name}} {{person.last_name}}').tokens
pp tokens.find_all{|token| token.is_a?(Array) && token[0] == :mustache }.map{|token| token[2][2]}
为了满足您的确切需求,您可能需要:

pp tokens.find_all{|token| token.is_a?(Array) && token[0] == :mustache }.map{|token| token[2][2].last.to_sym}.flatten

当您只有简单的键名时,这样的一些看起来可以正常工作:

[:first_name, :last_name]
tokens = Mustache::Template.new('Hello {{first_name}} {{person.last_name}}').tokens
pp tokens.find_all{|token| token.is_a?(Array) && token[0] == :mustache }.map{|token| token[2][2]}
为了满足您的确切需求,您可能需要:

pp tokens.find_all{|token| token.is_a?(Array) && token[0] == :mustache }.map{|token| token[2][2].last.to_sym}.flatten
使用

使用


这个相关的问题有一个与PHP相关的答案:但它没有回答我的问题。我也在上问这个问题。这个相关的问题有一个与PHP相关的答案:但它没有回答我的问题。我也在上问这个问题。谢谢分享。您是否考虑过向提交拉取请求{?@DavidJames有点像,除了IMO本身它可能不是超级有用之外。不过,在这个过程中,我几乎是在愤怒中放弃了它。根据在的回复,它看起来像是一种代币方法已经存在。@DavidJames我不得不重新审视它,以了解我为什么要这么做;这是很久以前的事了。谢谢你的后续行动。@DavidJames I think我这样做是为了设置自定义分隔符;没有其他原因——对不起,我在脑海中混淆了两种不同的东西。谢谢分享。您是否考虑过向提交拉取请求{?@DavidJames有点像,除了IMO本身它可能不是超级有用之外。不过,在这个过程中,我几乎是在愤怒中放弃了它。根据在的回复,它看起来像是一种代币方法已经存在。@DavidJames我不得不重新审视它,以了解我为什么要这么做;这是很久以前的事了。谢谢你的后续行动。@DavidJames I th我这样做是为了设置自定义分隔符;没有其他原因——对不起,我在头脑中混淆了两种不同的东西。