Giter Site home page Giter Site logo

engine-munger's Introduction

engine-munger

A replacement Express view class that provides asynchronous resolution, allows engines to use the lookup method to locate partials, and extends the lookup method to be configurable based on i18n locale and a template specialization rule map.

This is a backport of work for Express 5

Build Status

What does i18n mean?

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 and dustjs-linkedin together.

What does specialization mean?

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.

All engine-munger does is includes a specialization map with the list of key value pairs using the karka module.

{
    specialization : {
        'jekyll': [
            {
                is: 'hyde',
                when: {
                    'whoAmI': 'badGuy'
                }
            }
        ]
    }
}

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.

Using engine-munger in an application

This example uses the 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.

var munger = require('engine-munger');
var adaro = require('adaro');
var app = require('express')();

var specialization = {
    'jekyll': [
        {
            is: 'hyde',
            when: {
                'whoAmI': 'badGuy'
            }
        }
    ]
};
app.set("view", munger({
    "dust": {
        specialization: specialization
    },
    "js": {
        specialization: specialization,
        i18n: {
            fallback: 'en-US',
            contentPath: 'locales'
        }
    }
});

var engineConfig = {}; // Configuration for your view engine

app.engine('dust', adaro.dust(engineConfig));
app.engine('js', adaro.js(engineConfig));

Running Tests:

npm test

To run coverage:

npm run cover

engine-munger's People

Contributors

aredridel avatar gabrielcsapo avatar grawk avatar jasisk avatar pvenkatakrishnan avatar thefourtheye avatar totherik avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

engine-munger's Issues

Application becomes unresponsive when a load test is done after server startup

When we restart the kraken application and send a load test against it, the instances become unresponsive.

By adding logging I noticed that the unresponsive instances when on the limitStat function, they are over the threshold of 10, in which the event is queued in pendingStats.push, but it never gets dequeued.

On a unresponsive instance with logging I see:

(out): adaro engine method
(out): after aproba in engine
(out): before adaro dust get context
(out): call to dust.render in adaro
(out): dust on load
(out): dust on load
(out): dust on load
(out): on limit stat method
(out): limit over 10: 259
(out): on limit stat method
(out): limit over 10: 260

In an responsive instance I see:

bc-frontend-8001-1 (out): adaro engine method
bc-frontend-8001-1 (out): after aproba in engine
bc-frontend-8001-1 (out): before adaro dust get context
bc-frontend-8001-1 (out): call to dust.render in adaro
bc-frontend-8001-1 (out): dust on load

If I do a less intensive load test it works correctly, and if I do a more intensive load test after the less intensive load test it also works, seems this only happens when I restart the application and run an intensive load test without warm up

Specialization breaks when lookup not called from express tryRender

In engine-munger@^1, specialization only works on the initial "tryRender" call from express. This is because the karka "resolve" method requires the view model be passed in. If a nested dust template name might be modified by specialization rules, it will not work.

Earlier engine-munger would create a map of template names -> specialized template names on each tryRender call, then add that map as _specialization to the view model.

Getting a localized string when not in a template

The library works very well working within dust templates.
But there are times when the application needs access to localized strings and we are not rendering a template (e.g. let's imagine we are responding to an ajax request with JSON data and part of that data are localized strings to be displayed to the user... there is no template being rendered and still we need to get some localized strings)...
Is it possible to address this issue somehow?

Error message on unresolved files is terrible!

We don't give an error about what file we looked for and where we looked for it, just a couple undefined in an object, which we then try to pass to rendering, which fails because undefined isn't a valid template name.

Incorrect error message with view `index`

if I render with view index and file index.dust does not exist in templates, this line cause wrong error:

search.push([name, path.join(path.basename(name), 'index' + ext)]);

[ERROR] { [Error: ENOTDIR: not a directory, stat 'www-server/public/templates/index.dust/index.dust']
  errno: -20,
  code: 'ENOTDIR',
  syscall: 'stat',
  path: 'www-server/public/templates/index.dust/index.dust' }
Tue, 22 Mar 2016 15:46:22 GMT uncaughtException 
Error
    at www-server/node_modules/makara/node_modules/engine-munger/index.js:135:17
    at www-server/node_modules/makara/node_modules/engine-munger/index.js:156:24
    at www-server/node_modules/makara/node_modules/engine-munger/index.js:96:21
    at www-server/node_modules/makara/node_modules/engine-munger/index.js:195:13
    at FSReqWrap.oncomplete (fs.js:82:15)

if view is test then the Error is correct:

[ERROR] [VError: Failed to lookup view "layouts/default/hello.dust" in directory "www-server/public/templates"]

Rendering correct paths in HTML output

I'm using iisnode and trying to render content from within a sub-folder of our main app. The node app is hosted at /node/dust but the html coming out is referring to resources off the root path.

I've tried setting the kraken basedir but the file output from the Kraken with Specialization example for the main style sheet is "/css/app.css"

How could I make it say "/node/dust/css/app.css" ?

Question: what are the various ways to manually set the expressjs-res's locale

I'm using makara in a kraken (yo generated) application. Presently looking for some documentation to find the right approach to manually set the i18n of the express.js http response object. So that right .properties file gets picked-up based on the business logic.

Below is one method I found by trail and error, where the GB version of properties file gets picked-up correctly even though my browser has Accept-Language for US locale.

res.render('index', { name: 'index', locale: 'en_GB'});

However, I'm not certain if this is the right way, or could there be some situation that I'm unaware of during the run time where (user/browser settings, or, ..?) the above approach will get overridden and an incorrect .properties file will get picked up?

Question:

  1. Can someone point me to any documentation which explains how the locale can be manually set from a kraken-node-MVC-controller?
  2. If there are various ways, then which approach gets the highest priority?
  3. if it is not the engine-munger that does this magic, then please point me to the right repo

Specialization doesn't work with dustjs 2.6+

# when specialization/internationalization is enabled for dust engine with cache
not ok 39 should be equal
  ---
    operator: equal
    expected:
      null
    actual:
      {"jse_shortmsg":"Problem rendering dust template named spcl/hyde","jse_summary":"Problem rendering dust template named spcl/hyde: wtf","jse_cause":{},"message":"Problem rendering dust template named spcl/hyde: wtf"}
    at: Stub.callback (/Users/ariastewart/Projects/engine-munger/node_modules/adaro/lib/engine.js:137:22)
  ...
not ok 40 should be equal
  ---
    operator: equal
    expected: "<h1>Hello from hyde</h1>"
    actual:
    at: Stub.callback (/Users/ariastewart/Projects/engine-munger/node_modules/adaro/lib/engine.js:137:22)
  ...```

Bump graceful-fs dependency

graceful-fs versions lower than v4 reevaluate native fs module source, which is not supported.
You should update your graceful-fs dependency to v4 or later.

Tracking issue: nodejs/node#5213.

Not able to upgrade engine-munger(version 1.1.1)

Hi,

I am using the node version of 5.3.0.
I am trying to upgrade the below module.

 I. engine-munger from the version 0.2.8 to 1.1.1

II. adarois from the version 0.1.10 to 1.0.1
III. dustjs-linkedin from the version 2.6.2 to 2.7.2
when I give the command "npm install", it is giving the below error message

Unhandled rejection Error: Cannot find module 'engine-munger/lib/expressView'
at Function.Module._resolveFilename (module.js:326:15)
at Function.Module._load (module.js:277:25)
at Module.require (module.js:354:17)
at require (internal/module.js:12:17)

Can you please provide some suggestions?

@lmarkus

export makeViewClass as namespace?

There is a discrepancy in the README example which leads you to believe that the module exports makeViewClass as a named function:

var munger = require('engine-munger');
...
app.set("view", munger.makeViewClass(viewConfig));

Should the export statement be modified to match this example? Or should the example be changed to match the current behavior?

Engine-Munger always uses fallback locale for included template lookup

It seems that the latest npm version 1.1.1 does not lookup included files correctly.
According to the debug output below only the main template is correctly found with the localised version. All included partials are only searched for the fallback language. This does not affect properties files.

The app is setup as in the read me example:

const I18N_CONFIG = {
        contentPath: "locales",
        fallback: "de-DE",
        formatPath
    };

if (app.get("env") === "development") {
    app.engine("dust", adaro.dust(DUST_CONFIG));
    app.set("views", path.join(config.root, "app/views"));
    app.set("view engine", "dust");
} else {
    app.engine("js", adaro.js(DUST_CONFIG));
    app.set("views", path.join(config.root, "build/app/views"));
    app.set("view engine", "js");
}

app.set("view", munger({
    "properties": {
        root: [I18N_CONFIG.contentPath],
        i18n: I18N_CONFIG
    },
    "js": {
        root: "build/app/views",
        i18n: I18N_CONFIG
    }
}));

The template to render is composed of an included layout and some partial includes.

{>"layouts/master" /}
{<body}
    {>"header" /}
   {!...snip...!}
{/body

From the debug output:

ENGINE-MUNGER 92843: trying locales ["RU/ru","DE/de"]
ENGINE-MUNGER 92843: lookup "home.js"
ENGINE-MUNGER 92843: stat "~/workspace/node-app/dist/public-home-germany-node/build/app/views/RU/ru/home.js"
ENGINE-MUNGER 92843: found "~/workspace/node-app/dist/public-home-germany-node/build/app/views/RU/ru/home.js"
ENGINE-MUNGER 92843: render "~/workspace/node-app/dist/public-home-germany-node/build/app/views/RU/ru/home.js"
ADARO 92843: loading '~/workspace/node-app/dist/public-home-germany-node/build/app/views/RU/ru/home.js' directly as it is an absolute path
ADARO 92843: loaded '~/workspace/node-app/dist/public-home-germany-node/build/app/views/RU/ru/home.js' as 'home'
ENGINE-MUNGER 92843: trying locales ["RU/ru","DE/de"]
ENGINE-MUNGER 92843: lookup "home.properties"
ENGINE-MUNGER 92843: stat "~/workspace/node-app/dist/public-home-germany-node/locales/RU/ru/home.properties"
ENGINE-MUNGER 92843: found "~/workspace/node-app/dist/public-home-germany-node/locales/RU/ru/home.properties"
ENGINE-MUNGER 92843: trying locales ["RU/ru","DE/de"]
ENGINE-MUNGER 92843: lookup "home.properties"
ENGINE-MUNGER 92843: stat "~/workspace/node-app/dist/public-home-germany-node/locales/RU/ru/home.properties"
ENGINE-MUNGER 92843: found "~/workspace/node-app/dist/public-home-germany-node/locales/RU/ru/home.properties"
ADARO 92843: looking up 'layouts/master' using view engine
ENGINE-MUNGER 92843: trying locales ["DE/de"]
ENGINE-MUNGER 92843: lookup "layouts/master.js"
ENGINE-MUNGER 92843: stat "~/workspace/node-app/dist/public-home-germany-node/build/app/views/DE/de/layouts/master.js"
ENGINE-MUNGER 92843: found "~/workspace/node-app/dist/public-home-germany-node/build/app/views/DE/de/layouts/master.js"
ADARO 92843: loaded '~/workspace/node-app/dist/public-home-germany-node/build/app/views/DE/de/layouts/master.js' as 'layouts/master'
ENGINE-MUNGER 92843: trying locales ["RU/ru","DE/de"]
ENGINE-MUNGER 92843: lookup "layouts/master.properties"
ENGINE-MUNGER 92843: stat "~/workspace/node-app/dist/public-home-germany-node/locales/RU/ru/layouts/master.properties"
ENGINE-MUNGER 92843: found "~/workspace/node-app/dist/public-home-germany-node/locales/RU/ru/layouts/master.properties"
ENGINE-MUNGER 92843: trying locales ["RU/ru","DE/de"]
ENGINE-MUNGER 92843: lookup "layouts/master.properties"
ENGINE-MUNGER 92843: stat "~/workspace/node-app/dist/public-home-germany-node/locales/RU/ru/layouts/master.properties"
ENGINE-MUNGER 92843: found "~/workspace/node-app/dist/public-home-germany-node/locales/RU/ru/layouts/master.properties"
ADARO 92843: looking up 'header' using view engine
ENGINE-MUNGER 92843: trying locales ["DE/de"]
ENGINE-MUNGER 92843: lookup "header.js"

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.