1 | # twig-loader [![Build Status](https://travis-ci.org/zimmo-be/twig-loader.svg)](https://travis-ci.org/zimmo-be/twig-loader)
|
2 | Webpack loader for compiling Twig.js templates. This loader will allow you to require Twig.js views to your code.
|
3 |
|
4 | ## Installation
|
5 |
|
6 | `npm install twig-loader`
|
7 |
|
8 | ## Usage
|
9 |
|
10 | ### Webpack 2 and later
|
11 |
|
12 | [Documentation: Using loaders](https://webpack.js.org/concepts/loaders/)
|
13 |
|
14 | ``` javascript
|
15 | module.exports = {
|
16 | //...
|
17 |
|
18 | module: {
|
19 | rules: [
|
20 | {
|
21 | test: /\.twig$/,
|
22 | use: {
|
23 | loader: 'twig-loader',
|
24 | options: {
|
25 | // See options section below
|
26 | },
|
27 | }
|
28 | }
|
29 | ]
|
30 | },
|
31 |
|
32 | node: {
|
33 | fs: "empty" // avoids error messages
|
34 | }
|
35 | };
|
36 | ```
|
37 |
|
38 | ### Webpack 1
|
39 |
|
40 | [Documentation: Using loaders](http://webpack.github.io/docs/using-loaders.html?branch=master)
|
41 |
|
42 | ``` javascript
|
43 |
|
44 | module.exports = {
|
45 | //...
|
46 |
|
47 | module: {
|
48 | rules: [
|
49 | {
|
50 | test: /\.twig$/,
|
51 | loader: "twig-loader",
|
52 | options: {
|
53 | // See options section below
|
54 | },
|
55 | }
|
56 | ]
|
57 | },
|
58 |
|
59 | node: {
|
60 | fs: "empty" // avoids error messages
|
61 | }
|
62 | };
|
63 | ```
|
64 |
|
65 |
|
66 |
|
67 | ### Options
|
68 |
|
69 | - `twigOptions`: optional; a map of options to be passed through to Twig.
|
70 | Example: `{autoescape: true}`
|
71 |
|
72 | ## Loading templates
|
73 |
|
74 | ```twig
|
75 | {# File: dialog.html.twig #}
|
76 | <p>{{title}}</p>
|
77 | ```
|
78 |
|
79 | ```javascript
|
80 | // File: app.js
|
81 | var template = require("dialog.html.twig");
|
82 | // => returns pre-compiled template as a function and automatically includes Twig.js to your project
|
83 |
|
84 | var html = template({title: 'dialog title'});
|
85 | // => Render the view with the given context
|
86 |
|
87 | ```
|
88 |
|
89 | 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.
|
90 |
|
91 |
|
92 | ## Dynamic templates and registering at runtime
|
93 |
|
94 | twig-loader will only resolve static paths in your templates, according to your webpack configuration.
|
95 | When you want to use dynamic templates or aliases, they cannot be resolved by webpack, and will be
|
96 | left untouched in your template. It is up to you to make sure those templates are available in Twig
|
97 | at runtime by registering them yourself:
|
98 |
|
99 | ``` javascript
|
100 | var twig = require('twig').twig
|
101 | twig({
|
102 | id: 'your-custom-template-id,
|
103 | data: '<p>your template here</p>',
|
104 | allowInlineIncludes: true,
|
105 | rethrow: true
|
106 | });
|
107 | ```
|
108 |
|
109 | Or more advanced when using `webpack.context`:
|
110 | ``` javascript
|
111 | var twig = require('twig').twig
|
112 |
|
113 | var context = require.context('./templates/', true, /\.twig$/)
|
114 | context.keys().forEach(key => {
|
115 | var template = context(key);
|
116 | twig({
|
117 | id: key, // key will be relative from `./templates/`
|
118 | data: template.tokens, // tokens are exported on the template function
|
119 | allowInlineIncludes: true,
|
120 | rethrow: true
|
121 | });
|
122 | });
|
123 |
|
124 | ```
|
125 |
|
126 |
|
127 |
|
128 | ## Changelog
|
129 | 0.4.1 / 2018-06-12
|
130 | ==================
|
131 | * Upgrade mocha to fix security vulnerability warning
|
132 |
|
133 | 0.4.0 / 2018-05-17
|
134 | ==================
|
135 | * Add ablity to pass options to twig (PR #39)
|
136 |
|
137 | 0.3.1 / 2017-11-08
|
138 | ==================
|
139 | * Update to Twig.js 1.10, fixes #29
|
140 |
|
141 | 0.3.0 / 2017-02-19
|
142 | ==================
|
143 | * replace full path with a hash and implement mapcache for id/path resolution, fixes #12
|
144 |
|
145 | 0.2.4 / 2016-12-29
|
146 | ==================
|
147 | * Downgrade Twig.js back to 0.8.9 because of https://github.com/twigjs/twig.js/issues/440
|
148 |
|
149 | 0.2.3 / 2016-06-11
|
150 | ==================
|
151 | * Improve watch operation (rebuilding of modules)
|
152 | * Refactoring so compiler and the loader are in seperate modules
|
153 | * Add Twig as peer dependency
|
154 |
|
155 | 0.2.2 / 2016-06-03
|
156 | ==================
|
157 |
|
158 | * Add `embed` support
|
159 | * Update Twig.js version
|
160 |
|
161 | 0.2.1 / 2016-04-18
|
162 | ==================
|
163 |
|
164 | * Improve `import` support (https://github.com/zimmo-be/twig-loader/pull/8)
|
165 | * Rethrow exceptions when they occur during rendering to improve testing
|
166 |
|
167 | 0.2.0 / 2016-01-21
|
168 | ==================
|
169 |
|
170 | * Add support for import statements (useful for Macro's)
|
171 | * Correctly resolve dependencies from include/import/extend statements with relative path support: [\#3] and [\#5]
|
172 | * CHANGE: No longer add the `.twig` file extension. After upgrading twig-loader, you may need to update your files and add `.twig` manually
|
173 |
|