模板引擎在浏览器和服务器的渲染方法,及他们的区别(art-template)

本文详细介绍了art-template模板引擎的使用方法,包括在浏览器和Node.js环境中的应用,以及如何在Express框架中进行配置。同时,还讲解了子模板include和模板继承的语法,以及服务端渲染与客户端渲染的区别。

art-template模板引擎

art-template 不仅可以在浏览器使用,也可以在 node 中使用
art-templateAPI :http://aui.github.io/art-template/zh-cn/docs/index.html

在浏览器中使用模板引擎(art-template)

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>06-在浏览器中使用art-template</title>
</head>
<body>
  <!-- 
    注意:在浏览器中需要引用 lib/template-web.js 文件

    强调:模板引擎不关心你的字符串内容,只关心自己能认识的模板标记语法,例如 {{}}
    {{}} 语法被称之为 mustache 语法,八字胡啊。
   -->
  <script src="node_modules/art-template/lib/template-web.js"></script>
  <script type="text/template" id="tpl">
    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <title>Document</title>
    </head>
    <body>
      <p>大家好,我叫:{{ name }}</p>
      <p>我今年 {{ age }} 岁了</p>
      <h1>我来自 {{ province }}</h1>
      <p>我喜欢:{{each hobbies}} {{ $value }} {{/each}}</p>
    </body>
    </html>
  </script>
  <script>
    var ret = template('tpl', {
      name: 'Jack',
      age: 18,
      province: '北京市',
      hobbies: [
        '写代码',
        '唱歌',
        '打游戏'
      ]
    })
    console.log(ret)
  </script>
</body>
</html>

在node中使用模板引擎(art-template)

安装:
npm install art-template
该命令在哪执行就会把包下载到哪里。默认会下载到 node_modules 目录中
node_modules 不要改,也不支持改。

在 Node 中使用 art-template 模板引擎

  1. 安装 npm install art-template
  2. 在需要使用的文件模块中加载 art-template
    只需要使用 require 方法加载就可以了:require(‘art-template’)
    参数中的 art-template 就是你下载的包的名字
    也就是说你 isntall 的名字是什么,则你 require 中的就是什么
  3. 查文档,使用模板引擎的 API

node.js

var template = require('art-template')
var fs = require('fs')
fs.readFile('./tpl.html', function (err, data) {
  if (err) {
    return console.log('读取文件失败了')
  }
  // 默认读取到的 data 是二进制数据
  // 而模板引擎的 render 方法需要接收的是字符串
  // 所以我们在这里需要把 data 二进制数据转为 字符串 才可以给模板引擎使用
  var ret = template.render(data.toString(), {
    name: 'Jack',
    age: 18,
    province: '北京市',
    hobbies: [
      '写代码',
      '唱歌',
      '打游戏'
    ],
    title: '个人信息'
  })
  console.log(ret)
})

tpl.html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>{{ title }}</title>
</head>
<body>
  <p>大家好,我叫:{{ name }}</p>
  <p>我今年 {{ age }} 岁了</p>
  <h1>我来自 {{ province }}</h1>
  <p>我喜欢:{{each hobbies}} {{ $value }} {{/each}}</p>
  <script>
    var foo = '{{ title }}'
  </script>
</body>
</html>

在Express中配置使用express-art-template模板引擎

art-template-GitHub仓库
art-template 官方文档

  1. 安装
npm install --save art-template
//普通的
npm install --save express-art-template 
//搭配express使用的模板引擎
  1. 配置
    配置使用 art-template 模板引擎
    第一个参数,表示,当渲染以 .art 结尾的文件的时候,使用 art-template 模板引擎
//第一个参数用来配置视图的后缀名,这里是art,则你存储在views目录中的模板文件必须是xxx.art
//app.engine('art',require('express-art-template'))

//这里我们把art改为html
app.engine('html', require('express-art-template'))
  1. 使用
    Express 为 Response 相应对象提供了一个方法:render
    render 方法默认是不可以使用,但是如果配置了模板引擎就可以使用了
    res.render(‘html模板名’, {模板数据})
    第一个参数不能写路径,默认会去项目中的 views 目录查找该模板文件
    也就是说 Express 有一个约定:开发人员把所有的视图文件都放到 views 目录中
app.get('/', function (req, res) {
  res.render('index.html', {
     title: 'hello world'
  })
})

如果想要修改默认的 views视图渲染存储目录,可以

// 注意:第一个参数views千万不要写错
app.set('views', 目录路径)

子模板 include

  1. 语法
{{include './header.art'}}
{{include './header.art' data}}
  1. 案例
    index.html
<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="UTF-8">
   <title>Document</title>
</head>
<body>
   {{ include './header.html' }}
   <h1>中间内容</h1>
   {{ include './footer.html' }}
</html>

header.html

<h1>头部</h1>

footer

<h1>尾部</h1>

运行后显示

头部
中间内容
尾部

模板继承

  1. 语法
{{extend './layout.art'}}
{{block 'head'}} ... {{/block}}
  1. 案例
    layout.html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
  
  <link rel="stylesheet" href="/node_modules/bootstrap/dist/css/bootstrap.css">
  {{ block 'css' }}{{ /block }}
    <!-- 留坑添加css -->
    
</head>
<body>

  {{ include './header.html' }}
  <!-- 留坑 -->
  {{ block 'content' }}
    <h1>默认内容</h1>
  {{ /block }}
  {{ include './footer.html' }}
  
  <script src="/node_modules/jquery/dist/jquery.js"></script>
  <script src="/node_modules/bootstrap/dist/js/bootstrap.js"></script>
  {{ block 'script' }}{{ /block }}
  <!-- 留坑添加js -->
  
</body>
</html>

index.html

<!-- 引入layout.html页面 -->
{{extend './layout.html'}}

<!-- 向名为css的block填坑 -->
{{ block 'css' }}
<style>
  body {
    background-color: skyblue;
  }
</style>
{{ /block }}

{{ block 'content' }}
<div>
  <h1>index 页面填坑内容</h1>
</div>
{{ /block }}

{{ block 'script' }}
  <script>
    window.alert('index 页面自己的 js 脚本')
  </script>
{{ /block }}

header.html

<h1>头部</h1>

footer

<h1>尾部</h1>

模板继承和子模板

使用模板继承和子模板,可以省略一些公共内容,减少代码量。

服务端渲染和客户端渲染的区别

  • 客户端渲染不需要重新加载页面,所以速度上较快。
  • 客户端渲染不利于 SEO 搜索引擎优化
  • 服务端渲染是可以被爬虫抓取到的,客户端异步渲染是很难被爬虫抓取到的
  • 所以你会发现真正的网站既不是纯异步也不是纯服务端渲染出来的,而是两者结合来做的
  • 服务端渲染可以在网页源代码中看到渲染后的代码,而客户端渲染的看不到,只能点击查看元素中看到。
  • 例如京东的商品列表就采用的是服务端渲染,目的了为了 SEO 搜索引擎优化
  • 而它的商品评论列表为了用户体验,而且也不需要 SEO 优化,所以采用是客户端渲染
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值