NutzCN Logo
精华 分享:beetl模板引擎绑定变量的HTML标签的用法
发布于 89天前 作者 Wizzercn 135 次浏览 复制 上一个帖子 下一个帖子
标签: nutzwk

首先写个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>
1 回复

消灭0回复

添加回复
请先登陆
回到顶部