Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

twig

Package Overview
Dependencies
Maintainers
7
Versions
63
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

twig

JS port of the Twig templating language.

  • 1.17.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
7
Created

What is twig?

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.

What are twig's main functionalities?

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!

Other packages similar to twig

FAQs

Package last updated on 16 Nov 2023

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc