title: Using interpolation within Node.js EJS includes
body: <p>My Express app is using EJS, and my views directory looks like this:</p>
  
  <pre><code>./views
    ./contents
      home.ejs
    ./includes
      header.ejs
      footer.ejs
    layout.ejs
  </code></pre>
  
  <p>I'm trying to load home.ejs in my layout.ejs view conditionally based on a local variable named <code>contents</code> in my routes/index.js. That file looks like this:</p>
  
  <pre><code>/*
   * GET home page.
   */
  
  exports.index = function(req, res){
    res.render('index', { title: 'Home', contents: 'home.ejs' });
  };
  </code></pre>
  
  <p>Ideally I could simply write (in layout.ejs):</p>
  
  <pre><code>&lt;% include '/contents' + contents %&gt;
  </code></pre>
  
  <p>where the trailing "contents" is the local variable which contains the relative path to the body text to load.</p>
  
  <p>But alas, it appears EJS always interprets the text following an <code>include</code> directive literally, and there is no chance for any interpolation magic to happen.</p>
  
  <p>I've also tried to no avail:</p>
  
  <pre><code>&lt;% function yieldContent(contents){ %&gt;
    &lt;% var contentPath = 'contents/' + contents; %&gt;
    &lt;% include contentPath %&gt;
  &lt;% }; %&gt;
  &lt;% loadContent(); %&gt;
  </code></pre>
  
  <p></p>
  
  <p>Does anyone have a creative solution for conditionally including a view based on a variable passed in routes?</p>