2

I am in the process of adding a copyright line to the footer of a website that I have been working on and I cannot find the best way to get the current dynamically into the footer so I never have to set it again. I have tried multiple things, including a global setting that can be accessed from anywhere, but nothing has worked.

Any feedback would be appreciated. Thanks.

Doug Niccum
  • 196
  • 4
  • 16

1 Answers1

3

Write an ApostropheCMS nunjucks helper function. See those docs for the general issues around this. Your specific function could look like:

self.addHelpers({
  thisYear: function() {
    return new Date().getFullYear();
  }
});

If you put that in construct of a module of your own, let's say it's called helpers, then you can call it in Nunjucks as {{ apos.helpers.thisYear() }}.

These are very handy, just remember they cannot do any async work.

Tom Boutell
  • 7,281
  • 1
  • 26
  • 23
  • Thanks for the reply. I have added the code above to a module and my template and I am getting the following error: `Unable to call apos["helpers"]["thisYear"], which is undefined or falsey` – Doug Niccum Jan 17 '20 at 21:52
  • Is the module's alias option set to "helpers"? – Tom Boutell Jan 18 '20 at 22:06
  • The code that I am using is below. It is also worth adding that my server won't run when I add this module to the `app.js` file. This is the contents of index.js file within the `helpers` module directory: `module.exports = { extend: 'apostrophe-module', label: 'Helpers', alias: 'helpers', construct: function(self, options, callback) { self.addHelpers({ thisYear: function() { return new Date().getFullYear(); } }); } };` – Doug Niccum Jan 20 '20 at 13:14
  • You chose to include a callback for construct, but never invoked it. Remove the callback argument completely. – Tom Boutell Jan 21 '20 at 15:01
  • That fixed the issue. Thank you for your help. – Doug Niccum Jan 30 '20 at 18:03