Freemarker组件继承与重写

基于spring boot starter freemarker

        
        
            org.springframework.boot
            spring-boot-starter-freemarker
        

添加依赖

        
        
            com.googlecode.rapid-framework
            rapid-core
            4.0.5
        

添加配置

@Configuration
public class FreemarkerConfiguration {

    @Autowired
    freemarker.template.Configuration configuration;

    @PostConstruct
    public void setSharedVariable(){
        configuration.setSharedVariable("block", new BlockDirective());
        configuration.setSharedVariable("override", new OverrideDirective());
        configuration.setSharedVariable("extends", new ExtendsDirective());
    }

}

使用方法

首先定义一个模板

base.ftl




    <@block name="head">
    
    <#include "head.ftl"> 


    <@block name="body">
    <#include "foot.ftl">


然后另外一个新建一个页面

index.ftl

@override 类似vue的template

include 类似vue的组件引入


<@override name="head">
    Home

<@override name="body">
    
    <#include "../component/layout.ftl">


<@extends name="../component/base.ftl">

这样当访问index页面的时候,自动具备了base.ftl中定义的head和body等内容了,然后重写的就是block的内容

你可能感兴趣的:(Freemarker组件继承与重写)