【问题标题】:Mustache: Check for variable in current context onlyMustache:仅检查当前上下文中的变量
【发布时间】:2022-12-02 17:23:27
【问题描述】:

I\'m using the Mustache template engine in a project, and I\'ve run in to an issue where I have the same attribute name in different contexts, and I\'m wondering if there is a way to specify only the attribute from the local context, without changing the name of the attribute.

Consider having this data:

{
   title: \'Hello World\',
   href: \'http://example.com\',
   socials: [
      {
         label: \'Twitter\',
         href: \'http://twitter.com\'
      },
      {
         label: \'Facebook\',
      }
   ]
}

The issue here is that the href attribute is available in two different contexts which will cause problems if you try to use this conditionally, like this:

<div>
   <h1>{{title}}</h1>
   <a href=\"{{href}}\">{{href}}</a>
   <ul>
      {{#socials}}
         <li>
            {{#href}}
               <!--- In here http://twitter.com will be used for the first Social,
               but for Facebook it will use http://example.com.
               For Facebook, this value should be empty. --->

               <a href=\"{{href}}\">{{label}}</a>
            {{/href}}

            {{^href}}
               <!--- This will never be rendered --->
               {{label}}               
            {{/href}}
         </li>
      {{/socials}}
   </ul>
</div>

Hopefully this demonstrates my issue. What I want is to check ONLY the current context for the variable, not check the parent context as well.

I know I can use {{.}} to access the current context, but is there a way to access a specific variable in the current context? For example, {{.href}} (which I have tried, but does not work...)

标签: javascript mustache


【解决方案1】:
猜你喜欢
  • 1970-01-01
  • 2023-03-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-02
相关资源
最近更新 更多