Skip to content

Commit

Permalink
Add gen_id method to the global nunjucks context.
Browse files Browse the repository at this point in the history
This method generates a random 8 digit number, left padded with zeros.
This can then be used to uniquely id items in template generated handling.
  • Loading branch information
tom-saunders-cts committed Jan 24, 2025
1 parent ff77539 commit 06e51c1
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ exports.init = function (isA11yTest = false, a11yTestSession = {}, ftValue) {
}
};
njkEnv.addGlobal('globals', globals);
function gen_id() {
const rnd_val = Math.floor(Math.random() * 100000000);
const rnd_s = rnd_val.toString().padStart(8, '0');
return rnd_s;
}
njkEnv.addGlobal('gen_id', gen_id);

app.enable('trust proxy');

Expand Down

0 comments on commit 06e51c1

Please sign in to comment.