Security News
pnpm 10.0.0 Blocks Lifecycle Scripts by Default
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
string-template
Advanced tools
A simple string template function based on named or indexed arguments
The string-template npm package allows you to easily create and manipulate string templates. It provides a simple way to replace placeholders in a string with actual values, making it useful for generating dynamic content.
Basic Template Replacement
This feature allows you to replace placeholders in a string with corresponding values from an object. In this example, the placeholder {name} is replaced with 'World'.
const template = require('string-template');
const result = template('Hello, {name}!', { name: 'World' });
console.log(result); // Output: Hello, World!
Multiple Placeholders
You can use multiple placeholders in a single string and replace them with values from an object. This example replaces {firstName} and {lastName} with 'John' and 'Doe' respectively.
const template = require('string-template');
const result = template('Hello, {firstName} {lastName}!', { firstName: 'John', lastName: 'Doe' });
console.log(result); // Output: Hello, John Doe!
Nested Object Replacement
The package supports nested object replacement, allowing you to access properties within nested objects. In this example, {user.name} is replaced with 'Alice'.
const template = require('string-template');
const result = template('Hello, {user.name}!', { user: { name: 'Alice' } });
console.log(result); // Output: Hello, Alice!
Mustache is a logic-less template syntax. It is a popular templating engine that allows you to create templates with placeholders and replace them with actual values. Unlike string-template, Mustache supports more complex templating features such as loops and conditionals.
Handlebars is an extension of Mustache and provides a more powerful templating engine. It supports advanced features like helpers and partials, making it more suitable for complex templating needs compared to string-template.
Lodash's template function is part of the Lodash utility library. It provides a way to create compiled template functions that can interpolate data properties in 'interpolate' delimiters. It offers more flexibility and power compared to string-template, especially when used within the broader context of Lodash's utility functions.
A simple string template function based on named or indexed arguments
var format = require("string-template")
var greeting
// Format using an object hash with keys matching [0-9a-zA-Z]+
greeting = format("Hello {name}, you have {count} unread messages", {
name: "Robert",
count: 12
})
// greeting -> "Hello Robert, you have 12 unread messages"
// Format using a number indexed array
greeting = format("Hello {0}, you have {1} unread messages", ["Robert", 12])
// greeting -> "Hello Robert, you have 12 unread messages"
// Format using optional arguments
greeting = format("Hello {0}, you have {1} unread messages",
"Robert",
12)
// greeting -> "Hello Robert, you have 12 unread messages"
// Escape {} pairs by using double {{}}
var text = format("{{0}}")
// text -> "{0}"
string-template
exposes two template compiling options for when you need the
additional performance. Arguments passed to the compiled template are of the
same structure as the main string-template
function, so either a single
object/array or a list of arguments.
var compile = require("string-template/compile")
var greetingTemplate = compile("Hello {0}, you have {1} unread messages")
var greeting = greetingTemplate("Robert", 12)
// -> "Hello Robert, you have 12 unread messages"
Passing a truthy second argument to compile
will opt into using new Function
to generate a function. The function returned contains a literal string
concatenation statement, interleaving the correct arguments you have passed in.
var compile = require("string-template/compile")
var greetingTemplate = compile("Hello {0}, you have {1} unread messages", true)
// -> greetingTemplate generated using new Function
var greeting = greetingTemplate(["Robert", 12])
// -> "Hello Robert, you have 12 unread messages"
npm install string-template
FAQs
A simple string template function based on named or indexed arguments
The npm package string-template receives a total of 1,778,519 weekly downloads. As such, string-template popularity was classified as popular.
We found that string-template demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer 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
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
Product
Socket now supports uv.lock files to ensure consistent, secure dependency resolution for Python projects and enhance supply chain security.
Research
Security News
Socket researchers have discovered multiple malicious npm packages targeting Solana private keys, abusing Gmail to exfiltrate the data and drain Solana wallets.