• 模板引擎ejs详解


    singsingasong.js:

    const ejs=require('ejs');
    
    ejs.renderFile('./views/singsingasong.ejs', {'name':'singsingasong',json: {arr: [
      {user: 'blue', pass: '123456'},
      {user: 'zhangsan', pass: '654321'},
      {user: 'xiaoming', pass: '999999'},
    ]}}, function (err, data){
      console.log(data);
    });
    

    singsingasong.ejs :

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="ie=edge"> -->
        <title>zhagnwen's ejs-测试</title>
    </head>
    <body>
        <div>
            <%= name %>
            <%= json.arr[0].user%>
            <%= json.arr[0].pass%>
            <%= 13 %> + <%= 4 %>
    
            <!-- 等号 是转意输出,减号 不转意输出 -->
            <% var str="<div class=‘div1’></div>" %>
            <%-str %>
            <!-- 引用 a.txt文件 -->
            <% include ../a.txt %>
    
            <%for(var i=0;i<5;i++){%>
                <% include ../a.txt %>      //引入外部文件时注意层级‘../’
                <% } %>
            }
        </div>
    </body>
    </html>
    

    知识点:

    如果用到include,必须把它单独标记出来.因为它是ejs自带的,不是js的:

    <% if(type=='admin'){ %>
    <% include ../style/admin.css %>
    <%}else{%>
    <% include ../style/user.css %>
    <% } %>
    
  • 相关阅读:
    5.9上午
    5.4下午
    5.4上午
    5.2上午
    4.19下午
    4.18上午
    4.17下午
    4.17上午
    4.12下午
    4.13下午
  • 原文地址:https://www.cnblogs.com/n2meetu/p/8066195.html
Copyright © 2020-2023  润新知