ランキングに戻る

twigjs/twig.js

JavaScript

JS implementation of the Twig Templating Language

twigtwigjs
スター成長
スター
1.9k
フォーク
280
週間成長
Issue
53
5001k1.5k
2023年1月2024年3月2025年5月2026年7月
成果物npmnpm install twig.js
README

Known Vulnerabilities Test Suite 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.

関連リポジトリ
getgrav/grav

Modern, Crazy Fast, Ridiculously Easy and Amazingly Powerful Flat-File CMS powered by PHP, Markdown, Twig, and Symfony

PHPPackagistMIT Licensephpcms
getgrav.org
15.6k1.4k
twigphp/Twig

Twig, the flexible, fast, and secure template language for PHP

PHPPackagistBSD 3-Clause "New" or "Revised" Licensetwigphp
twig.symfony.com
8.4k1.3k
timber/timber

Create WordPress themes with beautiful OOP code and the Twig Template Engine

PHPPackagistMIT Licensetwigwordpress
timber.github.io/docs/
5.7k508
kimai/kimai

Kimai is the #1 open-source time-tracking application. From freelancers to companies and organisations - everyone can manage timesheets, generate reports, create invoices and so much more... Web-based multi-user application, available as On-Premise or SaaS version: https://www.kimai.org

PHPPackagistGNU Affero General Public License v3.0timetrackertimetracking
kimai.org
4.8k813
bolt/bolt

Bolt is a simple CMS written in PHP. It is based on Silex and Symfony components, uses Twig and either SQLite, MySQL or PostgreSQL.

PHPPackagistMIT Licensephpcms
4.1k794
picocms/Pico

Pico is a stupidly simple, blazing fast, flat file CMS.

PHPPackagistMIT Licensepicopicocms
picocms.org
3.9k608
craftcms/cms

Build bespoke content experiences with Craft.

PHPPackagistOthercmscraftcms
craftcms.com
3.6k705
cocur/slugify

Converts a string to a slug. Includes integrations for Symfony, Silex, Laravel, Zend Framework 2, Twig, Nette and Latte.

PHPPackagistMIT Licensephptwig
2.9k246
KnpLabs/KnpPaginatorBundle

SEO friendly Symfony paginator to sort and paginate

PHPPackagistMIT Licensetwigphp
knplabs.com/en/blog/knp-paginator-reborn
1.8k332
fxbois/web-mode

web template editing mode for emacs

Emacs LispGNU General Public License v3.0emacsemacs-lisp
web-mode.org
1.7k259
FOSSBilling/FOSSBilling

Empower your hosting business with FOSSBilling, the free and open-source solution for efficient billing and client management.

PHPPackagistApache License 2.0billingbootstrap
fossbilling.org
1.6k333
twigphp/Twig-extensions

Twig extensions

PHPPackagistMIT Licensephptwig
twig.symfony.com
1.4k269