如何自定义标签

写标签

继承了 TemplateDirectiveModel 接口 接口如图
他是freemarker.templete下的
在这里插入图片描述

一共有这么几个参数

Environment environment, Map map, TemplateModel[] templateModels, TemplateDirectiveBody templateDirectiveBody

这样前台就可以获取到 tag_list 里面的值就是后面的

environment.setVariable("tag_list", getModel(channelList(siteId, count,parentId,channelId)));

由于这个方法里面的参数要求后面是TemplateModel
在这里插入图片描述
所以 下面有这两个方法 返回TemplateModel类型

private DefaultObjectWrapper getBuilder() {
        return new DefaultObjectWrapperBuilder(Configuration.VERSION_2_3_25).build();
    }

    private TemplateModel getModel(Object o) throws TemplateModelException {
        return this.getBuilder().wrap(o);
    }

map.get 可以获取到传进来的参数

 Object siteId = map.get(this.SITE_ID);
        Object count = map.get(this.COUNT);
        Object parentId = map.get(this.PARENT_ID);
        Object channelId = map.get(this.CHANNEL_ID);

最后

  templateDirectiveBody.render(environment.getOut());
@Component
public class ChannelDirectiveList implements TemplateDirectiveModel {

    /**
     * 模板名称
     */
    public static final String TPL_NAME = "cms_channel_list";

    public static final String SITE_ID = "siteId";
    public static final String COUNT = "count";
    public static  final String PARENT_ID = "parentId";
    public static final String CHANNEL_ID = "channelId";

    @Autowired
    private IChannelService channelService;

    /**
     * 获取文章列表
     * @param siteId
     * @param count
     * @return
     */
    public Object channelList(Object siteId, Object count,Object parentId,Object channelId){
        Channel channel = new Channel();
        if(channelId!=null){
            Long[] channelIds= FrontUtils.convertUtils(channelId);
            channel.setChannelIds(channelIds);
        }
        if (siteId != null) {
            channel.setId(Integer.valueOf(siteId.toString()));
        }
        if (count != null) {
            channel.setCount( Long.valueOf(count.toString()));
        }
        if(parentId!=null) {
            channel.setParentId(Integer.valueOf(parentId.toString()));
            channel.setChannelIds(null);
        }
        List<Channel> channelList = channelService.getChannel(channel);
        return channelList;
    }

    /**
     *
     * @param environment 运行的环境
     * @param map 方法参数map  方法名和值
     * @param templateModels
     * @param templateDirectiveBody 输出形式
     * @throws TemplateException
     * @throws IOException
     */
    @Override
    public void execute(Environment environment, Map map, TemplateModel[] templateModels, TemplateDirectiveBody templateDirectiveBody) throws TemplateException, IOException {
        Object siteId = map.get(this.SITE_ID);
        Object count = map.get(this.COUNT);
        Object parentId = map.get(this.PARENT_ID);
        Object channelId = map.get(this.CHANNEL_ID);
        environment.setVariable("tag_list", getModel(channelList(siteId, count,parentId,channelId)));

        templateDirectiveBody.render(environment.getOut());
    }

    private DefaultObjectWrapper getBuilder() {
        return new DefaultObjectWrapperBuilder(Configuration.VERSION_2_3_25).build();
    }

    private TemplateModel getModel(Object o) throws TemplateModelException {
        return this.getBuilder().wrap(o);
    }

注册标签

  @Autowired
    private ContentDirective contentDirective;

    @Autowired
    private CommentDirectiveList commentDirectiveList;

    @Autowired
    private ViewDirective viewDirective;

    @PostConstruct
    public void shareVariable() {
        configuration.setSharedVariable(SiteTagDirective.TPL_NAME, siteTagDirective);
        configuration.setSharedVariable(ChannelDirective.TPL_NAME, channelDirective);
        configuration.setSharedVariable(ContentDirectiveList.TPL_NAME, contentDirectiveList);
        configuration.setSharedVariable(ContentDirectiveDetails.TPL_NAME, contentDirectiveDetails);
        configuration.setSharedVariable(ChannelDirectiveList.TPL_NAME, channelDirectiveList);
        configuration.setSharedVariable(ExtLinksDirectiveList.TPL_NAME, extLinksDirectiveList);
        configuration.setSharedVariable(ExtContentDirective.TPL_NAME, extContentDirective);
        configuration.setSharedVariable(HotSearchWordDirectiveList.TPL_NAME, hotSearchWord);
        configuration.setSharedVariable(CommentDirectivePageList.TPL_NAME, commentDirectivePageList);
        configuration.setSharedVariable(ModelDirective.TPL_NAME, modelDirective);
        configuration.setSharedVariable(ExtLinksTypeDirectiveList.TPL_NAME, extLinksTypeDirectiveList);
        configuration.setSharedVariable(ChildrenChannelDirectiveList.TPL_NAME, docChildrenDirectiveList);
        configuration.setSharedVariable(ContentDirective.TPL_NAME, contentDirective);
        configuration.setSharedVariable(CommentDirectiveList.TPL_NAME, commentDirectiveList);
        configuration.setSharedVariable(ViewDirective.TPL_NAME, viewDirective);

    }

使用标签

[@cms_channel_list count='5']
<table>
    [#list tag_list as a]
        <tr>
            <td>id:${a.id!}</td>
            <td>名称:${a.name!}</td>
        </tr>
    [/#list]
</table>
[/@cms_channel_list]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值