Using dustjs-helpers with Kraken js

2019-07-05 05:14发布

问题:

I created a project using Kraken 1.0.1 with yo kraken, with template engine dustjs, but I can not use functions dustjs-helpers.

My config.json:

"express": {
    "view cache": false,
    "view engine": "dust",
    "views": "path:./public/templates"
},

"view engines": {
    "dust": {
        "module": "engine-munger",
        "renderer": {
            "method": "dust",
            "arguments": [
                { "cache": false},
                {
                    "views": "config:express.views",
                    "view engine": "config:express.view engine",
                    "specialization": "config:specialization",
                    "i18n": "config:i18n"
                }
            ]
        }
    }
}

template.dust (not working helpers)

{@if cond="1<2"}
   <div>  x is less than y and b == c and either e or f exists in the output </div> 
 {:else}
   <div> x is >= y </div>
 {/if}

template2.dust (working)

        {?messages}
            {#messages}
                <div data-alert class="alert-box info radius">
                  {.}
                  <a href="#" class="close">&times;</a>
                </div>
            {/messages}
        {/messages}

Only dusts core working, I add dustjs-helpers with npm. How add dustjs-helpers in my kraken project?

回答1:

Having just fought things like this for the last two days. The problem is that dust helpers clobbers any earlier defined helpers when setting up it's own. See issue: https://github.com/linkedin/dustjs-helpers/issues/72

You can try using this repo version that derives from PR mentioned in the issue 72 thread to get around it for now. https://github.com/patrick-steele-idem/dustjs-helpers Or just be very sure your helpers are defined after dustjs load and dustjs-helpers is loaded.



回答2:

On Kraken.js 1.0.3+ to add dust helpers in a krakenJS project you you need to decleare them in your config.json. By default KrakenJS does not include Dust.js helpers.

Find the section dedicated to dust in your config/confing.json and add "dustjs-helpers" entry to the "helpers" array. It should look like this if you have also dust-makara enabled:

"dust": {
    "helpers": [

        "dust-makara-helpers",
        "dustjs-helpers"


    ]
},