|
1 | | -engine-munger |
| 1 | +engine-munger |
2 | 2 | ============= |
3 | 3 |
|
4 | | -Lead Maintainer: [Aria Stewart](https://github.com/aredridel) |
| 4 | +A replacement Express view class that provides asynchronous resolution, allows |
| 5 | +engines to use the lookup method to locate partials, and extends the lookup |
| 6 | +method to be configurable based on i18n locale and a template specialization |
| 7 | +rule map. |
| 8 | + |
| 9 | +This is a backport of [work for Express 5](https://github.com/strongloop/express/pull/2653) |
| 10 | + |
| 11 | +Lead Maintainer: [Aria Stewart](https://github.com/aredridel) |
5 | 12 |
|
6 | 13 | [](https://travis-ci.org/krakenjs/engine-munger) |
7 | 14 |
|
8 | | -A template engine munging library. |
9 | | -It looks for appropriate template consolidation library for specified view engine and includes i18n and specialization in the workflow. |
| 15 | +What does i18n mean? |
| 16 | +-------------------- |
| 17 | + |
| 18 | +i18n means "internationalization". Given a `locale` property in the render options, `engine-munger` will look for content in a locale-specific directory (or in a fallback locale if that is not a match) for templates and partials. This is particularly useful with template engines that pre-localize their compiled forms, such as with [`localizr`](https://github.com/krakenjs/localizr) and [`dustjs-linkedin`](http://dustjs.com/) together. |
10 | 19 |
|
11 | | -Note: If you use specialization features, you must use dustjs before 2.6.0. |
| 20 | +What does specialization mean? |
| 21 | +------------------------------ |
12 | 22 |
|
13 | | -###### What does i18n mean ? |
14 | | -Localization of included content tags for a specified locale. Currently supported only for dust templating engine and internally uses the module [localizr](https://github.com/krakenjs/localizr) for translating content tags included in the templates |
| 23 | +Ability to switch a specific template with another based on a rule set specified in the app config. The actual rule parsing is done using the module [`karka`](https://github.com/krakenjs/karka). |
15 | 24 |
|
16 | | -###### What does specialization mean ? |
17 | | -Ability to switch a specific template with another based on a rule set specified in the app config. The actual rule parsing is done using the module [karka](https://github.com/krakenjs/karka) and can be extended and used in any templating engine and not dust. |
18 | 25 | All engine-munger does is includes a specialization map with the list of key value pairs using the karka module. |
| 26 | + |
19 | 27 | ```javascript |
20 | 28 | { |
21 | | - _specialization : { |
22 | | - ... |
23 | | - originalTemplate : <mappedTemplate> |
24 | | - ... |
| 29 | + specialization : { |
| 30 | + 'jekyll': [ |
| 31 | + { |
| 32 | + is: 'hyde', |
| 33 | + when: { |
| 34 | + 'whoAmI': 'badGuy' |
| 35 | + } |
| 36 | + } |
| 37 | + ] |
25 | 38 | } |
26 | 39 | } |
27 | 40 | ``` |
28 | 41 |
|
29 | | -##### Currently supported template engines out of the box: |
30 | | - |
31 | | -* Dust: Engine types 'js' and 'dust' |
| 42 | +The above will switch the template from `jekyll` to `hyde` if the render options contain `"whoAmI": "badGuy"`. Rules can be as complex as you need for your application and are particularly good for running A/B tests. |
32 | 43 |
|
| 44 | +Using engine-munger in an application |
| 45 | +===================================== |
33 | 46 |
|
34 | | -Simple Usage: |
| 47 | +This example uses the [`adaro`](https://github.com/krakenjs/adaro) template engine, which wraps dust up as an express view engine, and uses engine-munger's more sophisticated lookup method to find partials, allowing them to be localized and specialized based on the render options. |
35 | 48 |
|
36 | 49 | ```javascript |
37 | | -var engine-munger = require('engine-munger'), |
38 | | - app = require('express')(); |
| 50 | +var munger = require('engine-munger'); |
| 51 | +var adaro = require('adaro'); |
| 52 | +var app = require('express')(); |
| 53 | + |
| 54 | +var specialization = { |
| 55 | + 'jekyll': [ |
| 56 | + { |
| 57 | + is: 'hyde', |
| 58 | + when: { |
| 59 | + 'whoAmI': 'badGuy' |
| 60 | + } |
| 61 | + } |
| 62 | + ] |
| 63 | +}; |
| 64 | +app.set("view", munger.makeViewClass({ |
| 65 | + "dust": { |
| 66 | + specialization: specialization |
| 67 | + }, |
| 68 | + "js": { |
| 69 | + specialization: specialization, |
| 70 | + i18n: { |
| 71 | + fallback: 'en-US', |
| 72 | + contentPath: 'locales' |
| 73 | + } |
| 74 | + } |
| 75 | +}); |
39 | 76 |
|
40 | | -app.engine('dust', engine-munger['dust'](settings, config)); |
41 | | -app.engine('js', engine-munger['js'](settings, config)); |
42 | | -``` |
| 77 | +var engineConfig = {}; // Configuration for your view engine |
43 | 78 |
|
44 | | -* settings : [JSON] Arguments you want passed to the templating engine, |
45 | | -* config: [JSON] used to specify whether you need i18n/specialization enabled. It also compulsarily requires the 'view' and 'view engine' settings passed into express app. |
46 | | - |
47 | | - If you are using kraken-js 1.0 along with engine-munger, the kraken generator will automatically set this all up for you. |
48 | | - But if you want to use this with a stand alone express app with dust as templating engine, you can specify as follows: |
49 | | - |
50 | | - Example params: |
51 | | - |
52 | | - ```javascript |
53 | | - var settings = {cache: false}, |
54 | | - config = { |
55 | | - 'views': 'public/templates', |
56 | | - 'view engine': 'dust', |
57 | | - 'i18n': { |
58 | | - 'fallback': 'en-US', |
59 | | - 'contentPath': 'locales' |
60 | | - }, |
61 | | - specialization: { |
62 | | - 'jekyll': [ |
63 | | - { |
64 | | - is: 'hyde', |
65 | | - when: { |
66 | | - 'whoAmI': 'badGuy' |
67 | | - } |
68 | | - } |
69 | | - ] |
70 | | - |
71 | | - } |
72 | | - }; |
73 | | - ``` |
| 79 | +app.engine('dust', adaro.dust(engineConfig)); |
| 80 | +app.engine('js', adaro.js(engineConfig)); |
| 81 | +``` |
74 | 82 |
|
75 | 83 | Running Tests: |
76 | 84 |
|
| 85 | +```shell |
| 86 | +npm test |
77 | 87 | ``` |
78 | | -To run tests: |
79 | | -$ npm test |
80 | 88 |
|
81 | | -To run coverage |
82 | | -$ npm run-script cover |
| 89 | +To run coverage: |
83 | 90 |
|
84 | | -To run lint |
85 | | -$ npm run-script lint |
| 91 | +```shell |
| 92 | +npm run cover |
86 | 93 | ``` |
87 | | - |
88 | | - |
|
0 commit comments