Use Templating on JavaScript Files
<p>I'm in the most interesting position where I would like to template my JavaScript files server-side to include and remove certain parts based on an object.  Given an object like so:</p>

<pre><code>var obj = {
    "includeA": true,
    "includeB": false,
    "includeC": true
}
</code></pre>

<p>I would like the ability to include/exclude certain parts of said JS file.  For example:</p>

<pre><code>if (obj.includeA) {
    // This is some code from A
}

if (obj.includeB) {
   // This is some code from B
}
</code></pre>

<p>Would produce either a string or a file that looks like:</p>

<pre><code>// This is some code from A
</code></pre>

<p>I've looked into some basic options, one of the foremost ideas that came to mind was to simply use <code>if</code> statements in JS.  This code however looks rather terrible, and considering there will be upto 1,000 lines of this,  not suitable at all:</p>

<pre><code>var string = ""

if (obj.includeA) {
    string += "This is one line \n"
    string += "This is another line \n"
}
// etc.
</code></pre>

<p>This produces the right output, but the repetitiveness of the <code>string +=</code> makes me detest it.  I then decided to package it up into a function:</p>

<pre><code>var string = ""

function call(line) {
    string += line + "\n"

if (obj.includeA) {
    call("This is one line")
    call"This is another line)
}
// etc.
</code></pre>

<p>But that seems only slightly better.  Is there any sort of templating engine (imagining something similar to Jade), that allows templating of text blocks like this?</p>