title: functions in ejs
body: <h2>What I'd like to have is something like this:</h2>
  
  <p><strong>app.js</strong> (node process, includes etc excluded for brevity but using ejs as rendering engine):</p>
  
  <pre><code>app.get('/', function(req, res){
  
      var ejsVariables = {
          title : 'ideal ejs function example',
          listData1 : {
              listTitle : 'my list',
              listItems : [
                  { name : 'first item', class : 'foo' },
                  { name : 'second item', class : 'bar' },
                  { name : 'last item', class : 'foo' }                ]
          },
          listData2 : {
              listTitle : 'your list',
              listItems : [
                  { name : 'a widget', class : 'foo' },
                  { name : 'another widget', class : 'baz' }
              ]
          }
      };
  
      res.render('index', ejsVariables);
  });
  </code></pre>
  
  <hr>
  
  <p><strong>index.ejs</strong>:</p>
  
  <pre><code>&lt;html&gt;
      &lt;head&gt;
          &lt;title&gt;&lt;%= title %&gt;&lt;/title&gt;
      &lt;/head&gt;
      &lt;body&gt;
          &lt;h1&gt;&lt;%= title %&gt;&lt;/h1&gt;
  
          &lt;% makeList(listData1) %&gt;
  
          &lt;p&gt;lorem ipsum&lt;/p&gt;
  
          &lt;% makeList(listData1) %&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <hr>
  
  <p><strong>???</strong></p>
  
  <hr>
  
  <p><strong>result</strong>:</p>
  
  <pre><code>&lt;html&gt;
      &lt;head&gt;
          &lt;title&gt;ideal ejs function example&lt;/title&gt;
      &lt;/head&gt;
      &lt;body&gt;
          &lt;h1&gt;ideal ejs function example&lt;/h1&gt;
  
          &lt;ul&gt;
              &lt;li class="foo"&gt;first item&lt;/li&gt;
              &lt;li class="bar"&gt;second item&lt;/li&gt;
              &lt;li class="foo"&gt;another item&lt;/li&gt;
          &lt;/ul&gt;
  
          &lt;p&gt;lorem ipsum&lt;/p&gt;
  
          &lt;ul&gt;
              &lt;li class="foo"&gt;a widget&lt;/li&gt;
              &lt;li class="baz"&gt;another widget&lt;/li&gt;
          &lt;/ul&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <hr>
  
  <h2>Question : what goes in the ??? section and/or should be changed above?</h2>
  
  <hr>
  
  <p>What I've tried so far</p>
  
  <p><h2>Attempt 1</h2>- I really want this to work, it just doesn't</p>
  
  <p><strong>index.ejs</strong></p>
  
  <pre><code>&lt;html&gt;
      &lt;head&gt;
          &lt;title&gt;&lt;%= title %&gt;&lt;/title&gt;
      &lt;/head&gt;
      &lt;body&gt;
          &lt;h1&gt;&lt;%= title %&gt;&lt;/h1&gt;
          &lt;% var makeList; %&gt;
          &lt;!-- this variable is declared here as those declared within the include are not
          accessible from the file from which it is called, I'm guessing some sort of 
          function scope is coming into play here --&gt;
  
          &lt;% include makeList %&gt;
  
          &lt;% makeList(listData1) %&gt;
  
          &lt;p&gt;lorem ipsum&lt;/p&gt;
  
          &lt;% makeList(ListData2) %&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <hr>
  
  <p><strong>makeList.ejs</strong></p>
  
  <pre><code>&lt;% function makeListItem(itemData){ %&gt;
          &lt;li class="&lt;%= itemData.class %&gt;" &gt;&lt;%= itemData.name %&gt;&lt;/li&gt;
  &lt;% } %&gt;
  
  &lt;% makeList = function(data){ %&gt;
      &lt;% if(data){ %&gt;
          &lt;ul&gt;
              &lt;% data.map(makeListItem) %&gt;
          &lt;/ul&gt;
      &lt;% } %&gt;
  &lt;% } %&gt;
  </code></pre>
  
  <p>In this situation both <code>makeListItem</code> and <code>makeList</code> are being called, it just appears that due to scoping or something else, when they come to be called they are unable to actually output to the template.</p>
  
  <hr>
  
  <p><h2>Attempt 2</h2> - this actually works, I just dislike the way I end up using includes instead of some sort of function call.</p>
  
  <p><strong>index.ejs</strong></p>
  
  <pre><code>&lt;html&gt;
      &lt;head&gt;
          &lt;title&gt;&lt;%= title %&gt;&lt;/title&gt;
      &lt;/head&gt;
      &lt;body&gt;
          &lt;h1&gt;&lt;%= title %&gt;&lt;/h1&gt;
  
          &lt;% var data = listData1 %&gt;
          &lt;% include makeList %&gt;
  
          &lt;p&gt;lorem ipsum&lt;/p&gt;
  
          &lt;% var data = listData2 %&gt;
          &lt;% include makeList %&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p><strong>makeList.ejs</strong></p>
  
  <pre><code>&lt;% function makeListItem(itemData){ %&gt;
          &lt;li class="&lt;%= itemData.class %&gt;" &gt;&lt;%= itemData.name %&gt;&lt;/li&gt;
  &lt;% } %&gt;
  
  &lt;% if(data){ %&gt;
      &lt;ul&gt;
          &lt;% data.map(makeListItem) %&gt;
      &lt;/ul&gt;
  &lt;% } %&gt;
  </code></pre>
  
  <hr>
  
  <p><h2>Attempt 3</h2> - this basically involved messing around with <a href="https://github.com/RandomEtc/ejs-locals" rel="noreferrer">ejs-locals</a>, however I found the functionality somewhat lacking. </p>
  
  <p>If I'm going to include another npm module in my application I'd really want it to be a more complete solution, but even then in an ideal world I'd rather do without and write it myself.</p>
  
  <hr>
  
  <p>Apologies for the length of this post, it got out of hand quite quickly. I commend you if you've got this far. </p>
  
  <p>Any input would be greatly appreciated.</p>