Search code examples
javascriptnode.jsnunjucksapostrophe-cms

Get widget options in template


I know it's possible to add an alias to index.js, but this is not recommended for NPM modules.

So, how do I pass JavaScript variables like the options object to a Nunjucks template?

data.widget only contains an id, the type and editable. Strangely enough, it doesn't contain the label.

index.js

module.exports = {
  extend: 'apostrophe-widgets',
  label: 'Privacy Cookie Widget',

  [...]
}

widget.html

<div class="[...]-widget">
  [...]

  {{ data.widget.label }} <!-- Nothing. -->

  [...]
</div>

home.html

[...]
{{ apos.singleton(data.[global|page], 'widgetName', 'widget-name', {}) }}
[...]

Solution

  • from a template you can get to your widgets root options config like this

    {{ apos.log(apos.modules['my-cool-widgets'].options.coolStuff) }}
    

    Where coolStuff is defined in my-cool-widgets/index.js

    module.exports = {        
      extend: 'apostrophe-widgets',        
      label: 'Cool Widg',
      coolStuff: {
        array: [1,2,3,4],
        hello: 'boom boom',
        hehe: true
      },
      addFields: [...]        
    };