Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

@ select helper usage and unit tests #16

Closed
vybs opened this Issue Apr 25, 2012 · 11 comments

Comments

Projects
None yet
5 participants
Contributor

vybs commented Apr 25, 2012

@select helper usage and unit tests

@ghost ghost assigned brikis98 Apr 25, 2012

select commented Apr 25, 2012

hello select here :D not sure what I should do

Contributor

vybs commented Apr 25, 2012

Oops! Apologize select! false alarm !

Hahah. Dust's @ syntax for helpers is going to end up @ mentioning a bunch of people on github :)

Contributor

rragan commented May 3, 2012

FYI, I think using @ when entering helper examples will dodge the issue.

@ghost ghost assigned jleppert May 21, 2012

Contributor

vybs commented May 21, 2012

@iamleppert

Here is the sample code @brikis98 came up with
using if helper

{#items}

{element}{~n} {/items}

Alternatively with select

{#items}

{~n} {/items}

Most of the checks are simple == ( and I think we can benefit from select helper avoiding the eval in the if )

if can be used for expressions that needs ORs

Please follow up with Jim ( lets get this into the standard helpers )

Slightly more complete example of the select helper:

{@select key="age"}
  {@eq value="2"}terrible two{/eq}
  {@lt value="10"}child{/lt}
  {@lt value="20"}teen{/lt}
  {@else}adult{/else}
{/select}

Note that eq, lt, gte, etc. are all helpers that work in a standalone format as well. For example:

<p>Does firstName equal Bob? {@eq key="firstName" value="Bob" type="string"}Yes!{:else}No!{/eq}</p>
<p>You {@gte key="age" value="21" type="number"}are{:else}are not{/gte} allowed to drink.</p>

The type parameter in these helpers lets you coerce the value attribute correctly. Supported options include string, number, date, boolean, and context (ie, read another value from the context).

I hacked up some working implementations for all of these, let me know if you want them.

select commented May 22, 2012

will you please put spaces in between all @ symbols !!!!1!11!!!!11111

select commented May 22, 2012

... and can i unsubscribe from this thread?

select commented May 22, 2012

ah i think i found it :D

Contributor

jleppert commented May 22, 2012

@brikis98 can you add code to the thread, I don't see it in the repo? or send me a pull request

This was done as a quick hack in another project and not directly in the dust repo, so it'll need some massaging, but here is the relevant code:

var isSelect = function(context) {
  var value = context.current();
  return typeof value === "object" && value.isSelect === true;
};

var filter = function(chunk, context, bodies, params, filter) {
  params = params || {};
  var actual;
  if (params.key) {
    actual = context.get(params.key);
  } else if (isSelect(context)) {
    actual = context.current().value;
    if (context.current().isResolved) {
      filter = function() { return false; };
    }
  } else {
    throw "No key specified for filter and no key found in context from select statement";
  }

  var expected = params.value;
  if (filter(expected, coerce(actual, params.type, context))) {
    if (isSelect(context)) {
      context.current().isResolved = true;
    }
    return chunk.render(bodies.block, context);
  } else if (bodies['else']) {
    return chunk.render(bodies['else'], context);
  }

  return chunk.write('');
};

var coerce = function(value, type, context) {
  if (value) {
    switch (type || 'number') {
      case 'number': return +value;
      case 'string': return String(value);
      case 'boolean': return Boolean(value);
      case 'date': return new Date(value);
      case 'context': return context.get(value);
    }
  }

  return value;
};

dust.helpers = _.extend(dust.helpers, {    
  select: function(chunk, context, bodies, params) {
    return chunk.render(bodies.block, context.push({isSelect: true, isResolved: false, value: context.get(params.key)}));
  },

  eq: function(chunk, context, bodies, params) {
    return filter(chunk, context, bodies, params, function(expected, actual) { return actual === expected; });
  },

  lt: function(chunk, context, bodies, params) {
    return filter(chunk, context, bodies, params, function(expected, actual) { return actual < expected ; });
  },

  lte: function(chunk, context, bodies, params) {
    return filter(chunk, context, bodies, params, function(expected, actual) { return actual <= expected ; });
  },

  gt: function(chunk, context, bodies, params) {
    return filter(chunk, context, bodies, params, function(expected, actual) { return actual > expected; });
  },

  gte: function(chunk, context, bodies, params) {
    return filter(chunk, context, bodies, params, function(expected, actual) { return actual >= expected; });
  },

  "else": function(chunk, context, bodies, params) {
    return filter(chunk, context, bodies, params, function(expected, actual) { return true; });
  }
});

@jleppert jleppert closed this in 89bdd6c May 30, 2012

sethkinast pushed a commit to sethkinast/dustjs that referenced this issue Mar 3, 2015

Merge pull request #16 from riksidhu/gh-pages
Documentation for Dust Partials
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment