zoukankan      html  css  js  c++  java
  • EJS 入门学习

    EJS(Embedded JavaScript templates)是一个简单高效的模板语言,通过数据和模板,可以生成HTML标记文本。可以说EJS是一个JavaScript库,EJS可以同时运行在客户端和服务器端,客户端安装直接引入文件即可,服务器端用npm包安装。

    1.EJS常用标签

    <%    %>    //   逻辑代码输出(JS编辑),如if、for等使用
    <%=  %>   //   输出  HTML转义后  的字符串 到模板中
    <%-  %>   //   输出 原始的HTML串到模板中,不对HTML进行转义    <>被浏览器识别  看做HTML 
    <%#   %>    //注释
    <%%  %>     //直接输出 <%  %>

    EJS成员函数

    • Render(str,data,[option]):  直接渲染  字符串  并生成   html str:需要解析的字符串模板 data:数据 option:配置选项
    • Compile(str,[option]):   编译字符串得到  模板函数   str:需要解析的字符串模板 option:配置选项

    2.服务端模板 

    $ npm install ejs  

    http.createServer(function(req,res){             
        fs.readFile("ejs02.ejs","utf-8",function(err,data){  
            res.writeHead(200,{'Content-Type':'text/html'});      
            var html = ejs.render(data,{title:title,userInfo:userInfo});  
            res.end(html);  
        })    
    }).listen(8080);  
    

    app.get("/ejs",function(req,res){
      res.render("ejs1",{title:tem.message});
    });



    文档资料 转 http://www.ybao.org/book/ejs/5362.html

  • 相关阅读:
    HDU 5795 A Simple Nim ——(Nim博弈 + 打表)
    【Insertion Sorted List】cpp
    【Merge K Sorted Lists】cpp
    【Merge Two Sorted Lists】cpp
    【Merge Sorted Array】cpp
    【Sum Root to Leaf Numbers】cpp
    【Binary Tree Maximum Path Sum】cpp
    【Path Sum II】cpp
    【Path Sum】cpp
    【Maximum Depth of Binary Tree 】cpp
  • 原文地址:https://www.cnblogs.com/zhouhongdan/p/9150454.html
Copyright © 2011-2022 走看看