写标签
继承了 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]