Security News
JSR Working Group Kicks Off with Ambitious Roadmap and Plans for Open Governance
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
lodash.template
Advanced tools
The lodash.template package is a part of the Lodash library which provides utility functions for common programming tasks using the functional programming paradigm. It is specifically designed for creating compiled templates which can be used to interpolate values into strings.
Template Compilation
Compiles templates into functions that can be used to interpolate values. The 'template' function takes a string and returns a compiled template function.
const _ = require('lodash.template');
const compiled = _.template('hello <%= user %>!');
console.log(compiled({ 'user': 'fred' }));
// => 'hello fred!'
HTML Escaping
Supports HTML escaping. The '<%-' sequence in the template string is used to escape values that are interpolated, preventing XSS attacks.
const _ = require('lodash.template');
const compiled = _.template('<b><%- value %></b>');
console.log(compiled({ 'value': '<script>' }));
// => '<b><script></b>'
Use of JavaScript Expressions
Allows the use of JavaScript expressions inside templates. This enables iteration and conditional logic within the template string.
const _ = require('lodash.template');
const compiled = _.template('<% _.forEach(users, function(user) { %><li><%- user %></li><% }); %>');
console.log(compiled({ 'users': ['fred', 'barney'] }));
// => '<li>fred</li><li>barney</li>'
Custom Delimiters
Supports custom delimiters. Users can define their own delimiters for interpolation, evaluation, and escaping.
const _ = require('lodash.template');
const compiled = _.template('hello ${ user }!', { 'interpolate': /\${([\s\S]+?)}/g });
console.log(compiled({ 'user': 'pebbles' }));
// => 'hello pebbles!'
Handlebars is a popular templating engine that is more feature-rich than lodash.template. It supports helpers, partials, and complex expressions, making it suitable for more complex templating tasks.
Mustache is a logic-less templating syntax that can be used for HTML, config files, source code, etc. It is simpler than lodash.template and does not allow for logic within the templates, focusing on simple tag replacement.
EJS, or Embedded JavaScript templates, is a templating engine that allows developers to generate HTML markup with plain JavaScript. It offers similar functionality to lodash.template but with different syntax and additional features like partials and layouts.
Formerly known as Jade, Pug is a high-performance template engine heavily influenced by Haml and implemented with JavaScript for Node.js and browsers. It offers a robust feature set including includes, inheritance, and mixins, which are not present in lodash.template.
The modern build of lodash’s _.template
exported as a Node.js/io.js module.
Using npm:
$ {sudo -H} npm i -g npm
$ npm i --save lodash.template
In Node.js/io.js:
var template = require('lodash.template');
See the documentation or package source for more details.
FAQs
The Lodash method `_.template` exported as a module.
The npm package lodash.template receives a total of 2,886,006 weekly downloads. As such, lodash.template popularity was classified as popular.
We found that lodash.template demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
Security News
Research
An advanced npm supply chain attack is leveraging Ethereum smart contracts for decentralized, persistent malware control, evading traditional defenses.
Security News
Research
Attackers are impersonating Sindre Sorhus on npm with a fake 'chalk-node' package containing a malicious backdoor to compromise developers' projects.