Index
Conditional Statements


Just js (inside the EJS brackets)
	<% if (1 === 1) { %>
		I am inside an if block!
	<% } %>
GOTCHA: the "else" statement must be as shown (i.e., on same line as brackets)
	<% if (0 === 1) { %>
		Hello, I am in if true block!
	<% } else { %>
		I am actually in the else block!
	<% } %>
We declare vars in the usual js way.
	<% const message = 'Wow I am a var named <message>'; %>
<% const rec = { w: 10, h: 2 }; %> <% const varNames = ['w', 'h']; %> <% const recs = [ {w: 10, h: 2}, {w: 5, h: 1}, {w: 8, h: 7}, {w: 3, h: 11} ]; %>
NOTE WELL -- Starting tag includes equal sign
"<%=".


Typically vars would be provided by model, but we declare them here to simplify the example.
	<% const message = 'Wow I am a var named <message>'; %>
	Message is  <%= message %>
<% const rec = { w: 10, h: 2 }; %> <p> Width is <%= rec.w %> </p> <p> Height is <%= rec.h %> </p>
Here we build a table by iterating over a coll of recs and a coll of var names.

The vars in this snippet are shown here...
	Setting up vars 'varNames' and 'recs'
	<% const varNames = ['w', 'h']; %>
	<% const recs = [
		{w: 10, h: 2},
		{w: 5, h: 1},
		{w: 8, h: 7},
		{w: 3, h: 11}
	]; %>
	<hr>
	<p>Iterating over recs (rows) and varNames (cols)</p>
	<table style='border: 1px dashed green;'>
	<thead style='color: blue;'>
		<tr>
		<% varNames.forEach(varNm => { %>
			<td> <%= varNm %> </td>
		<% }); %>
		</tr>
	</thead>
	<% recs.forEach(rec => { %>
		<tr>
			<% varNames.forEach(varNm => { %>
				<td> <%= rec[varNm] %> </td>
			<% }); %>
		</tr>
	<% }); %>
	</table>
How about a fun switch statement.
	<p> -- Switch Statement </p>
<% switch ('boo') { case 'foo': %> Hello I'm in the 'foo' case <% break; case 'boo': %> Hello I'm in the 'boo' case <% break; } %>


Including (Importing)


We can import EJS from other files using the includes statement.

Note that a dash is included in the start tag
"<%-"


We can import an EJS file via the "include" statement.
	<% if ('foo' === 'foo') { %>
		<%- include('fields/checkbox', {}); %>
	<% } %>
Using include within a switch statement.
<p>Including file named 'choice-list.ejs' via switch</p>

	<% switch ('choiceList') {
		case 'choiceList': %>
				<%- include('fields/choice-list'); %>
				<% break;
		case 'checkBox': %>
				<%- include('fields/checkbox'); %>
				<% break;
	} %>
The included file (bobby.ejs) can then use these params (vars) in the usual way.
<p>Import (include) can pass params, here we pass params 'rec' and 'r'</p>

	<%- include('fields/bobby', {rec: rec, r: rec}); %>


Example Source Code


Here is the example source code.

See "rec.ejs".