Problems with Dust.js Logic Helpers

I am working with jQuery 1.8.2 and Dust.js v1.1.1 for MVC style templates in a JavaScript application. When I use the logical assistant {@gt} , I get the following console error:

 Uncaught TypeError: Cannot read property 'gt' of undefined 

I believe that my template uses the correct syntax:

 <ul class="listview"> {#seasons} <li> <h3>{name}</h3> <p class="desc">{id}</p> {@gt key="months" value="0"} <span class="count">{months}</span> {/gt} </li> {/seasons} </ul><!--// end .listview --> 

Here is the JSON structure:

 { "seasons":[ { "name":"Spring", "id":"weklv7", "months": 8 }, { "name":"Summer", "id":"lvuec5", "months": 4 } ] } 

If I remove the logical assistant {@gt} from the template, the error disappears and the template loads correctly as HTML. For instance:

 <ul class="listview"> {#seasons} <li> <h3>{name}</h3> <p class="desc">{id}</p> <span class="count">{months}</span> </li> {/seasons} </ul><!--// end .listview --> 

Any help is much appreciated, thanks!

+4
source share
6 answers

It seems your problem is that dust.helpers does not exist, possibly because it has been removed from the Dust kernel . Make sure you have a dust core and Dust helpers load wherever your templates are displayed.

+5
source

Pay particular attention to the smfoote link provided.

The specific syntax you need to โ€œloadโ€ the vacuum cleaners in node:

 var dust = require('dustjs-linkedin'); dust.helper = require('dustjs-helpers'); 

Note that this is not what is written on the link, the document links that it should be dust.helper (S) with 's'.

You will not find this phrase anywhere on the main documentation site.

Apparently, it should be completely intuitively clear that everything should be so connected.

Sometimes I feel that people who do not document the impact of the significant changes they make to their projects are stealing my life. This particular gap in documentation crept in about 5 hours.

To help those souls who launched both npm install dustjs-linkedin AND npm install assistive vacuum cleaners and wonder why this doesnโ€™t just work automatically (since this is practically what all the documentation on the site offers), I will recover errors, which you will usually have when you get this error here:

  if( dust.helpers[name]){ ^ 

and then one of ...

 TypeError: Cannot read property 'if' of undefined TypeError: Cannot read property 'math' of undefined TypeError: Cannot read property 'eq' of undefined TypeError: Cannot read property 'not eq' of undefined TypeError: Cannot read property 'ne' of undefined TypeError: Cannot read property 'lt' of undefined TypeError: Cannot read property 'gt' of undefined TypeError: Cannot read property 'select' of undefined TypeError: Cannot read property 'size' of undefined TypeError: Cannot read property 'tap' of undefined TypeError: Cannot read property 'contextDump' of undefined TypeError: Cannot read property 'idx' of undefined TypeError: Cannot read property 'sep' of undefined 

Interestingly, if you assign dustjs helpers to "helpers" rather than "helpers", you get another error:

 undefined:1 ").write("</form>").write("\n\n").helper("contextDump",ctx,{},null).write("\n\ ^ TypeError: Cannot call method 'write' of undefined 

Itโ€™s trivial to write a little code to detect when someone is trying to use something starting with โ€œ@โ€ and say .. hey, you probably havenโ€™t loaded into your auxiliary library ... and make it work in node and not- node ... or they can just fix the documentation.

I sincerely hope that the extra 15 minutes I spent to put together the answer that Google can make Grock will save someone else 5 hours that I just lost.

-ft

+7
source

Despite the fact that ftrotter gave excellent information here, I just wanted to add, as I was still lost in the "s". Here is what I needed to do to write a custom helper:

 dust.helper = require('dustjs-helpers'); dust.helpers.myHelper = function (chunk, ctx, bodies, params) { 

Observe that I do not have an "s" when required, but I need to add an "s" when writing my assistant.

+3
source

To get a working client side (for example, launching in a browser only for the client), viewing the source on this working demo page helped many: http://linkedin.github.com/dustjs/test/test.html?q=helpers

This site is also very useful only for debug syntax.

For example, the select example does not work for compiling client-side javascript.

 {@select key=\"{foo}\"} {@eq value=\"bar\"}foobar{/eq} {@eq value=\"baz\"}foobaz{/eq} {@default} - default Text{/default} {/select} 

But it does:

  {@select key="{foo}"} {@eq value="bar"}foobar{/eq} {@eq value="baz"}foobaz{/eq} {@default} - default Text{/default} {/select} 

The kind of obvious why change works, but as you learn, this page is very useful.

+1
source

If you cannot add helpers, which would be a really strange solution, you can create your own function like this, which can be used instead of @gt or @math

 var baseContext = dust.makeBase({ position: function(chunk, context) { return context.stack.index + 1; }, }); 

Now you can use {position} instead of $ {idx}, which will count a cycle from 1 to n.

0
source

Edit:

I learned a lot more about dust since I wrote it a couple of days ago. Here are some clarifications!

The dust assistants library provides only trained assistants listed in the documentation. no need to create your own helpers. require('dustjs-linkedin') is interchangeable with require('dustjs-helpers') depending on whether you want to use the default helpers. (In your case, you do it.)

Also, I found this when looking for help so that it all works in Express, but I understand that your question has nothing to do with it. I hope that this work is useful to those who can stumble on this question in the same way as I do, so I will leave it here.


Note: dustjs-helpers is required in dustjs-linkedin and returns an instance from it, so you really need to do var dust = require('dustjs-helpers'); to get the right dust with auxiliary support (the same as the helpers used). You need to install dustjs-linkedin so that the request is successful, but what is it.

However, I was tired of the struggle for consolidation, adaro, dustjs-linkedin, dustjs-helpers, etc., none of which revealed functionality in an easy way for use in Express and wrote a small wrapper.

Here is my sample code with a couple of helpers added.

https://gist.github.com/myndzi/8837944

I call it in my application settings as follows:

 var dust = require('./lib/dust-express'); app.engine('dust', dust(app, { publicdir: path.join(app.get('basedir'), 'public'), mtimes: app.util.mtimes, helpers: true })); 

Hope this will be helpful to someone.

0
source

Source: https://habr.com/ru/post/1445264/


All Articles