mustache / mustache.github.com

The {{official}} website
http://mustache.github.io/
Other
2.29k stars 292 forks source link

Mustache need a standard helper-library? #140

Closed ppKrauss closed 8 months ago

ppKrauss commented 3 years ago

Mustache is simple and can do a lot of things, but some simple-tasks, typical for any template system, are not trivial with Mustache... For example the classic simple task of rendering a list with separators in TXT format:

red, green, blue need commas separating items.

The best solution is not automatic, need the "strange input" last.

{"items": [
    {"name": "red"},
    {"name": "green"},
    {"name": "blue"}
]}
{{#items}}
    {{name}}{{^last}}, {{/last}}
{{/items}} 

The "strange input" (a boolean) and the "reserved word" (last) can be injected by a helper-function... So, the best practice is to imagine "strange input" and the "reserved word" as standard Mustache's recommendations, instead "strangers". The logic of the injection is something as:

model['items'][ model['items'].length - 1 ].last = true;

And, in fact, it received ~100 votes as good practice here... See also this suggestion to transform it into a function of a reference-library... standard external library of helper-functions, or external class of helper-methods.
Imagine helper-functions grouped my prefix ms_. The function ms_items_setLast() solves the problem above, and some other functions as will be the recommended solution for other simple tasks, like the ones discussed on Stackoverflow.


Javascript:

function ms_items_setLast(x,label='') {
   x[ x.length - 1 ].last = true
   return x
}
// model = ms_items_setLast(model.items);

Python:

def ms_items_setLast(x,label=''):
      x[ len(x) - 1 ]['last'] = True
# ms_items_setLast(model['items'])

Summary: a "good practice + helper-functions" library.

PS: people who like the initiative just need a bit of discussion and blessings here, to start a new git with the library... External standalone, no impact, but ideally at github.com/mustache/stdlib.

jobol commented 3 years ago

MHO: good idea, I vote for. Though how to distinguish such strange value? Is there a need to distinguish it from other keys? If yes how? Why not parenthesis like in {{^(last)}}

ppKrauss commented 3 years ago

Hi @jobol, the Mustache language is really limited, there is no solution for the "rendering a list with separators" classic problem, when the list is a pure list (JSON array). To use the solution above you must to transform it into a "list of objects".

Another solution is to inject the separator into the array... Here a Javascript transform for each solution:

const pureList = ["red","green","blue"]
// Only two possible approaches, both using inject solutions:
let listWithSeparators = pureList.map( (x, i, arr) => x.toString()+((arr.length-1===i)? '':', ') ); // solved!
let listOfObjects = pureList.map( x=> ({name:x}) )  // need above mustache and "last" tag injection.

So, these maps are also good standard functions for our helper-library of inject transforms.

A little problem in the "injection strategy" is the reuse of values. You must to check, before inject something for an specific renderization, that the value will be not used in another place... In this example pureList = listWithSeparators or pureList = listOfObjects will be affect the future use of pureList. Sometimes you need to pollute your input data (with content duplication) to avoid destruction of reusable values.

We can assume that template transform is a terminal in the workflow that transforms data into content, so, in general, degeneration of Mustache's input data is not a problem.


About default value for last, you suggest "(last)" instead "last", is it? Well, if there are no risk on different Mustache implementations, yes, it is an option, as "_last" or any other. The most important is to be a standard (consensus) to be simple.

jgonggrijp commented 8 months ago

Closing this because this repository is about the website and not about the Mustache template language. Please take comments about the template language to https://github.com/mustache/spec/discussions.

Regarding this particular topic, previous relevant discussions can be found via https://github.com/mustache/spec/discussions/163, https://github.com/mustache/spec/discussions/153 and https://github.com/mustache/spec/discussions/164.