这里是文章模块栏目内容页
template.js模板解析类库的用法

它template.js是一款纯粹js原生模板解析类库。

即当通过script标签引入template.js到页面中后,

会增加 window.template 对象供使用。

例如:

模板:

 <script id="tmpId"  type="text/html">
<h1> <%= name %></h1>
 </script>
解析模板:
var html = template(document.getElementById('tmpId')[removed], {name:'这是一个演示'});
[removed](html);


下面介绍这个类库的安装。

github地址:  https://github.com/yanhaijing/template.js

支持通过npm安装,其他安装方法没找到。

npm install template_js


然后在 node_module目录找到 template_js/dist/template.aixo.js 

这个是支持通过script标签引入页面的类库。其他是通过require或者import等es6语法导入;


用法:

输出变量 

<%= name%> 
<%=:u name%> urlencode编码

更多的过滤参考源码,它的语法类似smarty 的语法。

它支持扩展 filter过滤器,增加对参数的过滤处理。


if和else 流程:

<div class="preview-image">
<div class="el-image"  style="width:<%= medias.width %>px;height: <%= medias.height %>px">
 <a data-href="<%= medias.linkurl %>" onclick="openurl()"
  <%if (medias.target==1){ %>target="_blank"<% } %>  title="<%= medias.name %>">
  <img  src="<%=content.image[0].url %>"
alt="<%=content.image[0].name %>" class="el-image__inner" />
 </a>
</div>
</div>

好了,本文内容全部结束,感谢您的阅读,希望能帮助到您。