node. JS Manual Query -5-ejs method

Source: Internet
Author: User

Use in Express
Set up a template directoryApp.Set(' Views ',Pathjoin (__dirname,  ' views '  //set template engine app. ( ' view engine ' ,  HTML ' //set the engine suffix. The contents of index.html can be Ejs code app.< Span class= "PLN" >engine (, require ( ' Ejs '               

Ejs Features: 1, caching function, the ability to cache the parsed HTML template, 2, <% code%> to execute the JavaScript code, 3, <%= code%> HTML Escape code, 4, <%-code %> will not be escaped, 5, support custom tags, such as ' <% ' can use ' {', '%> ' with '} ' instead; 6, provide some auxiliary functions for use in Templates 7, using <%-include filename%> Loading other page templates;

Ejs function/Operator
    1. Introduce a template without specifying a suffix to find layout.ejs. (Note: If the template directory is not defined, include is not valid)

<% include layout.html%> <%-include layout.html%>

    1. Statically compiled templates, without IO operations, can be very fast and common local variables.
      <% Users.foreach (user) {%> <% include user/show%> <%})%>

It means that the template name is dynamic.

    1. Customize the closing label. Tired of <%%>?
//设置闭合标签var ejs = require(‘ejs‘); ejs.open = ‘{{‘; ejs.close = ‘}}‘;

Global Settings in Express

app.set("view options",{ "open":"{{", "close":"}}"});
    1. Variable
//传递 index 模板引擎参数res.render(‘index‘,{‘title‘:‘haha‘);//使用参数<title><%= title %></title>
    1. Run JS Code
  <%for ( var I=p. Length-1; I>=0; I--) {%><input type= "Button" Value=<%=p[i ]%>><%}%>    
    1. option in Express through Res.redirect (' login ', [options]; To access the template, options can set some of the template engine's choices some parameters of options are: 1, Cache: If the parsed template is cached, filename is required as key, 2, FileName: template filename, 3, the context in which the function executes after scope:complile, 4, debug: Identifies whether the Debeg state, Debug is true to output the generated function content, 5, Compiledebug: Identity is compiled debug, True generates trace information during parsing, for debugging, 6, client, identity is used for browser client run, True returns the parsed function functions that can be run independently, 7, open, Code start tag, default to ' <% ', 8, close, code end tag, default to '%> ', 9, other variables provided when parsing the template. When used in Express, the options parameter is passed in by Response.render, which contains settings in express and user-supplied variable values.
    1. F Auxiliary Functions In addition, EJS provides some auxiliary functions to replace the use of JavaScript code, making it easier to manipulate the data. 1, first, returns the element of the array, 2, last, returns the final element of the array, 3, capitalize, returns the first uppercase String, 4, downcase, returns the lowercase of the string, 5, UpCase, returns the uppercase of the string, 6, sort, Sort (object.create (obj). Sort ()? ); 7, sort_by: ' prop ', sorted in ascending order according to the specified prop attribute, 8, size, return length, that is, long property, not necessarily an array, 9, plus:n, plus N, will be converted to number for operation; 10, Minus:n, Minus N, will be converted to number for operation, 11, times:n, multiplied by N, will be converted to number for operation, 12, Divided_by:n, divided by N, will be converted to number for operation; 13, join: ' Val ', use array ' Val ' most separators, which are merged into a string, 14, Truncate:n, intercepts the first n characters, exceeds the length, returns a copy of 15, Truncate_words:n, gets the first n Word,word in the string separated by a space; 16. Replace:pattern,substitution, string substitution, substitution does not provide that the matched substring will be deleted, 17, Prepend:val, if the operand is an array, then the merge is done, and the string is added Val in front; 18. Append:val, if the operand is an array, it is merged, and the string is appended with Val in the back; 19, map: ' prop ', returns an array of values in the object array that are prop, 20, reverse, flipping an array or string; 21, get: ' Prop ', gets the value of the property ' prop '; 22, JSON, converted to JSON format string

Auxiliary functions use

//注意闭合标签  <%=:  %><input type="button" value=<%=:p|first%>>
    1. Add a method to a template

app.locals[' say ' = function () {return ' Hello ';}; The methods defined in App.locals can be referenced in the template <input type= "button" Value=<%=say ()%>>

node. JS Manual Query -5-ejs method

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.