com.mitchellbosecke.pebble.PebbleEngine Java Examples

The following examples show how to use com.mitchellbosecke.pebble.PebbleEngine. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CMSURLHandler.java    From fenixedu-cms with GNU Lesser General Public License v3.0 7 votes vote down vote up
private void handleLeadingSlash(final HttpServletRequest req, HttpServletResponse res, Site sites) throws PebbleException,
        IOException, ServletException {
    if (req.getMethod().equals("GET")) {
        res.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
        res.setHeader("Location", rewritePageUrl(req));
        return;
    } else if (req.getMethod().equals("POST")) {
        if (CoreConfiguration.getConfiguration().developmentMode()) {
            PebbleEngine engine = new PebbleEngine.Builder().loader(new StringLoader()).extension(new CMSExtensions()).build();
            PebbleTemplate compiledTemplate =
                    engine.getTemplate("<html><head></head><body><h1>POST action with backslash</h1><b>You posting data with a URL with a backslash. Alter the form to post with the same URL without the backslash</body></html>");
            res.setStatus(500);
            res.setContentType("text/html");
            compiledTemplate.evaluate(res.getWriter(), I18N.getLocale());
        } else {
            renderer.errorPage(req, res, sites, 500);
        }
    }
}
 
Example #2
Source File: PebbleTemplateEngine.java    From pippo with Apache License 2.0 6 votes vote down vote up
@Override
public void renderString(String templateContent, Map<String, Object> model, Writer writer) {
    String language = (String) model.get(PippoConstants.REQUEST_PARAMETER_LANG);

    if (StringUtils.isNullOrEmpty(language)) {
        language = getLanguageOrDefault(language);
    }
    Locale locale = (Locale) model.get(PippoConstants.REQUEST_PARAMETER_LOCALE);
    if (locale == null) {
        locale = getLocaleOrDefault(language);
    }

    try {
        PebbleEngine stringEngine = new PebbleEngine.Builder()
            .loader(new StringLoader())
            .strictVariables(engine.isStrictVariables())
            .templateCache(null)
            .build();

        PebbleTemplate template = stringEngine.getTemplate(templateContent);
        template.evaluate(writer, model, locale);
        writer.flush();
    } catch (Exception e) {
        throw new PippoRuntimeException(e);
    }
}
 
Example #3
Source File: PebbleTemplateTest.java    From vertx-web with Apache License 2.0 6 votes vote down vote up
@Test
public void customBuilderShouldRender(TestContext should) {
  final Async test = should.async();
  final TemplateEngine engine = PebbleTemplateEngine.create(vertx, new PebbleEngine.Builder().extension(new TestExtension()).loader(new PebbleVertxLoader(vertx)).build());

  final JsonObject context = new JsonObject()
    .put("foo", "badger")
    .put("bar", "fox")
    .put("context", new JsonObject().put("path", "/test-pebble-template5.peb"));

  engine.render(context, "src/test/filesystemtemplates/test-pebble-template5.peb", render -> {
    should.assertTrue(render.succeeded());
    should.assertEquals("Hello badger and foxString is TESTRequest path is /test-pebble-template5.peb", render.result().toString());
    test.complete();
  });
}
 
Example #4
Source File: TemplateUtils.java    From tablesaw with Apache License 2.0 6 votes vote down vote up
public static PebbleEngine getNewEngine() {
  PebbleEngine engine;
  try {
    Loader<?> loader = new ClasspathLoader();
    if (templateLocations != null && !templateLocations.isEmpty()) {
      List<Loader<?>> loaders = new ArrayList<>();
      for (String templateLocation : templateLocations) {
        FileLoader fileLoader = new FileLoader();
        fileLoader.setPrefix(templateLocation);
        loaders.add(fileLoader);
      }
      // add this one last, so it is shadowed
      loaders.add(loader);
      loader = new DelegatingLoader(loaders);
    }
    engine = new PebbleEngine.Builder().loader(loader).strictVariables(false).build();
  } catch (PebbleException e) {
    throw new IllegalStateException(e);
  }
  return engine;
}
 
