/**
  Runs tmplish()-compatible input text tmpl through tmplish(),
  importing any symbols defined in obj as local symbols for
  template resolution purposes. If the template throws,
  it ouputs an error description instead.
*/
const processTemplate = proc(tmpl, obj){
    const tmplCompiled = tmplish(tmpl)
    const ce = argv.callee
    const cb = (ce.cb || (ce.cb = proc(){
        //api.ob.push()
        const err = catch {eval tmplCompiled}
        //err || api.ob.flush().pop()
        if(err){
            //api.ob.pop()
            $out "Error evaluating template: " err.codeString() ': ' err.message
            $out '<br/>@ line ' err.line ' col ' err.column // these aren't quite useful

            $out '<br/>The original template:<br/>' {<textarea rows='20' cols='80'>} tmplCompiled {</textarea>}
            $out '<br/>The compiled template:<br/>' {<textarea rows='20' cols='80'>} tmpl {</textarea>}
        }
        
    }))
    cb.importSymbols('tmplish', 'tmpl', 'out', 'tmplCompiled', obj || object{})()
}.importSymbols('out', 'tmplish')

const out = api.io.output
const CGI = api.cgi

const template = slurpFile( Fossil.file.dirPart(__FILE)+'tmplish.tmplish', true )
assert 'buffer' === typename template
processTemplate(template, object{
    symTmplish: {<tt>tmplish()</tt>},
    x: 3, y: 5, tmplishSource: template
})
