Skip to content

Fork of jfrk/twig-loader webpack loader for compiling Twig.js templates to support Drupal SDC

License

Notifications You must be signed in to change notification settings

mtbdata711/twig-loader

 
 

Repository files navigation

twig-loader

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

This is a fork of @jfrk/twig-loader to support using Drupal's Single Directory Components in Twig.js.

Installation

npm i @mtbdata711/twig-loader

Usage

Documentation: Using loaders

module.exports = {
    //...

    module: {
        rules: [
            {
                test: /\.twig$/,
                loader: "twig-loader",
                options: {
                    // See options section below
                },
            }
        ]
    },

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

Options

  • extender: optional; the full path to a module which exports a function(Twig) which extends Twig (such as adding filters and functions). Must use export default instead of module.exports. Example: __dirname + "/src/extendTwig.js"
  • paths: optional; an array of absolute paths that contain Twig templates. When this option is included, paths that doesn’t begin with a . or / inside tags like {% include %} and {% extends %} will be considered relative to one of these paths instead of the file. This mimics the PHP implementation of Twig.
  • twigOptions: optional; a map of options to be passed through to Twig. Example: {autoescape: true}
  • sdc - optional; object of namespaces and paths to SDC namespaces. Example: {design_system: path.resolve(__dirname, './components')}

Loading templates

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

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

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.

About

Fork of jfrk/twig-loader webpack loader for compiling Twig.js templates to support Drupal SDC

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 100.0%