Login
Artifact [211bdf0b35]
Login

Artifact 211bdf0b3597d81e67c00f5236e2c538e5e356d0:


/**
   The tmpl module (as distinct from the tmpl plugin!) provides
   utilities for working with s2.tmpl().

*/
return {
    /**
       "Processes" a tmpl() template as follows...

       The first argument is a tmpl()-compiled script (of type Buffer)
       or a non-compiled script of type String (which this function
       compiles it and ignores the 3rd parameter).

       The second parameter is an optional container holding key/value
       pairs which get imported into the current scope before
       evaluating the script. This allows one to easily create
       mini-templates for use in loops and such. If you _have_
       to pass a value but don't have an object, any falsy value
       will do.

       The final argument is only used in error reporting,
       and is stored in any exception propagated via evaluating
       a template. It is ignored when the first argument
       has a typename of 'string'.
    */
    process: proc(template, opt, tmplUncompiled){
        if('string'===typename template){
            tmplUncompiled = template;
            template = this.compile(template);
        }
        affirm 'buffer' === typename template;
        const ex = catch opt
            ? doIt.importSymbols(opt)()
            : eval -> template;
        ex && throw {
            message: "Error evaluating compiled template (location info is likely to be completely wrong).",
            exception: ex,
            template: {
                compiled: template.toString(),
                uncompiled: tmplUncompiled ? tmplUncompiled.toString() : undefined
            }
        };
    }.importSymbols({
        doIt:proc(){
            return eval -> template;
        }
    }),

    processFile: proc(fn, opt){
        return this.process( this.load(fn, true), opt );
    },

    /**
       A proxy for s2.tmpl().
    */
    compile: s2.tmpl,
    /**
       Uses the tmpl! plugin to load the given file
       and optionally compile it using this.compile()
    */
    load: proc(fn, compile){
        return requireS2([(compile ? 'tmpl-compiled!' : 'tmpl!')+fn]).0;
    }.importSymbols(nameof requireS2);
};