JPress社区

在自定义页面中使用模板标签

...

fangdengfu

如何在page页面中使用自定义标签? 

 

先在 

io.jpress.module.page.controller.PageController.index()

中加入以下代码(一定要在page对象塞到controller上下文之前):

        // 取出context
        Enumeration<String> attrNames = getAttrNames();
        Map<String, Object> data = new HashMap<>();
        while (attrNames.hasMoreElements()) {
            String key = attrNames.nextElement();
            Object attr = getAttr(key);
            data.put(key, attr);
        }

        // 根据数据库中取出的模板进行渲染成string,然后再塞回来
        String newContent = RenderManager.me().getEngine().getTemplateByString(page.getContent()).renderToString(data);
        newContent = newContent.replaceAll("&lt;", "<")
                .replaceAll("&gt;", ">")
                .replaceAll("&quot;", "\"")
                .replace("<pre>", "")
                .replace("</pre>", "");
        page.setContent(newContent);

 

启动项目。。。。。

 

新建页面:

在页面内容中使用模板标签:

#articlePage()
<div class="newsitem padding-big-top">
    <ul>
        #for(article : articlePage.list)
        <li class="clearfix">
            <a href="#(article.url)" title="#(article.title ??)">
                <div class="news-date">
                    <div class="news-date1">#date(article.created, "dd")</div>
                    <div class="news-date2">#date(article.created, "yyyy/MM")</div>
                </div>
                <div class="news-bodys">
                    <h3>#(article.title ??)</h3>
                    <p>​#maxLength(article.text,100)</p>
                </div>
            </a>
        </li>
        #end
    </ul>
</div>

<div class="pagess">
    <div class="layout text-center"><a class="button border-main" href="/">更多>></a> </div>
</div>
#end

 

发布。。。。。

 

查看页面测试结果:

纯粹混脸熟,各位亲喷。。