diff --git a/source/_posts/ejs.md b/source/_posts/ejs.md index 2a0803f..d3cfcf8 100644 --- a/source/_posts/ejs.md +++ b/source/_posts/ejs.md @@ -8,38 +8,52 @@ tags: - Framework categories: - Programming -intro: What is the "E" for? "Embedded?" Could be. How about "Effective," "Elegant," or just "Easy"? EJS is a simple templating language that lets you generate HTML markup with plain JavaScript. No religiousness about how to organize things. No reinvention of iteration and control-flow. It's just plain JavaScript. +intro: EJS(Embedded JavaScript) is a simple templating language that lets you generate HTML markup with plain JavaScript. plugins: - copyCode --- -## Get Started { .cols-2 } +Get Started { .cols-3 } +----------- -### Install +### Hello world +#### install ``` npm install ejs ``` -### Use +#### hello.ejs +```html +<% if (user.email) { %> +

<%= user.email %>

+<% } %> +``` + +#### CLI +```shell +$ ejs hello.ejs -o hello.html +``` + + + +### Render with Data -Pass EJS a template string and some data. BOOM, you've got some HTML. ``` let ejs = require('ejs'); + let people = ['geddy', 'neil', 'alex']; -let html = ejs.render('<%= people.join(", "); %>', {people: people}); -``` +let tpl = '<%= people.join(", "); %>'; + +let html = ejs.render(tpl, {people: people}); +console.log(html); +``` +Pass EJS a template string and some data. -### CLI -Feed it a template file and a data file, and specify an output file. -``` -ejs ./template_file.ejs -f data_file.json -o ./output.html -``` ### Browser Support -Download a browser build from the latest release, and use it in a script tag. ``` ``` +Use ejs in a script tag. -## Docs -### Example -``` -<% if (user) { %> -

<%= user.name %>

-<% } %> +### Variables +| | | +|--------------|----------------------------------| +| `<%= var %>` | Prints the value of the variable | +| `<%- var %>` | Prints without HTML escaping | + + +### CLI + +Render and specify an output file. +```shell +$ ejs hello.ejs -o hello.html ``` -### Usage +Feed it a template file and a data file +```shell +$ ejs hello.ejs -f data.json -o hello.html +``` + + + +### Comments +```html +<%# This line will denote a comment %> +``` +--------- +```html +<%# This is a multi-line EJS comment. + It can span multiple lines, + but will not be displayed + in the final HTML output. +%> +``` + + + + +### Method ``` +let ejs = require('ejs'); let template = ejs.compile(str, options); + template(data); // => Rendered HTML string @@ -73,16 +119,58 @@ ejs.renderFile(filename, data, options, function(err, str){ }); ``` -### Includes + +### Including Files + +```html +<%- include('partials/navbar.ejs') %> ``` + +Include a template with data: + +```html +<% include('header', { title: 'My Page' }) %> +``` +------------ + +```html ``` +To include a template, needs a file name option, paths are relative + + + +Docs {.cols-3} +-------- + + +### Conditionals +```html +<% if (userLoggedIn) { %> +

Welcome, <%= username %>!

+<% } else { %> +

Please log in.

+<% } %> +``` + + +### Using loops +```html +<% if (userLoggedIn) { %> +

Welcome, <%= username %>!

+<% } else { %> +

Please log in.

+<% } %> +``` + + + ### Custom delimiters ``` @@ -90,22 +178,29 @@ let ejs = require('ejs'), users = ['geddy', 'neil', 'alex']; // Just one template -ejs.render('', {users: users}, +ejs.render('', + {users: users}, {delimiter: '?'}); // => 'geddy | neil | alex' // Or globally ejs.delimiter = '$'; -ejs.render('<$= users.join(" | "); $>', {users: users}); +ejs.render('<$= users.join(" | "); $>', + {users: users}); // => 'geddy | neil | alex' ``` + + + ### Caching ``` let ejs = require('ejs'), LRU = require('lru-cache'); -ejs.cache = LRU(100); // LRU cache with 100-item limit + +// LRU cache with 100-item limit +ejs.cache = LRU(100); ``` ### Custom file loader @@ -132,7 +227,7 @@ ejs.fileLoader = myFileLoader; <%- include('footer'); -%> ``` -## Client-side support { .cols-1 } +## Client-side support { .cols-2 } ### Example @@ -229,4 +324,4 @@ Examples of use : $ ejs -p [ -c ] ./template_file.ejs -o ./output.html $ ejs ./test/fixtures/user.ejs name=Lerxst $ ejs -n -l _ ./some_template.ejs -f ./data_file.json -``` \ No newline at end of file +```