Doug Niccum
Doug Niccum

Reputation: 235

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.

Upvotes: 2

Views: 3845

Answers (2)

Beavis
Beavis

Reputation: 1

Without adding anything externally, just inside the .njk create a <span>-tag with id-attribute, create a script which sets data to this span.

Example :

<p><span id="current-year"> All rights reserved.</p>

<script> document.getElementById('current-year').textContent = new Date().getFullYear(); </script>

Upvotes: 0

Tom Boutell
Tom Boutell

Reputation: 7572

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.

Upvotes: 3

Related Questions