Handlebars基本知識(shí)

官方文檔地址:https://handlebarsjs.com/zh/api-reference/data-variables.html

1、自定義函數(shù)(助手):Handlebars.registerHelper
例:
Handlebars.registerHelper(‘demo’,function(aString){
  return aString.toUpperCase();
})

模板調(diào)用:
{{ demo firstName}}
函數(shù)(定義的助手)名+空格+參數(shù)

定義:
{
firstName:"Wang"
}

顯示結(jié)果:
WANG


2、特殊符號(hào)不想被轉(zhuǎn)意:用{{{
例:
定義:
{
specialChars:"& < > \" ' ` ="
}

模板定義:
{{specialChars}}
{{{specialChars}}}

顯示結(jié)果:
&amp; &lt; &gt; &quot; &#x27; &#x60; &#x3D;
& < > \ ' ` =


3、助手:../    確切值為父級(jí)上下文,根據(jù)調(diào)用該塊的不同而有所不同
例:
定義:
{
permalink:"111",
comments:[{ },{ }]
}

模板調(diào)用:
{{permalink}}
{{#each comments}}
  {{../permalink}} 

  {{#if title}}
    {{../permalink}}
  {{/if}}
{{/each}}


4、助手  
Handlebars.Safestring  防止html轉(zhuǎn)意
Handlebars.escapeExpression  不改變傳的值的意義,例如針對(duì)字符串的表達(dá)不轉(zhuǎn)意
例:
定義:
{ url: "https://yehudakatz.com/" }

Handlebars.registerHelper("link", function(text, url) {
      var url = Handlebars.escapeExpression(url),   //https://yehudakatz.com/
          text = Handlebars.escapeExpression(text)  //"See Website"
          
     return new Handlebars.SafeString("<a href='" + url + "'>" + text +"</a>");
});

模板定義:
{{link "See Website" url}}

輸出:
<a >See Website</a>

自定義函數(shù)中傳參高級(jí)使用:
定義:
{
  person: {
    firstname: "Yehuda",
    lastname: "Katz",
    url: "https://yehudakatz.com/"
  }
}

Handlebars.registerHelper("link", function(text, options) {  //options代表剩余所傳參數(shù)
    var attributes = [];

    Object.keys(options.hash).forEach(key => {
        var escapedKey = Handlebars.escapeExpression(key);
        var escapedValue = Handlebars.escapeExpression(options.hash[key]);
        attributes.push(escapedKey + '="' + escapedValue + '"');
    })
    var escapedText = Handlebars.escapeExpression(text);
    
    var escapedOutput ="<a " + attributes.join(" ") + ">" + escapedText + "</a>";
    return new Handlebars.SafeString(escapedOutput);
});
備注:數(shù)組.join(" ")  將數(shù)組以括號(hào)中的形式進(jìn)行字符串分割

模板調(diào)用:
{{link "See Website" href=person.url class="person"}}

輸出:
<a class="person" >See Website</a>


5、如果使用與輸入對(duì)象的屬性相同的名稱注冊(cè)了助手,則該助手的優(yōu)先級(jí)高于輸入屬性,如果出現(xiàn)重復(fù)情況可用this. 或  ./ 作為調(diào)用定義參數(shù)的前綴
例:
定義:
{name:"wenya"}

自定義助手
Handlebars.registerHelper('name', function () {
    return "Nils"
})

模板調(diào)用:
helper: {{name}}
data: {{./name}} or {{this.name}}


6、Handlebars提供對(duì)子表達(dá)式的支持,它允許您在單個(gè)胡須內(nèi)調(diào)用多個(gè)幫助程序,并將內(nèi)部幫助程序調(diào)用的結(jié)果作為參數(shù)傳遞給外部幫助程序。子表達(dá)式用括號(hào)定界。
例:
{{outer-helper (inner-helper 'abc') 'def'}}

在這種情況下,inner-helper將使用字符串參數(shù)調(diào)用'abc',
并且inner-helper函數(shù)返回的任何內(nèi)容都將作為第一個(gè)參數(shù)傳遞
outer-helper(并'def'作為的第二個(gè)參數(shù)傳遞outer-helper)。


7、~ 在大括號(hào)中添加該字符后刪除改側(cè)所有空格字符
另外補(bǔ)充:if else 的使用
{{#if test}}
      {{title}}
{{^}}
      Empty
{{/if}}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容