### EJS CLI Usage Examples Source: https://ejs.co/index Provides practical examples of using the EJS command-line interface for rendering templates, specifying output files, and providing data from files or strings. ```bash $ 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 ``` -------------------------------- ### EJS Client-Side Rendering Example Source: https://ejs.co/index Provides an example of using EJS on the client-side. It demonstrates rendering a template with data and inserting the output into the DOM using both vanilla JavaScript and jQuery. ```html
``` -------------------------------- ### Install EJS with NPM Source: https://ejs.co/index Installs the EJS templating engine using the Node Package Manager (NPM). This is the standard way to add EJS to a Node.js project. ```bash $ npm install ejs ``` -------------------------------- ### EJS Include Example Source: https://ejs.co/index Demonstrates how to include one EJS template within another, emphasizing the use of the raw output tag (`<%-`) to prevent double-escaping and passing local variables to the included template. ```ejs ``` -------------------------------- ### EJS Template Example Source: https://ejs.co/index A simple EJS template snippet showing conditional rendering based on a JavaScript variable. It uses scriptlet tags to embed JavaScript logic within HTML. ```html <% if (user) { %>

<%= user.name %>

<% } %> ``` -------------------------------- ### EJS CLI Options Source: https://ejs.co/index Lists the command-line interface options for EJS, mirroring the JavaScript configuration options, including output file, data input, delimiters, strict mode, whitespace removal, and debugging. ```bash * `cache` Compiled functions are cached, requires `filename` * `-o / --output-file FILE` Write the rendered output to FILE rather than stdout. * `-f / --data-file FILE` Must be JSON-formatted. Use parsed input from FILE as data for rendering. * `-i / --data-input STRING` Must be JSON-formatted and URI-encoded. Use parsed input from STRING as data for rendering. * `-m / --delimiter CHARACTER` Use CHARACTER with angle brackets for open/close (defaults to %). * `-p / --open-delimiter CHARACTER` Use CHARACTER instead of left angle bracket to open. * `-c / --close-delimiter CHARACTER` Use CHARACTER instead of right angle bracket to close. * `-s / --strict` When set to `true`, generated function is in strict mode * `-n / --no-with` Use 'locals' object for vars rather than using `with` (implies --strict). * `-l / --locals-name` Name to use for the object storing local variables when not using `with`. * `-w / --rm-whitespace` Remove all safe-to-remove whitespace, including leading and trailing whitespace. * `-d / --debug` Outputs generated function body * `-h / --help` Display this help message. * `-V/v / --version` Display the EJS version. ``` -------------------------------- ### EJS Layouts with Includes Source: https://ejs.co/index Illustrates a common pattern for implementing layouts in EJS by including header and footer partials. This approach helps in creating reusable page structures without explicit block support. ```html <%- include('header'); -%>

Title

My page

