0.1.1 • Published 5 years ago

twig-webpack-loader v0.1.1

Weekly downloads
3
License
MIT
Repository
github
Last release
5 years ago

twig-webpack-loader

Webpack loader for compiling Twig.js templates. This loader will allow you to require Twig.js views to your code. fork from twig-loader

Installation

npm install --save-dev twig-webpack-loader

Usage

Documentation: Using loaders

module.exports = {
    //...

    module: {
        loaders: [
            { test: /\.twig$/, loader: "twig-webpack-loader" }
        ]
    },

    node: {
        fs: "empty" // avoids error messages
    }
};

Loading templates

{# File: dialog.html.twig #}
<p>{{title}}</p>
// File: app.js
const template = require("dialog.html.twig");
// => returns pre-compiled template as a function and automatically includes Twig.js to your project

const html = template({title: 'dialog title'});
// => Render the view with the given context

Options

NameTypeDefaultDescription
dataobject or function(context){}The data that is exposed in the templates. Function should return an object'
functionsobjectundefinedExtends Twig with custom functions
filtersobjectundefinedExtends Twig with custom filters
testsobjectundefinedExtends Twig with custom tests
extendfunction(Twig)undefinedExtends Twig with custom tags and more

When you extend another view, it will also be added as a dependency. All twig functions that refer to additional templates are supported: import, include, extends & embed.

Custom functions, filters, tests and tags

You can use functions, filters, tests and extend options to extend Twig. See here for adding custom functions, filters and tests, and here for adding custom tags.

module.exports = {
  // ...
  rules: [
    // ...
    {
      test: /\.twig$/,
      use: [
        {
          loader: 'twig-webpack-loader',
          options: {
            functions: {
              repeat(value, times) {
                return new Array(times + 1).join(value);
              }
            },
            filters: {
              backwords(value) {
                return value.split(' ').reverse().join(' ');
              }
            },
            tests: {
              theAnswer(value) {
                return value === 42;
              }
            },
            extend(Twig) {
              Twig.exports.extendTag({
                type: 'echo',
                regex: /^echo\s+(.+)$/,
                next: [],
                open: true,
                compile: function (token) {
                  var expression = token.match[1];

                  token.stack = Twig.expression.compile.apply(this, [{
                    type: Twig.expression.type.expression,
                    value: expression
                  }]).stack;

                  delete token.match;
                  return token;
                },
                parse: function (token, context, chain) {
                  return {
                    chain: false,
                    output: Twig.expression.parse.apply(this, [token.stack, context])
                  };
                }
              });
            }
          }
        }
      ]
    }
    // ...
  ]
};

Changelog

0.1.0 / 2019-03-08

  • add options for extend Twig