首先写个tag类:
@IocBean
public class CmsChannelListTag extends GeneralVarTagBinding {
@Inject
private CmsChannelService cmsChannelService;
@Override
public void render() {
String parentId = Strings.sNull(this.getAttributeValue("parentId"));
String parentName = Strings.sNull(this.getAttributeValue("parentName"));
List<Cms_channel> list = new ArrayList<>();
if (Strings.isNotBlank(parentName)) {
Cms_channel channel = cmsChannelService.fetch(Cnd.where("name", "=", parentName));
if (channel != null)
list = cmsChannelService.query(Cnd.where("parentId", "=", channel.getId()).and("disabled", "=", false).asc("location"));
} else {
list = cmsChannelService.query(Cnd.where("parentId", "=", parentId).and("disabled", "=", false).asc("location"));
}
for (Cms_channel channel : list) {
this.binds(channel);
this.doBodyRender();
}
}
}
然后启动的时候注册tag:
for (ViewMaker vm : config.getViewMakers()) {
if (vm instanceof BeetlViewMaker) {
((BeetlViewMaker)vm).groupTemplate.registerTagFactory("cms_channel_list", ()->ioc.get(CmsChannelListTag.class));
}
}
再然后页面代码:
<#cms_channel_list parentId="" parentName="新手上路" var="o">
${o.name!}
</#cms_channel_list>