Skip to content

Latest commit

 

History

History
90 lines (65 loc) · 1.8 KB

README.md

File metadata and controls

90 lines (65 loc) · 1.8 KB

twigjs-loader

Build Status npm version Dependencies status

Description

twig.js loader for Webpack

Installation

This package requires node.js 8 at least.

Install with npm:

$ npm i -D twigjs-loader

Usage

var indexView = require('./index.twig');
console.log(indexView({ variable1: 'value' });

webpack.config.js

module.exports = {
  ...
  module: {
    rules: [
      {
        test: /\.twig$/,
        use: 'twigjs-loader',
        options: {
          functions: {
            someFunction: function() {
              return true;
            },
          }
        }
      },
      ...
  },
  ...
}

Options

Name Type Default Description
functions object undefined Extends Twig with custom functions

With Express

example, typescript example

$ npm i twigjs-loader

index.js:

import * as express from "express";
import { ExpressView } from "twigjs-loader";
import indexView from "./views/index.twig";

const app = express();
app.set("view", ExpressView);

app.get("/", (req, res) => {
  res.render(indexView, {
    url: `${req.protocol}://${req.get("host")}${req.originalUrl}`,
  })
});

const port = process.env.NODE_PORT || 8080;
app.listen(port, () => {
  console.log(`Example app listening on port ${port}.`);
});

License

Licensed under MIT.

Credits

Based on zimmo-be/twig-loader