npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

twig

v1.17.1

Published

JS port of the Twig templating language.

Downloads

1,061,152

Readme

Known Vulnerabilities Build Status NPM version Gitter

About

Twig.js is a pure JavaScript implementation of the Twig PHP templating language (https://twig.symfony.com/)

The goal is to provide a library that is compatible with both browsers and server side JavaScript environments such as node.js.

Twig.js is currently a work in progress and supports a limited subset of the Twig templating language (with more coming).

Docs

Documentation is available in the twig.js wiki on Github.

Feature Support

For a list of supported tags/filters/functions/tests see the Implementation Notes page on the wiki.

Install

Download the latest twig.js release from github: https://github.com/twigjs/twig.js/releases or via NPM:

npm install twig --save

Bower

A bower package is available from philsbury. Please direct any Bower support issues to that repo.

Browser Usage

Include twig.js or twig.min.js in your page, then:

var template = Twig.twig({
    data: 'The {{ baked_good }} is a lie.'
});

console.log(
    template.render({baked_good: 'cupcake'})
);
// outputs: "The cupcake is a lie."

Webpack

A loader is available from zimmo.be.

Node Usage (npm)

Tested on node >=6.0.

You can use twig in your app with

var Twig = require('twig'), // Twig module
    twig = Twig.twig;       // Render function

Usage without Express

If you don't want to use Express, you can render a template with the following method:

import Twig from 'twig';
Twig.renderFile('./path/to/someFile.twig', {foo:'bar'}, (err, html) => {
  html; // compiled string
});

Usage with Express

Twig is compatible with express 2 and 3. You can create an express app using the twig.js templating language by setting the view engine to twig.

app.js

Express 3

var Twig = require("twig"),
    express = require('express'),
    app = express();

// This section is optional and used to configure twig.
app.set("twig options", {
    allowAsync: true, // Allow asynchronous compiling
    strict_variables: false
});

app.get('/', function(req, res){
  res.render('index.twig', {
    message : "Hello World"
  });
});

app.listen(9999);

views/index.twig

Message of the moment: <b>{{ message }}</b>

An Express 2 Example is available on the wiki.

Alternatives

Contributing

If you have a change you want to make to twig.js, feel free to fork this repository and submit a pull request on Github. The source files are located in src/*.js.

twig.js is built by running npm run build

For more details on getting setup, see the contributing page on the wiki.

Environment Requirements

When developing on Windows, the repository must be checked out without automatic conversion of LF to CRLF. Failure to do so will cause tests that would otherwise pass on Linux or Mac to fail instead.

Tests

The twig.js tests are written in Mocha and can be invoked with npm test.

License

Twig.js is available under a BSD 2-Clause License, see the LICENSE file for more information.

Acknowledgments

See the LICENSES.md file for copies of the referenced licenses.

  1. The JavaScript Array fills in src/twig.fills.js are from https://developer.mozilla.org/ and are available under the MIT License or are public domain.

  2. The Date.format function in src/twig.lib.js is from http://jpaq.org/ and used under a MIT license.

  3. The sprintf implementation in src/twig.lib.js used for the format filter is from http://www.diveintojavascript.com/projects/javascript-sprintf and used under a BSD 3-Clause License.

  4. The strip_tags implementation in src/twig.lib.js used for the striptags filter is from http://phpjs.org/functions/strip_tags and used under and MIT License.