Skip to main content

Looping

np.Templating template processor supports a variety of JavaScript code formats. The following examples demonstrate how you can use looping controls (for, .forEach, etc) and output internal control-flow variables in your templates

for loops

<% for(let i = 0; i <= 10; i++) { %> <%- i %> <% } %>

Produces the following

 0  1  2  3  4  5  6  7  8  9  10

array.forEach (Simple)

The following example will iterate an array of values, outputting each item

### Event Data

<% events = ['event1','event2','event3'] -%>
<% events.forEach((event, i) => {
%><%- event %>
<%});%>

Produces the following

### Event Data

event1
event2
event3

array.forEach

<% names = ['mike','kira','joelle','brady','bailey','trevor'] %>
<% names.forEach((name, i) => {
%><%- i === names.length - 1 ? utils.titleCase(name) : utils.titleCase(name) + '\n' %><%});%>

<%- '#### array.forEach (handling whitespace)\n'%><% names = ['mike','kira','joelle','brady','bailey','trevor'] %><% names.forEach((name, i) => {
%><%- i === names.length - 1 ? utils.titleCase(name) : utils.titleCase(name) + '\n' %><%});%>

Produces the following

#### array.forEach (handling whitespace)
Mike
Kira
Joelle
Brady
Bailey
Trevor