zoukankan      html  css  js  c++  java
  • nodejs(9)使用arttemplate渲染动态页面

     

    使用arttemplate渲染动态页面

    • 安装 两个包 npm i art-template express-art-template -S

    • 自定义一个模板引擎 app.engine('自定义模板引擎的名称', 渲染函数)

    • 将自定义的模板引擎,配置为 express 的默认模板引擎 app.set('view engine', '具体模板引擎的名称')

    • 配置 模板页面的存放路径 app.set('views', '路径')

    js文件

    // 导入模块
    const express = require('express')
    
    // 创建服务器
    const app = express()
    
    // 1. 自定义一个模板引擎
    // app.engine('自定义模板引擎的名称 命名为html', 渲染函数)
    app.engine('html', require('express-art-template'))
    // 2. 设置项目中默认的模板引擎
    app.set('view engine', 'html')
    // 3.设置默认模板引擎的存放路径
    app.set('views', './views')
    
    app.get('/', (req, res) => {
      res.render('home.html', {
        name: 'houfee',
        age: 24,
        gender: '男',
        hobby: ['唱歌', '跳舞', '吃饭'],
        desc: '<h1>这是html代码</h1>'
      })
    })
    
    // 启动服务器
    app.listen(4444, () => {
      console.log('express server running at http://127.0.0.1:4444')
    })
      

     home.html

      <h1>ART views</h1>
      <p>姓名:{{name}}</p>
      <p>年龄:{{age}}</p>
      <p>性别:{{gender}}</p>
      <p>爱好:{{hobby}}</p>
      <p>介绍:{{desc}}</p>

  • 相关阅读:
    zookeeper集群搭建
    mysql当前库的查询
    逻辑卷管理
    python小技巧,各种进制的转换
    修剪我的身心
    点击右键很慢,禁用网卡就好了问题解决了(转载)
    实际应用中的C#加密
    ckeditor+ckfinder配置
    ASP.NET MVC学习总结(二)
    MVC3关于用户名的验证
  • 原文地址:https://www.cnblogs.com/houfee/p/10365914.html
Copyright © 2011-2022 走看看