Skip to content
New issue

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

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

defining complex helper functions in mustache.js #480

Closed
nikolas opened this issue Aug 7, 2015 · 8 comments
Closed

defining complex helper functions in mustache.js #480

nikolas opened this issue Aug 7, 2015 · 8 comments

Comments

@nikolas
Copy link
Contributor

nikolas commented Aug 7, 2015

Is it possible to define a helper function that takes multiple arguments in Mustache.js 2.1.3, like this Handlebars function?

  /**
   * {{truncate}}
   * Truncates a string given a specified `length`,
   * providing a custom string to denote an `omission`.
   * @param  {[type]} str      [description]
   * @param  {[type]} length   [description]
   * @param  {[type]} omission [description]
   * @return {[type]}          [description]
   */
  truncate: function (str, limit, omission) {
    if (Utils.isUndefined(omission)) {
      omission = '';
    }
    if (str.length > limit) {
      return str.substring(0, limit - omission.length) + omission;
    } else {
      return str;
    }
  },

(https://github.com/assemble/handlebars-helpers/blob/master/lib/helpers/helpers-strings.js#L237)

If not, would this be complicated to implement? I need this feature for one of my projects.

@bobthecow
Copy link

Sorry, this is intentionally not possible in mustache, as function calls and parameters are fundamentally incompatible with mustache's "logic-free" core principle. You should be preparing your data model before sending it to the template. If this is really something you need, handlebars might be a better fit.

@nikolas
Copy link
Contributor Author

nikolas commented Aug 7, 2015

I see - I thought it might be intentional. Mustache.js does support helper functions though: https://github.com/janl/mustache.js#functions

I thought that limiting those functions to not take any parameters seems like kind of an arbitrary limitation. If the aim is for Mustache to be "logic-free", wouldn't you remove helper functions?

@bobthecow
Copy link

Those are lambda sections, and they're an optional part of the mustache spec. I'm personally of the opinion that they're not necessary, but they do exist, and most mustache implementations support them. And yes, people abuse them to add logic to their templates.

@nikolas
Copy link
Contributor Author

nikolas commented Aug 8, 2015

I see.. thanks for the explanation.

@nikolas nikolas closed this as completed Aug 8, 2015
@muayyad-alsadi
Copy link

kindly re-open this,

Sorry, this is intentionally not possible in mustache, as function calls and parameters are fundamentally incompatible with mustache's "logic-free" core principle.

lambdas are important part of the spec

those who wrote the spec knows the principles of mustache better

Lambdas
When the value is a callable object, such as a function or lambda, the object will be invoked and passed the block of text. The text passed is the literal block, unrendered. {{tags}} will not have been expanded - the lambda should do that on its own. In this way you can implement filters or caching.

to help templates be logic-free, template designers has two options
either to put the logic on the filter or put the logic on the template

not only mustach but also Django is also logic-free, but they also have filters

for example in jinja

{{ name|striptags|upper }}

in golang templates

{{ .Name | ToUpper }}

in original mustache spec

Template:

{{#wrapped}}
  {{name}} is awesome.
{{/wrapped}}

Hash:

{
  "name": "Willy",
  "wrapped": function() {
    return function(text, render) {
      return "<b>" + render(text) + "</b>"
    }
  }
}

the point of all this is not to say that jinja or golang is better than mustach, and I know mustach is more strict than others about logic,
but the point is to show that lambda in the original spec is useful and can help having less logic in template by offloading the logic to the filter which is outside the template

@bobthecow
Copy link

This issue isn’t about filters or lambdas. I agree with you about both of those. But this issue is about calling functions from inside mustache templates which take parameters.

@Spongman
Copy link

Spongman commented Aug 4, 2019

surely you can call functions from within templates using the {{#function}}{{argument}}{{/function}} syntax. isn't it a purely syntactic change to allow the more intuitive {{function(argument)}} syntax?

@muayyad-alsadi
Copy link

more intuitive {{function(argument)}}

The more intutive in logic-free templates is

{{Argument|filter}}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants