/*
 * Copyright 2014 Jesko Karwasz
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package de.warhuhn.java.web.spark;

import com.lyncode.jtwig.JtwigModelMap;
import com.lyncode.jtwig.JtwigTemplate;
import com.lyncode.jtwig.configuration.JtwigConfiguration;
import com.lyncode.jtwig.exception.ParseException;
import com.lyncode.jtwig.exception.CompileException;
import com.lyncode.jtwig.exception.RenderException;
import com.lyncode.jtwig.resource.ClasspathJtwigResource;
import org.eclipse.jetty.io.RuntimeIOException;
import spark.ModelAndView;
import spark.TemplateEngine;

/**
 * TemplateEngine for Spark based on Jtwig.
 */
public class JtwigTemplateEngine extends TemplateEngine {

    JtwigConfiguration jtwigConfiguration;

    /**
     * Default Constructor
     */
    public JtwigTemplateEngine() {
        this.jtwigConfiguration = new JtwigConfiguration();
    }

    /**
     * Constructor to add your own JtwigConfiguration
     *
     * @param jtwigConfiguration Your customized JtwigConfiguration
     */
    public JtwigTemplateEngine(JtwigConfiguration jtwigConfiguration) {
        this.jtwigConfiguration = jtwigConfiguration;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String render(ModelAndView modelAndView) {
        ClasspathJtwigResource templateResource = new ClasspathJtwigResource("templates/" + modelAndView.getViewName());

        JtwigTemplate template = new JtwigTemplate(templateResource, jtwigConfiguration);

        String result;

        try {
            // TODO: will break if the model is not a HashMap<String, Object>
            result = template.output(new JtwigModelMap().add((java.util.Map<String, Object>) modelAndView.getModel()));
        } catch (ParseException | CompileException | RenderException e) {
            throw new RuntimeIOException(e);
        }

        return result;
    }
}

@@ -1195,24 +1195,81 @@
 gine %7B%0A%0A    
+private String templateRoot = %22templates/%22;%0A%0A    private 
 JtwigConfigu
@@ -1295,16 +1295,16 @@
 ration;%0A
-
 %0A    /**
@@ -1711,32 +1711,495 @@
 uration;%0A    %7D%0A%0A
+    /**%0A     * Constructor to add your own JtwigConfiguration and own templateRoot for the templates.%0A     *%0A     * @param templateRoot       Root path in ClassPath pointing to the templates%0A     * @param jtwigConfiguration Your customized JtwigConfiguration%0A     */%0A    public JtwigTemplateEngine(String templateRoot, JtwigConfiguration jtwigConfiguration) %7B%0A        this.templateRoot = templateRoot;%0A        this.jtwigConfiguration = jtwigConfiguration;%0A    %7D%0A%0A
     /**%0A     * %7B
@@ -2364,25 +2364,29 @@
 esource(
-%22
+this.
 template
 s/%22 + mo
@@ -2377,19 +2377,20 @@
 template
-s/%22
+Root
  + model

Make the template root configurable