Security News
Node.js EOL Versions CVE Dubbed the "Worst CVE of the Year" by Security Experts
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
The 'twig' npm package is a JavaScript implementation of the Twig templating engine, which is originally a PHP-based template engine. It allows developers to create dynamic and reusable templates for web applications. Twig is known for its simplicity, flexibility, and performance.
Template Rendering
This feature allows you to define and render templates with dynamic data. The example shows how to create a simple template and render it with a variable.
const Twig = require('twig');
// Define a template
const template = Twig.twig({
data: 'Hello, {{ name }}!'
});
// Render the template with data
const output = template.render({ name: 'World' });
console.log(output); // Output: Hello, World!
Template Inheritance
Template inheritance allows you to create a base template and extend it in child templates. This promotes reusability and maintainability of your templates. The example demonstrates how to extend a base template and render it with dynamic content.
const Twig = require('twig');
// Define a base template
const baseTemplate = Twig.twig({
id: 'base',
data: '<html><body>{% block content %}{% endblock %}</body></html>'
});
// Define a child template that extends the base template
const childTemplate = Twig.twig({
data: '{% extends "base" %}{% block content %}Hello, {{ name }}!{% endblock %}'
});
// Render the child template with data
const output = childTemplate.render({ name: 'World' });
console.log(output); // Output: <html><body>Hello, World!</body></html>
Custom Filters
Custom filters allow you to extend Twig's functionality by defining your own filters. The example shows how to create a custom 'capitalize' filter and use it in a template.
const Twig = require('twig');
// Define a custom filter
Twig.extendFilter('capitalize', function(value) {
return value.charAt(0).toUpperCase() + value.slice(1);
});
// Define a template using the custom filter
const template = Twig.twig({
data: 'Hello, {{ name|capitalize }}!'
});
// Render the template with data
const output = template.render({ name: 'world' });
console.log(output); // Output: Hello, World!
Nunjucks is a powerful templating engine for JavaScript, inspired by Jinja2 (Python) and Twig (PHP). It offers similar features such as template inheritance, custom filters, and asynchronous rendering. Nunjucks is known for its flexibility and ease of use, making it a popular choice for JavaScript developers.
Handlebars is a popular templating engine that provides a simple and intuitive syntax for creating dynamic templates. It supports features like template partials, helpers, and custom expressions. Handlebars is known for its performance and is widely used in both client-side and server-side applications.
EJS (Embedded JavaScript) is a templating engine that allows you to generate HTML markup with plain JavaScript. It supports features like template partials, custom filters, and includes. EJS is known for its simplicity and ease of integration with Express.js and other Node.js frameworks.
Twig.js is a pure JavaScript implementation of the Twig PHP templating language (http://twig.sensiolabs.org/)
The goal is to provide a library that is compatable with both browsers and node.js.
Twig.js is currently a work in progress and supports a limited subset of the Twig templating language (with more coming).
Supported tags:
Filters are not yet supported, but most of the expression syntax is implemented.
Documentation is available in the twig.js wiki
Twig.js is available under the BSD 2-clause license http://www.opensource.org/licenses/BSD-2-Clause
See the LICENSE file for details
The fills for Array.forEach and Array.indexOf in src/twig.fills.js are from https://developer.mozilla.org/ and used under a Creative Commons Attribution-ShareAlike 2.5 License
FAQs
JS port of the Twig templating language.
The npm package twig receives a total of 233,874 weekly downloads. As such, twig popularity was classified as popular.
We found that twig demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 7 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
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
Security News
cURL and Go security teams are publicly rejecting CVSS as flawed for assessing vulnerabilities and are calling for more accurate, context-aware approaches.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.