使用地图中的值作为胡子模板地图中另一个值的关键点 [英] Using the value from a map as a key to another value in the map of a mustache template

查看:88
本文介绍了使用地图中的值作为胡子模板地图中另一个值的关键点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Handlebars的Java端口 jknack 。我需要创建一个A-Z列表。地图的关键是字母表中的一个字母。目前,我有胡须模板中的26个:

  {{#if A}}< li><< ; a< / a>< / li> {{else}}< li class =unused-letter> A< / li> {{/ if}} 

我可以在模型的关键字keys下添加一个字母数组,例如,并循环显示结果:

  {{keys}} 
{{#if。}} < li>< a href =#a> {{。}}< / a>< / li> {{else}}< li class =unused-letter> {{ }} {/ if}}
{{/ keys}}



<但是,这并不检查地图是否有与期间值匹配的关键字。



有没有一种方法可以使用从映射为地图中另一个对象的关键字?



更新的信息



看看马克的参赛作品,我可以看到我正从错误的方向接近问题。如果信件没有结果,我的模型在地图中没有条目。但是,如果我更新我的模型以便键(字母)具有空列表而不是没有键/条目,那么我可以只使用Map上的键,而不是尝试使用另一个条目来提供可能代表所有值的值可能的键。

解决方案

我不确定你的索引数据格式是什么样子,所以想象索引是在这个JSON中格式:

  {
items:[
{
A:[ ]
},
{
B:[
dummy,
dummy,
dummy
]
$,
{
C:[]
},
{
D:[
dummy,
dummy,
dummy
]
},
{
E:[]
},
{
F:[
dummy,
dummy,
dummy
]
}




$ b现在要遍历显示已使用/未使用的索引需要一个模板,如:

 < ul> 
{{#items}}
{{#each。}}
{{#if。}}
< li>< a href =#{{关键}}> {{@键}}< / A>< /锂>
{{else}}
< li class =unused-letter> {{@ key}}< / li>
{{if}}
{{/ each}}
{{/ items}}
< / ul>

以上示例数据会导致:

 < ul> 
< li class =unused-letter> A< / li>
< li>< a href =#B> B< / a>< / li>
< li class =unused-letter> C< / li>
< li>< a href =#D> D< / a>< / li>
< li class =unused-letter> E< / li>
< li>< a href =#F> F< / a>< / li>
< / ul>


I'm using the jknack Java port of Handlebars. I need to create an A-Z list. The key to the map is a letter of the alphabet. At the moment, I have 26 of these in the mustache template:

{{#if A}}<li><a href="#a">A</a></li>{{else}}<li class="unused-letter">A</li>{{/if}}

It would be nice of I could add an array of the letters to the model under the key "keys", for example, and loop through the results:

{{#keys}}
   {{#if .}}<li><a href="#a">{{.}}</a></li>{{else}}<li class="unused-letter">{{.}}</li>{{/if}}
{{/keys}}

However, this doesn't check to see if the map has a key matching the value of the period.

Is there a way of using a value obtained from the map as the key to another object in the map?

Updated information

Looking at Mark's entry I can see that I was approaching the problem from the wrong direction. My model didn't have an entry in the Map if a letter had no results. However, if I update my model so that a key (letter) has an empty list rather than no key/entry at all then I can just use the keys on the Map rather than trying to use another entry to provide values that could represent all possible keys.

解决方案

I'm not sure what your index data format looks like, so imagine the index was in JSON in this format:

{
   "items":[
      {
         "A":[]
      },
      {
         "B":[
            "dummy",
            "dummy",
            "dummy"
         ]
      },
      {
         "C":[]
      },
      {
         "D":[
            "dummy",
            "dummy",
            "dummy"
         ]
      },
      {
         "E":[]
      },
      {
         "F":[
            "dummy",
            "dummy",
            "dummy"
         ]
      }
   ]
}

Now to iterate through displaying used/unused indexes would require a template like:

<ul>
{{#items}}
{{#each .}}
{{#if .}}
  <li><a href="#{{@key}}">{{@key}}</a></li>
{{else}}
  <li class="unused-letter">{{@key}}</li>
{{/if}}
{{/each}}
{{/items}}
</ul>

which for the above example data would result in:

<ul>
  <li class="unused-letter">A</li>
  <li><a href="#B">B</a></li>
  <li class="unused-letter">C</li>
  <li><a href="#D">D</a></li>
  <li class="unused-letter">E</li>
  <li><a href="#F">F</a></li>
</ul>

这篇关于使用地图中的值作为胡子模板地图中另一个值的关键点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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