Pinjasaur/templater.js

Loops

pwFoo opened this issue · 2 comments

pwFoo commented

What's about loop support in templates?

Hey @pwFoo, what would you be looking for in loop support? Similarly to #1, I'd consider the effort required to refactor & change the templating logic plus updating the test suite to be more than something I'm able to personally do right now. Happy to help facilitate a PR and work on defining a spec for what it could look like, however.

A couple thoughts off the top of my head:

  1. I dogfooded templater.js in the docs of another personal project of mine, meed, and used a native JS loop to build up the rendered output before writing it to the DOM: https://codepen.io/Pinjasaur/pen/byNoGv (the demo doesn't fully work right now for other reasons: Pinjasaur/meed#1)
  2. One of the features I added previously was for a key in the context object to be a function which is called at runtime and the returned result is used as the value rendered in the template. I'm wondering if this feature could be used to create a loop functionality but defined within the context object versus the template. When I initially added the feature I kept it very minimal: the function has no arguments. But maybe it would make sense to have optional arguments e.g. the template, context object, or similar to how Mustache supports lambdas? https://mustache.github.io/mustache.5.html#Sections
pwFoo commented

I think ja loop should be fine and avoid bloating up templates.js. thanks!