在句柄中分配一个变量 [英] assign a variable in handlebars
问题描述
< 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屋!