Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 /*
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  *
  * Copyright (c) 2013-2014 sagyf Yang. The Four Group.
  */
 package com.github.sog.render.ftl;
 

Freemarker的覆盖自定义标记,用来覆盖模板中的指定区域.

Author(s):
poplar.yfyang
Version:
1.0 2012-04-20 上午7:32
Since:
JDK 1.5
public class OverrideDirective implements TemplateDirectiveModel {
    
覆盖模板的自定义指令名称
    public final static String DIRECTIVE_NAME = "override";
    @SuppressWarnings("rawtypes")
    @Override
    public void execute(Environment envMap paramsTemplateModel[] loopVars,
                        TemplateDirectiveBody bodythrows TemplateExceptionIOException {
        String name = DirectiveUtils.getRequiredParam(params"name");
        String overrideVariableName = DirectiveUtils.getOverrideVariableName(name);
        TemplateDirectiveBodyOverrideWraper override = DirectiveUtils.getOverrideBody(envname);
        TemplateDirectiveBodyOverrideWraper current = new TemplateDirectiveBodyOverrideWraper(bodyenv);
        if (override == null) {
            env.setVariable(overrideVariableNamecurrent);
        } else {
            DirectiveUtils.setTopBodyForParentBody(currentoverride);
        }
    }

    
重写指令的内容渲染包装器
    static class TemplateDirectiveBodyOverrideWraper implements TemplateDirectiveBodyTemplateModel {
        
运行环境
        public final  Environment                         env;
        
当前内容渲染
        private final TemplateDirectiveBody               body;
        
内容渲染包装器
        public        TemplateDirectiveBodyOverrideWraper parentBody;

        
构造一个包装器

Parameters:
body 内容渲染
env 运行环境
                                                   Environment env) {
            super();
            this. = body;
            this. = env;
        }
        @Override
        public void render(Writer outthrows TemplateExceptionIOException {
            if ( == nullreturn;
            try {
                .setVariable(.this);
                .render(out);
            } finally {
                .setVariable(.preOverridy);
            }
        }
    }
New to GrepCode? Check out our FAQ X