当前位置:网站首页>node结合art-template 模板引擎 创建模板文件
node结合art-template 模板引擎 创建模板文件
2022-06-11 12:23:00 【爱玲姐姐】
art-template 模板引擎
1. art-template 模板引擎的基础概念
1.1 art-template 模板引擎
art-template 模板引擎是第三方模块。
让开发者以更加友好的方式拼接字符串,使项目代码更加清晰、更加易于维护。
// 未使用模板引擎的写法
var ary = [{
name: '张三', age: 20 }];
var str = '<ul>';
for (var i = 0; i < ary.length; i++) {
str += '<li>\ <span>'+ ary[i].name +'</span>\ <span>'+ ary[i].age +'</span>\ </li>';
}
str += '</ul>';
<!-- 使用模板引擎的写法 -->
<ul>
{
{each ary}}
<li>{
{$value.name}}</li>
<li>{
{$value.age}}</li>
{
{/each}}
</ul>
1.2 art-template模板引擎
在命令行工具中使用 npm install art-template 命令进行下载
使用const template = require(‘art-template’)引入模板引擎
告诉模板引擎要拼接的数据和模板在哪 const html = template(‘模板路径’, 数据);
使用模板语法告诉模板引擎,模板与数据应该如何进行拼接
1.3 art-template代码示例
// 导入模板引擎模块
const template = require('art-template');
// 将特定模板与特定数据进行拼接
const html = template('./views/index.art',{
data: {
name: '张三',
age: 20
}
});
<div>
<span>{
{data.name}}</span>
<span>{
{data.age}}</span>
</div>
2. 模板引擎语法
2.1 模板语法
art-template同时支持两种模板语法:标准语法和原始语法。
标准语法可以让模板更容易读写,原始语法具有强大的逻辑处理能力。
标准语法: { { 数据 }}
原始语法:<%=数据 %>
2.2 输出
将某项数据输出在模板中,标准语法和原始语法如下:
标准语法:{ { 数据 }}
原始语法:<%=数据 %>
<!-- 标准语法 -->
<h2>{
{value}}</h2>
<h2>{
{a ? b : c}}</h2>
<h2>{
{a + b}}</h2>
<!-- 原始语法 -->
<h2><%= value %></h2>
<h2><%= a ? b : c %></h2>
<h2><%= a + b %></h2>
2.3 原文输出
如果数据中携带HTML标签,默认模板引擎不会解析标签,会将其转义后输出。
标准语法:{ {@ 数据 }}
原始语法:<%-数据 %>
<!-- 标准语法 -->
<h2>{
{@ value }}</h2>
<!-- 原始语法 -->
<h2><%- value %></h2>
2.4 条件判断
<!-- 标准语法 -->
{
{if 条件}} ... {
{/if}}
{
{if v1}} ... {
{else if v2}} ... {
{/if}}
<!-- 原始语法 -->
<% if (value) { %> ... <% } %>
<% if (v1) { %> ... <% } else if (v2) { %> ... <% } %>
2.5 循环
标准语法:{ {each 数据}} { {/each}}
原始语法:<% for() { %> <% } %>
<!-- 标准语法 -->
{
{each target}}
{
{$index}} {
{$value}}
{
{/each}}
<!-- 原始语法 -->
<% for(var i = 0; i < target.length; i++){ %>
<%= i %> <%= target[i] %>
<% } %>
2.6 子模版
使用子模板可以将网站公共区块(头部、底部)抽离到单独的文件中。
标准语法:{ {include ‘模板’}}
原始语法:<%include(‘模板’) %>
<!-- 标准语法 -->
{
{include './header.art'}}
<!-- 原始语法 -->
<% include('./header.art') %>
2.7 模板继承
使用模板继承可以将网站HTML骨架抽离到单独的文件中,其他页面模板可以继承骨架文件。
2.8 模板继承示例
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>HTML骨架模板</title>
{
{block 'head'}}{
{/block}}
</head>
<body>
{
{block 'content'}}{
{/block}}
</body>
</html>
<!--index.art 首页模板-->
{
{extend './layout.art'}}
{
{block 'head'}} <link rel="stylesheet" href="custom.css"> {
{/block}}
{
{block 'content'}} <p>This is just an awesome page.</p> {
{/block}}
2.9 模板配置
向模板中导入变量 template.defaults.imports.变量名 = 变量值;
设置模板根目录 template.defaults.root = 模板目录
设置模板默认后缀 template.defaults.extname = ‘.art’
边栏推荐
- JMeter learning experience
- Splunk certificate expired, making kV store unable to start
- 14. Course summary and review
- Record a troubleshooting of MySQL master-slave asynchrony
- 9. Parler de threadlocal
- STM32 development of practical series 7-data Porter DMA
- Jerry's aicmd command [chapter]
- Some common websites
- ftp服务器:serv-u 的下载及使用
- flink 滚动窗口、滑动窗口、会话窗口、全局窗口
猜你喜欢

What scenarios can the member management system of the multi guest swimming pool achieve?

羽毛球馆有哪些创收方式

Netstat command details

Troubleshoot Splunk kvstore "starting"

Flick scrolling window, sliding window, session window, global window

Workload management of Splunk best practices

游泳馆暑期业绩翻倍的方法

flink 数据流图、并行度、算子链、JobGraph与ExecutionGraph、任务和任务槽

ZABBIX installation and configuration application

Acwing50+Acwing51周赛+Acwing3493.最大的和(未完结)
随机推荐
Master-slave replication of MySQL
(solve) the kV store down problem of Splunk
Splunk health checks orphaned searches
Redis RDB and AOF
Wechat authorization to obtain mobile phone number
9、聊聊ThreadLocal
flink Spark 和 Flink对比
知物由学 | 行为时序建模在社交引流黑产识别中的应用
经营体育馆有哪些要素?
Jerry's aicmd_ SET_ BT_ Addr command format [chapter]
2、CompletableFuture
8、原子操作类之18罗汉增强
罗景:连接效率优化实践
What is QoS? (quality of service)
flink 多流转换(侧输出流分流、Union、Connect) 实时对账app 的支付操作和第三方的支付操作的双流 Join
Wireshark packet capturing and debugging RTSP
oracle删除用户后,却还可以使用该用户登录
14. Course summary and review
Meichuang technology data security management platform won the "leading scientific and Technological Achievement Award" of 2022 digital Expo
11. Synchronized and lock escalation