Example #5
Source File: TemplateUtils.java    From tablesaw with Apache License 2.0 6 votes vote down vote up
public static PebbleEngine getNewEngine() {
  PebbleEngine engine;
  try {
    Loader<?> loader = new ClasspathLoader();
    if (templateLocations != null && !templateLocations.isEmpty()) {
      List<Loader<?>> loaders = new ArrayList<>();
      for (String templateLocation : templateLocations) {
        FileLoader fileLoader = new FileLoader();
        fileLoader.setPrefix(templateLocation);
        loaders.add(fileLoader);
      }
      // add this one last, so it is shadowed
      loaders.add(loader);
      loader = new DelegatingLoader(loaders);
    }
    engine = new PebbleEngine.Builder().loader(loader).strictVariables(false).build();
  } catch (PebbleException e) {
    throw new IllegalStateException(e);
  }
  return engine;
}
 
Example #6
Source File: PebbleEngineProducerTest.java    From krazo with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldCorrectlySetEscapingStrategy() {
    properties.put(PebbleProperty.ESCAPING_STRATEGY.key(), CustomEscapingStrategy.class.getCanonicalName());

    PebbleEngine pebbleEngine = pebbleEngineProducer.pebbleEngine();
    EscapeFilter ef = (EscapeFilter) pebbleEngine.getExtensionRegistry().getFilter("escape");

    assertEquals(ef.getDefaultStrategy(), "userDefinedEscapingStrategy");
}
 
Example #7
Source File: PebbleTemplateEngine.java    From jbake with MIT License 5 votes vote down vote up
private void initializeTemplateEngine() {
    Loader loader = new FileLoader();
    loader.setPrefix(config.getTemplateFolder().getAbsolutePath());

    /*
     * Turn off the autoescaper because I believe that we can assume all
     * data is safe considering it is all statically generated.
     */
    EscaperExtension escaper = new EscaperExtension();
    escaper.setAutoEscaping(false);

    engine = new PebbleEngine.Builder().loader(loader).extension(escaper).build();
}
 
Example #8
Source File: PebbleTemplateEngine.java    From pippo with Apache License 2.0 5 votes vote down vote up
@Override
public void init(Application application) {
    super.init(application);

    Router router = getRouter();
    PippoSettings pippoSettings = getPippoSettings();

    List<Loader<?>> loaders = new ArrayList<>();
    PippoTemplateLoader templateLoader = new PippoTemplateLoader();

    templateLoader.setCharset(PippoConstants.UTF8);
    templateLoader.setPrefix(getTemplatePathPrefix());
    templateLoader.setSuffix("." + getFileExtension());
    loaders.add(templateLoader);

    PebbleEngine.Builder builder = new PebbleEngine.Builder()
        .loader(new DelegatingLoader(loaders))
        .strictVariables(false)
        .extension(new GlobalVariablesExtension()
            .set("contextPath", router.getContextPath())
            .set("appPath", router.getApplicationPath()))
        .extension(new I18nExtension(application.getMessages()))
        .extension(new FormatTimeExtension())
        .extension(new PrettyTimeExtension())
        .extension(new AngularJSExtension())
        .extension(new WebjarsAtExtension(router))
        .extension(new PublicAtExtension(router))
        .extension(new RouteExtension(application.getRouter()));

    if (pippoSettings.isDev()) {
        // do not cache templates in dev mode
        builder.cacheActive(false);
        builder.extension(new DebugExtension());
    }

    // allow custom initialization
    init(application, builder);

    engine = builder.build();
}
 
Example #9
Source File: PebbleTemplateEngineImpl.java    From vertx-web with Apache License 2.0 5 votes vote down vote up
public PebbleTemplateEngineImpl(Vertx vertx, String extension) {
  this(vertx,
    extension,
    new PebbleEngine.Builder()
      .loader(new PebbleVertxLoader(vertx))
      .extension(new PebbleVertxExtension())
      .cacheActive(false).build());
}
 
Example #10
Source File: PebbleEngineProducerTest.java    From ozark with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldCorrectlySetEscapingStrategy() {
  properties.put(PebbleProperty.ESCAPING_STRATEGY.key(), CustomEscapingStrategy.class.getCanonicalName());

  PebbleEngine pebbleEngine = pebbleEngineProducer.pebbleEngine();
  EscapeFilter ef = (EscapeFilter) pebbleEngine.getExtensionRegistry().getFilter("escape");

  assertEquals(ef.getDefaultStrategy(), "userDefinedEscapingStrategy");
}
 
