All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.jooby.internal.thymeleaf.ThymeleafTemplateEngine Maven / Gradle / Ivy

There is a newer version: 3.6.0
Show newest version
/*
 * Jooby https://jooby.io
 * Apache License Version 2.0 https://jooby.io/LICENSE.txt
 * Copyright 2014 Edgar Espina
 */
package io.jooby.internal.thymeleaf;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import edu.umd.cs.findbugs.annotations.NonNull;
import io.jooby.ModelAndView;

public class ThymeleafTemplateEngine implements io.jooby.TemplateEngine {

  private TemplateEngine templateEngine;
  private List extensions;

  public ThymeleafTemplateEngine(TemplateEngine templateEngine, List extensions) {
    this.templateEngine = templateEngine;
    this.extensions = Collections.unmodifiableList(extensions);
  }

  @NonNull @Override
  public List extensions() {
    return extensions;
  }

  @Override
  public String render(io.jooby.Context ctx, ModelAndView modelAndView) {
    Map model = new HashMap<>(ctx.getAttributes());
    model.putAll(modelAndView.getModel());

    // Locale:
    Locale locale = modelAndView.getLocale();
    if (locale == null) {
      locale = ctx.locale();
    }

    Context context = new Context(locale, model);
    String templateName = modelAndView.getView();
    if (!templateName.startsWith("/")) {
      templateName = "/" + templateName;
    }
    return templateEngine.process(templateName, context);
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy