通过 Filter_Plugin_ViewComments_Template 接口,可以在 Z-BlogPHP 前台评论列表(通过 AJAX 加载或分页切换时)渲染前修改模板变量,适用于向评论区注入自定义数据等场景。该接口在整页渲染时通常不触发。
Filter_Plugin_ViewComments_Template
Template $template
下例在评论列表中注入当前文章评论总数,方便主题模板显示:
<?php RegisterPlugin("demoAPP", "ActivePlugin_demoAPP"); function ActivePlugin_demoAPP() { Add_Filter_Plugin('Filter_Plugin_ViewComments_Template', 'demoAPP_ViewComments_Template'); } function demoAPP_ViewComments_Template(&$template) { // 注入自定义变量,主题模板中可用 {$demoapp_comment_total} 读取 $article = $template->GetTags('article'); $template->SetTags('demoapp_comment_total', $article->CommNums); }
$template
Template
SetTags()
GetTags()
ViewComments()
article
comments
commentspagebar
<label id="AjaxCommentBegin"></label>
<label id="AjaxCommentEnd"></label>
评论列表模板变量扩展
通过
Filter_Plugin_ViewComments_Template接口,可以在 Z-BlogPHP 前台评论列表(通过 AJAX 加载或分页切换时)渲染前修改模板变量,适用于向评论区注入自定义数据等场景。该接口在整页渲染时通常不触发。接口信息
Filter_Plugin_ViewComments_TemplateTemplate $template完整案例
下例在评论列表中注入当前文章评论总数,方便主题模板显示:
注意事项
$template为系统的Template对象,通过SetTags()注入变量、通过GetTags()读取已有变量;ViewComments()函数(评论列表 AJAX 请求)中触发,模板变量包含article(文章对象)、comments(评论数组)、commentspagebar(分页条);模板文件默认为comments;<label id="AjaxCommentBegin"></label>与<label id="AjaxCommentEnd"></label>之间的内容返回给前端,因此注入的变量需要在这两个标签范围内被主题模板引用。