Page tree
Skip to end of metadata
Go to start of metadata

Your Rating: Results: 1 Star2 Star3 Star4 Star5 Star 94 rates

This tutorial shows the steps necessary to enable in-place templating in a Blossom project. In-place templating allows you to edit your templates in AdminCentral. It can be very useful when you need to update a template in a live production environment.

Note that this is not possible for JSP templates.

Add the dependency to your module

Add this to your pom.xml:

<dependency>
  <groupId>info.magnolia</groupId>
  <artifactId>magnolia-module-inplace-templating</artifactId>
  <version>1.2.1</version>
</dependency>

And this to your module descriptor (/src/main/resources/META-INF/magnolia/myModule.xml):

<dependency>
  <name>inplace-templating</name>
  <version>1.2.1</version>
</dependency>

This is necessary to ensure that the in-place templating module is started before your module. For more details see the documentation of the module mechanism.

Make sure your templates are on the classpath

The general convention is to keep your freemarker templates on the classpath in a package with the same name as your module.

/src/main/resources/myModule/paragraphs/myparagraph.ftl

The ViewResolvers for this module would look like this:

  <bean class="info.magnolia.module.blossom.view.TemplateViewResolver">
    <property name="prefix" value="/myModule/templates/"/>
    <property name="suffix" value=".ftl"/>
    <property name="viewRenderer">
      <bean class="info.magnolia.module.blossom.view.FreemarkerTemplateViewRenderer"/>
    </property>
  </bean>
  <bean class="info.magnolia.module.blossom.view.ParagraphViewResolver">
    <property name="prefix" value="/myModule/paragraphs/"/>
    <property name="suffix" value=".ftl"/>
    <property name="viewRenderer">
      <bean class="info.magnolia.module.blossom.view.FreemarkerParagraphViewRenderer"/>
    </property>
  </bean>

In this setup a paragraph that uses the myparagraph.ftl template will simple return myparagraph and the ViewResolver will know where it is thanks to the configured prefix and suffix.

@RequestMapping("/myparagraph")
public String render() { return "myparagraph"; }

Install the templates

When your module is installed it needs to install its templates into the templating repository. This is done by adding a task in your VersionHandler.

@Override
protected List<Task> getExtraInstallTasks(InstallContext installContext) {
    ArrayList<Task> tasks = new ArrayList<Task>();
    tasks.add(new TemplatesInstallTask("/myModule/.*\\.ftl", true));
    return tasks;
}

And that's it!

In AdminCentral you now have a new menu option called Templating. It displays all the installed templates and you can edit them as you like. Don't forget to enable the template otherwise the original from the classpath will still be used.

A note on mixing freemarker and JSP templates

When you use both freemarker and JSP's in your project you need to configure a view resolver for each type and return view names from your controllers that clearly maps to only one of them. A common way of doing this is to return viewNames with the extension, and then specify a pattern on the ViewResolver that matches the extension.

The ViewResolver in this example will only be used when the controller returns a view name that ends with .ftl.

  <bean class="info.magnolia.module.blossom.view.TemplateViewResolver">
    <property name="prefix" value="/myModule/templates/"/>
    <property name="viewNames" value="*.ftl"/>
    <property name="viewRenderer">
      <bean class="info.magnolia.module.blossom.view.FreemarkerTemplateViewRenderer"/>
    </property>
  </bean>