perwendel / spark-template-engines

Repository for different Template engine implementations.
Apache License 2.0
134 stars 101 forks source link

Twig: Cannot find symbol #74

Closed mhulse closed 5 years ago

mhulse commented 6 years ago
> Task :compileJava FAILED
/........./JtwigTemplateExample.java:23: error: cannot find symbol
        }, new JtwigTemplateEngine());
               ^
  symbol:   class JtwigTemplateEngine
  location: class JtwigTemplateExample
1 error

My JtwigTemplateExample.java:

package foo.report;

import java.util.HashMap;
import java.util.Map;

import spark.ModelAndView;

import static spark.Spark.get;

public class JtwigTemplateExample {

    public static void main(String[] args) {

        get("/hello", (rq, rs) -> {
            Map<String, Object> model = new HashMap<>();
            model.put("message", "Hello Jtwig!");
            return new ModelAndView(model, "hello.twig"); // hello.twig file is in resources/templates directory
        }, new JtwigTemplateEngine());

    }

}

build.gradle:

plugins {
    id 'java'
    id 'application'
}

sourceCompatibility = 1.8

mainClassName = 'Foo'

repositories {
    mavenCentral()
}

dependencies {
    compile 'com.sparkjava:spark-core:2.7.2'
    compile 'com.sparkjava:spark-template-jtwig:2.7.1'
}

Can anyone help me to get this working? I'm pretty new to java, so be kind. 😄

mhulse commented 5 years ago

This works:

build.gradle:

repositories {
    mavenCentral()
    maven {
        url 'http://dl.bintray.com/jtwig/maven'
    }
}
dependencies {
    ...
    compile 'org.jtwig:jtwig-core:5.87.0.RELEASE'
    ...
}

TemplateUtil.java

// https://github.com/perwendel/spark-template-engines/blob/f4a958f3896151a7d4ee0e1baa22093169acbc54/spark-template-jtwig/src/main/java/spark/template/jtwig/JtwigTemplateEngine.java
import com.foo.baz.plugins.JtwigTemplateEngine;

...

public static String render(String templatePath, Request request, Response response, Map<String, Object> model) {

    model = (model != null) ? model : new HashMap<>();

    ...

    return new JtwigTemplateEngine().render(new ModelAndView(model, templatePath));

}

Main.java

get("forgot-password/", (request, response) -> TemplateUtil.render(request,"foo/forgot-password.twig"));

And that did the trick.