在句柄中分配一个变量 [英] assign a variable in handlebars

查看:143
本文介绍了在句柄中分配一个变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究大商业模板平台。这个平台使用句柄语法。我需要能够根据我的URL中的一个参数设置一个值,更像是'window.location.pathname',并且我需要能够在整个站点访问这个新变量。我可以使用普通JavaScript以两种不同的方式工作,但我不想在整个站点的每个地方重新创建脚本。所以基本上,我可以使用一些帮助将我的2个vanilla脚本中的一个脚本放入格式化的句柄中。我的工作如下所示:

 < p id =BrandLogo>< / p> 
< script>
var directory = window.location.pathname;
var品牌;
var str = directory.includes(blackvue);
if(str === false){
branding = value one;
} else {
branding = value 2
}
document.getElementById(BrandLogo)。innerHTML = branding;
< / script>

 < p id =BrandLogo>< / p> 
< script>
var directory = window.location.pathname;
var品牌;
if(str =='/ URL-I-Want-To-Focus-On /'){
branding = value one;
} else {
branding = value 2
}
document.getElementById(BrandLogo)。innerHTML = branding;
< / script>

预先致谢

解决方案

首先,创建一个辅助函数:

  var handlebars = require('handlebars'); 
handlebars.registerHelper(setVar,function(varName,varValue,options){
options.data.root [varName] = varValue;
});

接下来,设置您的变量。

  {{setVargreetingHello World!}} 
{{#if some-condition}}
{{setVar品牌化一个}}
{{else}}
{{setVar品牌价值2}}
{{/ if}}



最后,使用变量:

 < DIV> 
< h1> {{问候语}}< / h1>
< / div>

document.getElementById(BrandLogo)。innerHTML = {{branding}};


I am working on the stencil platform on big commerce. this platform uses the handlebars syntax. I need to be able to set a value based on one of the parameters in my URL, more that like the 'window.location.pathname', and i need to be able to access this new variable across the site. I am able to make something work two different ways using regular JavaScript, but i do not want to recreate my script in every place throughout the site. So basically, I could use some help getting one of my 2 vanilla scripts into a handlebars for formatting. What i have that works is shown below:

<p id="BrandLogo"></p>
<script>
    var directory = window.location.pathname;
    var branding;
    var str = directory.includes("blackvue");
    if (str === false) {
        branding = value one;
    } else {
        branding = value 2
    }
    document.getElementById("BrandLogo").innerHTML = branding;
</script>

or

<p id="BrandLogo"></p>
<script>
    var directory = window.location.pathname;
    var branding;
    if (str == '/URL-I-Want-To-Focus-On/') {
        branding = value one;
    } else {
        branding = value 2
    }
    document.getElementById("BrandLogo").innerHTML = branding;
</script>

Thanks in advance

解决方案

If you are trying to set and use a local variable within handlebars try something like this:

First, create a helper function:

var handlebars = require('handlebars');
handlebars.registerHelper("setVar", function(varName, varValue, options) {
  options.data.root[varName] = varValue;
});

Next, set your variable(s).

{{setVar "greeting" "Hello World!"}}
{{#if some-condition}}
    {{setVar "branding" "Value one"}}
{{else}}
    {{setVar "branding" "Value 2"}}
{{/if}}

Finally, use the variable(s):

<div>
  <h1>{{greeting}}</h1>
</div>

document.getElementById("BrandLogo").innerHTML = {{branding}};

这篇关于在句柄中分配一个变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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