I have added a 'subscription' piece to my project, which is set up and works correctly. This piece has a field that contains the year which the subscription is valid for. I wanted to create a new widget that would be able to check if the current user has a subscription that matches the current year, and not show itself if it does not.
I could figure out how to loop through a user's subscriptions and determine if any of them contained the correct year, but I'm having trouble finding a way to pass the current year into the widget to compare with. Is there a recommended way to pass extra data into the widget (in req.data, I assume), or any way to get the current date when building a Nunjucks template?
Thanks!
You can pass extra data to a widget from your template like
{{ apos.singleton(data.page, specialHeader, header, { specialThing: true }) }}
or
{{ apos.area(data.page, specialArea, { widgets: { header: { specialThing: true } } }) }}
and then check data.options
in your widget's widget.html
to use it.
You can also create arbitrary JavaScript functions to be used in your Nunjucks templates (referred to as helpers
). You can read about that here http://apostrophecms.org/docs/technical-overviews/how-apostrophe-handles-requests.html#template-helpers-invoking-synchronous-java-script-code-from-your-template .. this would be an easy place to return the current year.