datetimeapostrophe-cmsnunjucks

How to get current year in a Nunjucks template within Apostrophe CMS


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.


Solution

  • 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.