mozilla / nunjucks

A powerful templating engine with inheritance, asynchronous control, and more (jinja2 inspired)
https://mozilla.github.io/nunjucks/
BSD 2-Clause "Simplified" License
8.55k stars 638 forks source link

Value resolver for non plain-object contexts #946

Open cleverplatypus opened 7 years ago

cleverplatypus commented 7 years ago

Hi, I'm looking for an alternative to dustjs for my custom mvc library. Nunjucks seems to satisfy the requirements.

However, one feature that I could not find is a way to proxy the objects to be passed as context to support custom getters/setters. My library uses the ObservableObject class to update views when the model changes. It exposes the .prop(chain, [new_value]) to deep-access properties and notify any observers about value changes.

This seems to be supported in Handlebars as it is used by SproutCore and Emberjs with their Model classes. At the moment I have to call .toPlainObject() on my models before rendering with dustjs, and this creates unnecessary data duplication.

Is there a way to extend Nunjucks to support this feature?

ArmorDarks commented 7 years ago

Hm, can you explain more detailed what exactly you want to achieve?

As far as I understand your goal, out of box Nunjucks doesn't have nothing to support this. But you always can pass any methods to Nunjucks environment with addGlobal, and they will appear in your templates as functions, like {{ getMyProps('path.to.props') }}.