freemarker-template-inheritance

Freemarker template inheritance.

License

License

GroupId

GroupId

kr.pe.kwonnam.freemarker
ArtifactId

ArtifactId

freemarker-template-inheritance
Last Version

Last Version

0.4.RELEASE
Release Date

Release Date

Type

Type

jar
Description

Description

freemarker-template-inheritance
Freemarker template inheritance.
Project URL

Project URL

https://github.com/kwon37xi/freemarker-template-inheritance
Source Code Management

Source Code Management

https://github.com/kwon37xi/freemarker-template-inheritance

Download freemarker-template-inheritance

How to add to project

<!-- https://jarcasting.com/artifacts/kr.pe.kwonnam.freemarker/freemarker-template-inheritance/ -->
<dependency>
    <groupId>kr.pe.kwonnam.freemarker</groupId>
    <artifactId>freemarker-template-inheritance</artifactId>
    <version>0.4.RELEASE</version>
</dependency>
// https://jarcasting.com/artifacts/kr.pe.kwonnam.freemarker/freemarker-template-inheritance/
implementation 'kr.pe.kwonnam.freemarker:freemarker-template-inheritance:0.4.RELEASE'
// https://jarcasting.com/artifacts/kr.pe.kwonnam.freemarker/freemarker-template-inheritance/
implementation ("kr.pe.kwonnam.freemarker:freemarker-template-inheritance:0.4.RELEASE")
'kr.pe.kwonnam.freemarker:freemarker-template-inheritance:jar:0.4.RELEASE'
<dependency org="kr.pe.kwonnam.freemarker" name="freemarker-template-inheritance" rev="0.4.RELEASE">
  <artifact name="freemarker-template-inheritance" type="jar" />
</dependency>
@Grapes(
@Grab(group='kr.pe.kwonnam.freemarker', module='freemarker-template-inheritance', version='0.4.RELEASE')
)
libraryDependencies += "kr.pe.kwonnam.freemarker" % "freemarker-template-inheritance" % "0.4.RELEASE"
[kr.pe.kwonnam.freemarker/freemarker-template-inheritance "0.4.RELEASE"]

Dependencies

compile (1)

Group / Artifact Type Version
org.freemarker : freemarker jar 2.3.19

test (1)

Group / Artifact Type Version
junit : junit jar 4.11

Project Modules

There are no modules declared in this project.

Freemarker Template Inheritance

Maven Central

With these template inheritance directives you can manage template layouts..

You don't need to use layout frameworks like Sitemesh or Apache Tiles.

If you use JSP, refer to jsp-template-inheritance.

From 0.4.RELEASE, you can downlod this library from the maven central repository.

Gradle dependencies

dependencies {
    compile 'kr.pe.kwonnam.freemarker:freemarker-template-inheritance:0.4.RELEASE'
}

Maven dependencies

<dependency>
    <groupId>kr.pe.kwonnam.freemarker</groupId>
    <artifactId>freemarker-template-inheritance</artifactId>
    <version>0.4.RELEASE</version>
    <scope>compile</scope>
</dependency>

Spring Framework Settings

Java Config - refer to FreemarkerTemplateinheritanceConfig.java

@Bean
public Map<String, TemplateModel> freemarkerLayoutDirectives() {
    Map<String, TemplateModel> freemarkerLayoutDirectives = new HashMap<String, TemplateModel>();
    freemarkerLayoutDirectives.put("extends", new ExtendsDirective());
    freemarkerLayoutDirectives.put("block", new BlockDirective());
    freemarkerLayoutDirectives.put("put", new PutDirective());

    return freemarkerLayoutDirectives;
}

@Bean
public FreeMarkerConfigurer freemarkerConfig() {
    FreeMarkerConfigurer freemarkerConfig = new FreeMarkerConfigurer();
    freemarkerConfig.setTemplateLoaderPath("/WEB-INF/ftls/");
    freemarkerConfig.setDefaultEncoding("UTF-8");

    Map<String, Object> freemarkerVariables = new HashMap<String, Object>();
    freemarkerVariables.put("layout", freemarkerLayoutDirectives());

    freemarkerConfig.setFreemarkerVariables(freemarkerVariables);
    return freemarkerConfig;
}

@Bean
public ViewResolver viewResolver() {
    FreeMarkerViewResolver viewResolver = new FreeMarkerViewResolver();
    viewResolver.setCache(false);
    viewResolver.setPrefix("");
    viewResolver.setSuffix(".ftl");
    viewResolver.setContentType("text/html; charset=utf-8");
    return viewResolver;
}

XML Config - refer to spring-servlet.xml

<import resource="classpath:/kr/pe/kwonnam/freemarker/inheritance/freemarker-layout-directives.xml" />

<bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
    <property name="templateLoaderPath" value="/WEB-INF/ftls/"/>
    <property name="defaultEncoding" value="utf-8" />
    <property name="freemarkerVariables">
        <map>
            <entry key="layout" value-ref="freemarkerLayoutDirectives" />
        </map>
    </property>
</bean>

Usage

base.ftl : layout

<!DOCTYPE html>
<html>
    <head>
        <title>Base Layout</title>
        <@layout.block name="head">
            <script type="text/javascript" src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
        </@layout.block>
    </head>
    <body>
        <@layout.block name="header">
            <h1>Base Layout</h1>
        </@layout.block>
        <div class="base">
            <@layout.block name="contents">
                <h2>Contents will be here</h2>
            </@layout.block>
        </div>
        <@layout.block name="footer">
            <div>Footer base</div>
        </@layout.block>
    </body>
</html>

view.ftl : contents

<@layout.extends name="layouts/base.ftl">
    <@layout.put block="head">
        <script src="//ajax.googleapis.com/ajax/libs/mootools/1.4.5/mootools-yui-compressed.js"></script>
    </@layout.put>
    <@layout.put block="header" type="prepend">
        <h2>Index Page</h2>
    </@layout.put>
    <@layout.put block="contents">
        <p>blah.. blah..</p>
    </@layout.put>
    <@layout.put block="footer" type="replace">
        <hr/>
        <div class="footer">Footer replaced by index</div>
    </@layout.put>
</@layout.extends>

PutType

  1. APPEND : The put contents will be appended after block's contents. default.
  2. PREPEND : The put contents will be prepended before block's contents.
  3. REPLACE : The put contents will replace block's contents. The block's contents will be removed.

Example

example module is a web application layout example. Run the module and browse http://localhost:8080/index. Refer to index.ftl and /layout/base.ftl

Known Problems

  1. In <@layout.extends name="">.., name should be absolute path.
  2. The content which wraps <@layout.extends ..> is shown. So you must not put any content before/after <@layout.extends ..>.

References

  1. Jade template inheritance.
  2. Fwd: template inheritance for freemarker

Versions

Version
0.4.RELEASE