perwendel / spark-template-engines

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

Pebble with Spark is not working #37

Closed met68 closed 7 years ago

met68 commented 8 years ago

Trying to build a new project using Spark 2.5 with Pebble using Eclipse IDE on a Mac and keep getting the following error:

[qtp606094178-19] ERROR spark.webserver.MatcherFilter - java.lang.IllegalArgumentException: com.mitchellbosecke.pebble.error.LoaderException: Could not find template....

I get this error if I follow the instructions here to the letter or with any other possible template path I try. Have wasted a day with this now and unless somebody can help, I will have to go with an other templating engine after all.

tipsy commented 8 years ago

You get this error if you clone and run the pebble example here?

celestialorb commented 8 years ago

I'm experiencing the same issue. I cannot figure out where to place my templates. All I get is a 500 Internal Server Error. I'm having trouble with logging as well so I don't have more info at the moment.

Additionally, the example provided in the README.md differs from the example file located under src.

I'm using gradle to build my project, could it be that gradle is building the project incorrectly?

I'm giving up. I simply cannot get this to work. Please update the documentation to make the directory structure clear.

tipsy commented 8 years ago

Fixed in #33 ?

met68 commented 8 years ago

Not sure why this was closed. Please reopen this issue. Still not working for me and still clear as mud, where the templates need to go and how they need to be addressed. If I follow the instructions in the readme and example files, I get nowhere.

N0odlez commented 8 years ago

I should be addressing this issue in my next PR.

met68 commented 8 years ago

I am totally new to Gihub, Spark, Java etc. and would therefore REALLY appreciate, if somebody could tell me how I might be able to use Pebble 2.3 in a Spark 2.5 (Maven) project. Where do I need to put these templates so that Pebble picks them up??? Had a working version where I placed them in src/main/resources, but that now no longer works.... Any help would be much appreciated, as I have already wasted so much time with this. But being a big fan of the Twig, I would really like to get this to work somehow. Is anybody actually using Pebble with Spark in the real world? If so, how do you do it?

met68 commented 8 years ago

I am trying to use the Pebble template in a Spark 2.5 Maven project. When I include the Spark Pebble version 2.3 template from the Maven Repo, this still uses Pebble 1.4.4., which is way out-of-date. How can I pull-in the Spark Pebble template using Pebble version 2.2.2 or similar into my project? Sorry, if this is obvious to most of you, but I am new to Java and Maven.

N0odlez commented 8 years ago

@met68 Please be patient. I am working my but off to make sure that all of these template engines are up to date and working as they should. Unfortunately I work full time (40 hours /w) and can only contribute when I can. This will be updated to the most recent version of pebble when pushed.

met68 commented 8 years ago

@AzureusNation No worries. I really appreciate all the effort which you guys are putting in, so we may use this cool software. Thank you!

tipsy commented 7 years ago

Has been fixed.

met68 commented 7 years ago

No idea why this was closed, as if anything, it now seems to be more broken than before. All my apps which were working previously now no longer work with Pebble. This project is a bit of a shambles to be honest.

tipsy commented 7 years ago

@met68 Are you building 2.5 yourself, or still using 2.3? We still haven't released the fixes. Will try to get it done after the holidays.

met68 commented 7 years ago

@tipsy I am trying to include 2.3 in Spark 2.5.4

tipsy commented 7 years ago

In that case, nothing should have changed (it should be the same amount as broken as before) Could you try to build 2.5 locally and see if that works for you? Or you can just use pebble directly (read on).

This project is a bit of a shambles to be honest.

"This project" is just a tiny wrapper for the pebble engine. The template engine wrappers are provided by the community, not the maintainers. If you are unhappy with a wrapper, you can make changes and create a pull-request. This is the wrapper in its current state (2.5):

package spark.template.pebble;

import java.io.IOException;
import java.io.StringWriter;
import java.util.Map;

import spark.ModelAndView;
import spark.TemplateEngine;

import com.mitchellbosecke.pebble.PebbleEngine;
import com.mitchellbosecke.pebble.error.PebbleException;
import com.mitchellbosecke.pebble.loader.Loader;
import com.mitchellbosecke.pebble.template.PebbleTemplate;

/**
 * Template Engine based on Pebble.
 *
 * @author Nikki
 */
public class PebbleTemplateEngine extends TemplateEngine {

    /**
     * The Pebble Engine instance.
     */
    private final PebbleEngine engine;

    /**
     * Construct a new template engine using pebble with a default engine.
     */
    public PebbleTemplateEngine() {
        this.engine = new PebbleEngine.Builder().build();
    }

    /**
     * Construct a new template engine using pebble with an engine using a special loader.
     */
    public PebbleTemplateEngine(Loader loader) {
        this.engine = new PebbleEngine.Builder().loader(loader).build();
    }

    /**
     * Construct a new template engine using pebble with a specified engine.
     *
     * @param engine The pebble template engine.
     */
    public PebbleTemplateEngine(PebbleEngine engine) {
        this.engine = engine;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    @SuppressWarnings("unchecked")
    public String render(ModelAndView modelAndView) {
        Object model = modelAndView.getModel();

        if (model == null || model instanceof Map) {
            try {
                StringWriter writer = new StringWriter();

                PebbleTemplate template = engine.getTemplate(modelAndView.getViewName());
                if (model == null) {
                    template.evaluate(writer);
                } else {
                    template.evaluate(writer, (Map<String, Object>) modelAndView.getModel());
                }

                return writer.toString();
            } catch (PebbleException | IOException e) {
                throw new IllegalArgumentException(e);
            }
        } else {
            throw new IllegalArgumentException("Invalid model, model must be instance of Map.");
        }
    }
}

You can improve it if you want, or just add the pebble engine directly:

<dependency>
    <groupId>com.mitchellbosecke</groupId>
    <artifactId>pebble</artifactId>
    <version>2.2.3</version>
</dependency>

Then copy/paste the parts of the wrapper-code that you need for your project.