Gabriel Tiongco
Gabriel Tiongco

Reputation: 23

Inject data into variables defined in asciidoc template

I've been looking around online for a while now and I can't seem to determine whether this is possible or not so was hoping someone might be able to point me in some direction..

I was wondering specifically whether it was possible to programatically insert data into an asciidoc template so that I can use it to generate something like an invoice document. I've looked at AsciidoctorJ and that seems to do the final step of generating the pdf of the asciidoc file which is fine.

I've looked over the docs for macros in asciidoc but couldn't seem to find exactly what I wanted..

I found an example using latex and a library called Java LaTeX Report which allows you to define a .tex file with things like:

#foreach( $name in $services )
    \Fee{$name[0]} {$name[1]} {1} 
#end

and then populate those variables from data in code and process the template by going:

ArrayList<ArrayList<String>> services = new ArrayList<ArrayList<String>>();

        ArrayList<String> subservice1 = new ArrayList<String>();
        ArrayList<String> subservice2 = new ArrayList<String>();
        ArrayList<String> subservice3 = new ArrayList<String>();

        subservice1.add("Software");
        subservice1.add("50");
        subservice2.add("Hardware1");
        subservice2.add("500");
        subservice3.add("Hardware2");
        subservice3.add("850");

        services.add(subservice1);
        services.add(subservice2);
        services.add(subservice3);      

        converter.replace("services", services);

        converter.parse(template, invoice1);

Generating the text in latex:

\Fee{Software} {50} {1} 
\Fee{Hardware1} {500} {1} 
\Fee{Hardware2} {850} {1} 

Is there some library that will allow me to do something similar for asciidoc? Any help will be greatly appreciated.

Upvotes: 0

Views: 699

Answers (1)

Gabriel Tiongco
Gabriel Tiongco

Reputation: 23

Through what I found online, there is no way to do natively in asciidoc what was shown for the LaTeX alternative. However, you can achieve a similar result using a templating language like FreeMarker to do the variable binding. The output from that would be the asciidoc template with all the data inserted. You can just define the extension of the output file so you can parameterise any file type.

Upvotes: 0

Related Questions