<%- include('footer'); -%> ``` -------------------------------- ### Basic EJS Rendering Source: https://ejs.co/index Demonstrates how to use EJS to render an HTML string with dynamic data. It shows how to pass a JavaScript array to the template for rendering. ```javascript let ejs = require('ejs'); let people = ['geddy', 'neil', 'alex']; let html = ejs.render('<%= people.join(", "); %>', {people: people}); ``` -------------------------------- ### EJS Client-Side Include Callback Source: https://ejs.co/index Demonstrates how to handle includes on the client-side in EJS using a callback function. Since direct file system access is not available, this callback allows fetching and returning the content of included files. ```javascript let str = "Hello <%= include('file', {person: 'John'}); %>"; let fn = ejs.compile(str, {client: true}); fn(data, null, function(path, d){ // include callback // path -> 'file' // d -> {person: 'John'} // Put your code here // Return the contents of file as a string }); // returns rendered string ``` -------------------------------- ### EJS CLI Usage Source: https://ejs.co/index Illustrates how to use the EJS command-line interface (CLI) to process template files. It takes a template file, a data file, and specifies an output file. ```bash ejs ./template_file.ejs -f data_file.json -o ./output.html ``` -------------------------------- ### EJS in Browser Usage Source: https://ejs.co/index Shows how to use EJS directly in a web browser by including the EJS script. It demonstrates rendering an HTML string with data in a client-side JavaScript environment. ```html ``` -------------------------------- ### EJS Rendering Methods Source: https://ejs.co/index Details the primary methods for using EJS: `compile` for creating reusable templates and `render` for direct string rendering. It also includes `renderFile` for rendering from a file. ```javascript let template = ejs.compile(str, options); template(data); // => Rendered HTML string ejs.render(str, data, options); // => Rendered HTML string ejs.renderFile(filename, data, options, function(err, str){ // str => Rendered HTML string }); ``` -------------------------------- ### EJS Configuration Options Source: https://ejs.co/index Details the various options available for configuring EJS rendering, such as caching, filename usage, root path settings, view paths, context, debug compilation, client-side compilation, delimiters, whitespace removal, escaping, output function names, and async rendering. ```ejs * `cache` Compiled functions are cached, requires `filename` * `filename` Used by `cache` to key caches, and for includes * `root` Set project root for includes with an absolute path (e.g, /file.ejs). Can be array to try to resolve include from multiple directories. * `views` An array of paths to use when resolving includes with relative paths. * `context` Function execution context * `compileDebug` When `false` no debug instrumentation is compiled * `client` Returns standalone compiled function * `delimiter` Character to use for inner delimiter, by default '%' * `openDelimiter` Character to use for opening delimiter, by default '<' * `closeDelimiter` Character to use for closing delimiter, by default '>' * `debug` Outputs generated function body * `strict` When set to `true`, generated function is in strict mode * `_with` Whether or not to use `with() {}` constructs. If `false` then the locals will be stored in the `locals` object. (Implies `--strict`) * `localsName` Name to use for the object storing local variables when not using `with` Defaults to `locals` * `rmWhitespace` Remove all safe-to-remove whitespace, including leading and trailing whitespace. It also enables a safer version of `-%>` line slurping for all scriptlet tags (it does not strip new lines of tags in the middle of a line). * `escape` The escaping function used with `<%=` construct. It is used in rendering and is `.toString()`ed in the generation of client functions. (By default escapes XML). * `outputFunctionName` Set to a string (e.g., `'echo'` or `'print'`) for a function to print output inside scriptlet tags. * `async` When `true`, EJS will use an async function for rendering. (Depends on async/await support in the JS runtime. ``` -------------------------------- ### Custom EJS File Loader Source: https://ejs.co/index Shows how to implement a custom file loader for EJS. This allows pre-processing of template files before they are read. The default loader uses `fs.readFileSync`. A custom loader can modify the file content or fetch it from different sources. ```javascript let ejs = require('ejs'); let myFileLoader = function (filePath) { return 'myFileLoader: ' + fs.readFileSync(filePath); }; ejs.fileLoader = myFileLoader; ``` -------------------------------- ### EJS Template Tags Source: https://ejs.co/index Explains the different types of tags used in EJS templates for control flow, outputting values (escaped and unescaped), comments, and literal tag output. ```ejs * `<%` 'Scriptlet' tag, for control-flow, no output * `<%_` ‘Whitespace Slurping’ Scriptlet tag, strips all whitespace before it * `<%=` Outputs the value into the template (HTML escaped) * `<%-` Outputs the unescaped value into the template * `<%#` Comment tag, no execution, no output * `<%%` Outputs a literal '<%' * `%>` Plain ending tag * `-%>` Trim-mode ('newline slurp') tag, trims following newline * `_%>` ‘Whitespace Slurping’ ending tag, removes all whitespace after it ``` -------------------------------- ### EJS Caching with LRU Cache Source: https://ejs.co/index Demonstrates how to integrate an LRU cache with EJS for caching intermediate JavaScript functions. This improves performance by reusing compiled templates. The cache limit can be configured when creating the LRU cache instance. ```javascript let ejs = require('ejs'); let LRU = require('lru-cache'); ejs.cache = LRU(100); // LRU cache with 100-item limit ``` -------------------------------- ### EJS Custom Delimiters Source: https://ejs.co/index Illustrates how to set custom delimiters for EJS templates, both on a per-template basis using the `delimiter` option during rendering and globally by modifying the `ejs.delimiter` property. ```javascript let ejs = require('ejs'), users = ['geddy', 'neil', 'alex']; // Just one template olds.render('', {users: users}, {delimiter: '?'}); // => 'geddy | neil | alex' // Or globally olds.delimiter = '$'; olds.render('<$= users.join(" | "); $>', {users: users}); // => 'geddy | neil | alex' ``` -------------------------------- ### Clearing EJS Cache Source: https://ejs.co/index Explains how to clear the EJS cache. This is useful when templates are updated and the cached versions need to be invalidated. The `ejs.clearCache()` function can be used for this purpose. ```javascript ejs.clearCache(); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.