Example #11
Source File: Pebble.java    From template-benchmark with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Setup
public void setup() throws PebbleException {
    PebbleEngine engine = new PebbleEngine.Builder().autoEscaping(false).build();
    template = engine.getTemplate("templates/stocks.pebble.html");
    this.context = getContext();
}
 
Example #12
Source File: PebbleTemplateEngine.java    From spark-template-engines with Apache License 2.0 4 votes vote down vote up
/**
 * Construct a new template engine using pebble with a default engine.
 */
public PebbleTemplateEngine() {
    this.engine = new PebbleEngine.Builder().build();
}
 
Example #13
Source File: PebbleTemplateEngine.java    From spark-template-engines with Apache License 2.0 4 votes vote down vote up
/**
 * 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();
}
 
Example #14
Source File: Component.java    From tablesaw with Apache License 2.0 4 votes vote down vote up
protected PebbleEngine getEngine() {
  return engine;
}
 
Example #15
Source File: Component.java    From tablesaw with Apache License 2.0 4 votes vote down vote up
protected PebbleEngine getEngine() {
  return engine;
}
 
Example #16
Source File: PebbleTemplateEngineImpl.java    From vertx-web with Apache License 2.0 4 votes vote down vote up
public PebbleTemplateEngineImpl(Vertx vertx, String extension, PebbleEngine engine) {
  super(vertx, extension);
  this.pebbleEngine = engine;
}
 
Example #17
Source File: PebbleEngineProducerTest.java    From ozark with Apache License 2.0 4 votes vote down vote up
@Test
public void shouldCreateAnInstanceOfPebbleEngine() {
  assertTrue(pebbleEngineProducer.pebbleEngine() instanceof PebbleEngine);
}
 
Example #18
Source File: PebbleViewEngine.java    From ozark with Apache License 2.0 4 votes vote down vote up
@Inject
public PebbleViewEngine(@ViewEngineConfig PebbleEngine pebbleEngine) {
  this.pebbleEngine = pebbleEngine;
}
 
Example #19
Source File: PebbleViewEngine.java    From krazo with Apache License 2.0 4 votes vote down vote up
@Inject
public PebbleViewEngine(@ViewEngineConfig PebbleEngine pebbleEngine) {
    this.pebbleEngine = pebbleEngine;
}
 
Example #20
Source File: PebbleTemplateEngine.java    From spark-template-engines with Apache License 2.0 2 votes vote down vote up
/**
 * Construct a new template engine using pebble with a specified engine.
 *
 * @param engine The pebble template engine.
 */
public PebbleTemplateEngine(PebbleEngine engine) {
    this.engine = engine;
}
 
Example #21
Source File: PebbleTemplateEngine.java    From vertx-web with Apache License 2.0 2 votes vote down vote up
/**
 * Create a template engine using a custom Builder, e.g. if
 * you want use custom Filters or Functions.
 *
 * @return the engine
 */
@GenIgnore
static PebbleTemplateEngine create(Vertx vertx, PebbleEngine engine) {
  return new PebbleTemplateEngineImpl(vertx, DEFAULT_TEMPLATE_EXTENSION, engine);
}
 
Example #22
Source File: PebbleTemplateEngine.java    From vertx-web with Apache License 2.0 2 votes vote down vote up
/**
 * Create a template engine using a custom Builder, e.g. if
 * you want use custom Filters or Functions.
 *
 * @return the engine
 */
@GenIgnore
static PebbleTemplateEngine create(Vertx vertx, String extension, PebbleEngine engine) {
  return new PebbleTemplateEngineImpl(vertx, extension, engine);
}
 
Example #23
Source File: PebbleTemplateEngine.java    From pippo with Apache License 2.0 2 votes vote down vote up
/**
 * Override this method if you want to modify the template configuration.
 *
 * @param application
 * @param configurationTarget
 */
protected void init(Application application, PebbleEngine.Builder configurationTarget) {
}