Question

I'm trying to compile a bunch of handlebars templates into a single compiled file using ant. I have a number of folders that each contain about 4 templates each and I want to compile these all into one file. With folders like:

folder01
  |- templates
       |- f1_01.handlebars
       |- f1_02.handlebars
       |- f1_03.handlebars
       |- f1_04.handlebars
folder02
  |- templates
       |- f2_01.handlebars
       |- f2_02.handlebars
       |- f2_03.handlebars
       |- f2_04.handlebars
build.xml

I essentially want to run the command:

handlebars **/templates/*.handlebars -f compiled-templates.js

I have tried the following but it only seems to include 1 file in the output js file.

<macrodef name="handlebars">
    <attribute name="target"/>
    <sequential>
        <apply executable="${handlebars}" failonerror="false">
            <fileset dir="." includes="**/templates/">
                <include name="*.handlebars"/>
            </fileset>
            <arg value="-f compiled-templates.js"/>
        </apply>
    </sequential>
</macrodef>

Also, strangely, the output file starts with a space character, which I can't seem to get rid of. Any help would be greatly appreciated.

Was it helpful?

Solution 4

I ended up using a <concat> task, to create one file out of all the templates, and running the executable once on that file.

<concat destfile="all.handlebars" append="true">
    <fileset dir="." includes="**/templates/">
        <include name="*.handlebars"/>
    </fileset>
</concat>

OTHER TIPS

After searching a lot on stackoverflow and, more importantly, reading the docs i came up with this solution which works.

<echo level="info" message="Pre Compiling templates" />
<apply parallel="true" failonerror="true" executable="node">
  <arg value="${webclient.dir.build}/node_modules/handlebars/bin/handlebars" />
  <srcfile />
  <fileset dir="${webclient}/app/templates" includes="**/*.handlebars"/>
  <arg line="-f ${webclient}/app/templates/handlebars.templates.js -m -a" />
</apply>

try :

...
<arg line="-f compiled-templates.js"/>
...

instead of :

...
<arg value="-f compiled-templates.js"/>
...

Use <script> task where you can embed Javascript or Groovy code that does the iteration work. It is a good practise to call some shorts scripts as a help for such problems as they are usually more expressive than the tricky XML-loop-condition notations.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top