<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          wet-template簡(jiǎn)易服務(wù)器端 WEB 模板引擎

          聯(lián)合創(chuàng)作 · 2023-09-29 01:08

          wet 名稱來(lái)縮寫(xiě)于 WEb Template。

          wet 是一個(gè)基于 ES6 的簡(jiǎn)易服務(wù)器端 WEB 模板引擎,完全沒(méi)有第三方依賴。

          安裝

          Github: https://github.com/hyjiacan/wet

          Gitee: https://gitee.com/hyjiacan/wet

          npm: https://www.npmjs.com/package/@hyjiacan/wet

          npm i @hyjiacan/wet

          模板用法

          wet 模板基于 html 語(yǔ)法,模板語(yǔ)言也與常用的 js 寫(xiě)法一致。

          demo.html

          <div>
            <ul>
              <t-for on="item of list">
                <li>{{item}}</li>
              </t-for>
              <t-for on="item in set">
                <li>
                  {{item.key}}: {{item.value}}
                  <t-with xx="item.value.x.y">
                    <span>{{xx.z}}</span>
                  </t-with>
                </li>
              </t-for>
            </ul>
            <t-if on="visible">
              condition is true
            </t-if>
            <t-else>
              condition is false
            </t-else>
            <t-tree on="treeData as item">
              <div>
                {{item.data}}
                <div>
                	<t-children />    
                </div>
              </div>
            </t-tree>
            <t-include file="../common/footer.html" />
          </div>
          const context = {
            list: [1, 2, 3],
            set: {
              a: 1,
              b: {
                x: {
                  y: {z: 5}
                }
              },
              c: 3
            },
            visible: false
          }
          wet.render('/path/to/demo.html', context, {cache: true})

          通過(guò)調(diào)用 wet.render(template, context, options?) 執(zhí)行模板的渲染,此函數(shù)會(huì)返回渲染后的HTML字符串。

          • template 模板文件,可以是相對(duì)路徑或絕對(duì)路徑
          • context 模板的上下文,為模板渲染提供數(shù)據(jù)。必須是對(duì)象,不能是數(shù)組
          • options 可選的選項(xiàng),目前只有一個(gè)字段 cache,用于指示是否緩存模板文件。注:此緩存指緩存模板文件的DOM結(jié)構(gòu),并不是渲染結(jié)果

          t-for

          此標(biāo)簽用于遍歷數(shù)據(jù)集。

          for..of 遍歷數(shù)組

          <t-for on="item of array" step="1">
          <span>{{item}}</span>
          </t-for>

          step 是步長(zhǎng),默認(rèn)為 1

          也可以寫(xiě)作

          <t-for on="item, index of array">
          <span>{{index}}: {{item}}</span>
          </t-for>

          index 為數(shù)組的索引,從 0 開(kāi)始

          另外, for...of 也可以直接根據(jù)指定數(shù)值進(jìn)行循環(huán)

          <t-for on="item of 10">
          <span>{{item}}</span>
          </t-for>

          <t-for on="item of 1-10">
          <span>{{item}}</span>
          </t-for>

          1-10 表示從 1 到 10,當(dāng)起始位置為 0 時(shí) 0-10 ,可以省略為:10。 - 兩側(cè)不能有空白。僅支持整數(shù)。

          范圍始終包含邊界值,如:

          • 1-10: 1, 2, ..., 9, 10
          • 0-10: 0, 1, ..., 9, 10
          • 10: 0, 1, ..., 9, 10
          • 9: 0, 1, ..., 9

          array 暫不支持?jǐn)?shù)組字面量

          for..in 遍歷集合

          <t-for on="item in object">
          <span>{{item.key}}: {{item.value}}</span>
          </t-for>

          也可以寫(xiě)作

          <t-for on="value, key in object">
          <span>{{item.key}}: {{item.value}}</span>
          </t-for>

          object 暫不支持對(duì)象字面量

          t-if

          條件分支: <t-if on="condition">,condition 應(yīng)該是一個(gè)條件表達(dá)式

          t-elif

          條件分支 <t-elif on="condition">,condition 應(yīng)該是一個(gè)條件表達(dá)式

          t-else

          其它條件分支: <t-else>

          t-with

          設(shè)置作用域。

          <t-with varName="a.b.c">
            {{varName}}
          </t-with>

          在此范圍內(nèi),varName 等于 a.b.c 的值,varName 應(yīng)該是一個(gè)合法的標(biāo)識(shí)符。

          此標(biāo)簽一般用于簡(jiǎn)寫(xiě)層次比較深的對(duì)象引用。

          t-tree / t-children

          渲染樹(shù)結(jié)構(gòu)

          <t-tree on="tree as item">
            <div>{{item.title}}</div>
            <div>{{item.description}}</div>
            <div>
              <t-children />
            </div>
          </t-tree>
          • tree 是上下文中的樹(shù)結(jié)構(gòu)變量,其應(yīng)該是一個(gè)數(shù)組(array, 以支持多個(gè)根結(jié)點(diǎn))
          • item 是每個(gè)節(jié)點(diǎn)在此作用域內(nèi)的變量名稱

          另外還有一個(gè)配合使用的標(biāo)簽 <t-children field="children" />,此標(biāo)簽標(biāo)記子節(jié)點(diǎn)應(yīng)該渲染的位置

          其 field 屬性指定了子元素的字段名稱,默認(rèn)值為 children,即當(dāng)字段名稱為 children 時(shí),屬性 field 可省略。 t-children 的子元素會(huì)被直接丟棄。

          t-html

          提供直接渲染HTML的方法,當(dāng)不使用此標(biāo)簽時(shí),表達(dá)式的輸出會(huì)被轉(zhuǎn)義。

          例:

          <t-html>{{'<p></p>'}}</t-html>,輸出 <p></p>。

          而在不使用 t-html 時(shí): {{'<p></p>'}},輸出 &lt;p&gt;&lt;/p&gt;

          t-include

          包含功能支持 <t-include file="./another.html" />

          其屬性 file 是一個(gè)相對(duì)文件路徑,相對(duì)于當(dāng)前文件的目錄。

          在 t-include 下,只允許出現(xiàn) t-fill 作為其子項(xiàng)。

          組合使用 t-include/t-fill/t-hole 可以實(shí)現(xiàn)布局模板的功能。

          t-hole/t-fill

          t-hole 在模板文件中預(yù)留 hole,以在模板文件被 include 時(shí)進(jìn)行填充。

          a.html

          <div>
              <t-hole name="title">
                <div>默認(rèn)內(nèi)容</div>
              </t-hole>
              <t-hole></t-hole>
          </div>

          在上面的模板文件中,定義了兩個(gè) hole。一個(gè)具名的 title<t-hole name="title">,另一個(gè)是匿名的: <t-hole>

          不論是具名還是匿名,相同名稱只能聲明一次。

          b.html

          <t-include file="a.html">
            <t-fill>填充匿名 hole</t-fill>
            <t-fill name="title">填充具名 hole: title</t-fill>
          </t-include>

          不論是具名還是匿名,相同名稱只能填充一次。

          {{}}

          表達(dá)式支持: {{var}}{{obj.prop.value}}, `{{a - b}}

          如果想要原樣輸出{{/}}符號(hào),那么寫(xiě)作 {!{/}!},此時(shí)不會(huì)被解析,: {{/}}。

          瀏覽 21
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          編輯 分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          編輯 分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  欧美色影院 | 久草天堂| 欧美人妻日韩视频 | 黄色网址中文字幕不卡 | 国产黄视频在线看 |