在网站运营中,用户生成内容(UGC)是提升互动和活跃度的重要环节,无论是文章下方的评论,还是网站整体的留言板,都能有效地促进用户交流和反馈收集。安企CMS(AnQiCMS)在设计之初就充分考虑了用户互动的重要性,并提供了强大且灵活的功能来管理和展示这些用户提交的内容。
要在安企CMS中显示用户提交的评论或留言,关键在于理解其模板机制和对应的内置标签。安企CMS采用类似Django模板引擎的语法,通过特定的标签来调用后台数据,并在前端模板中渲染呈现。
巧妙利用模板文件和标签
安企CMS为评论列表和留言板分别设置了特定的模板文件和标签,使得内容的展示变得直观。
通常,评论列表的展示会建议在 comment/list.html 或 comment_list.html 模板中完成,而留言板则推荐使用 guestbook/index.html 或 guestbook.html。这些预设的模板路径让内容组织更加规范。
在评论列表显示用户提交的内容
想要在文章或产品详情页下方呈现用户评论,安企CMS提供了一个简洁高效的模板标签:commentList。
调用评论数据:
commentList标签是核心。在你的评论模板文件(例如comment/list.html)中,可以使用它来获取与特定内容相关的评论数据。这个标签通常需要一个archiveId参数,用于指定这些评论是属于哪篇文章或产品的。例如:{% commentList comments with archiveId=archive.Id type="page" limit="10" %} {# 遍历评论列表 #} {% for item in comments %} {# 显示单条评论内容 #} {% endfor %} {% endcommentList %}这里的
archive.Id会自动获取当前页面的文章或产品ID。type="page"表示启用分页功能,limit="10"则限制每页显示10条评论。展示评论详情: 在
{% for item in comments %}循环内部,你可以访问每条评论的详细信息,例如:{{item.UserName}}:评论用户的昵称。{{item.Content}}:评论的具体内容。{{stampToDate(item.CreatedTime, "2006-01-02 15:04")}}:评论的提交时间,使用stampToDate标签进行格式化。{{item.Status}}:评论的审核状态。这是一个非常重要的字段,通常我们会根据它来决定是否在前端显示这条评论。例如,只显示Status为1(已审核通过)的评论。{{item.Parent}}:如果这是一条回复评论,此字段会包含被回复评论的完整信息,这对于构建多级回复的评论区非常有用。
一个基本的评论展示结构可能看起来像这样:
{% for item in comments %} {% if item.Status == 1 %} {# 只显示已审核的评论 #} <div class="comment-item"> <div class="comment-header"> <strong>{{item.UserName}}</strong> {% if item.Parent %} <span>回复</span> <strong>{{item.Parent.UserName}}</strong> {% endif %} <span class="comment-time">{{stampToDate(item.CreatedTime, "2006-01-02 15:04")}}</span> </div> <div class="comment-content"> {% if item.Parent %} <blockquote>{{item.Parent.Content|truncatechars:100}}</blockquote> {# 截取父评论内容 #} {% endif %} <p>{{item.Content}}</p> </div> {# 这里可以添加回复按钮、点赞功能等交互元素 #} </div> {% else %} <div class="comment-item pending"> <p>您的评论正在审核中,请耐心等待。</p> </div> {% endif %} {% endfor %}集成评论分页: 如果评论数量较多,分页功能是必不可少的。在
commentList标签设置为type="page"后,你可以配合pagination标签来生成分页链接。{% pagination pages with show="5" %} <div class="pagination-links"> <a href="{{pages.FirstPage.Link}}">首页</a> {% if pages.PrevPage %}<a href="{{pages.PrevPage.Link}}">上一页</a>{% endif %} {% for pageItem in pages.Pages %} <a class="{% if pageItem.IsCurrent %}active{% endif %}" href="{{pageItem.Link}}">{{pageItem.Name}}</a> {% endfor %} {% if pages.NextPage %}<a href="{{pages.NextPage.Link}}">下一页</a>{% endif %} <a href="{{pages.LastPage.Link}}">尾页</a> </div> {% endpagination %}评论提交表单: 仅仅展示评论是不够的,用户还需要一个提交评论的入口。这通常是一个标准的HTML表单,其
action属性指向/comment/publish接口。表单内需包含archive_id(隐藏字段)、user_name和content等字段。
在留言板显示用户提交的内容
安企CMS的留言板功能同样灵活,它允许用户在后台自定义留言字段,从而收集多样化的信息。
调用留言板字段: 留言板的展示通常在
guestbook/index.html模板中实现。核心标签是guestbook,它会获取你在后台定义的留言字段信息。{% guestbook fields %} <form method="post" action="/guestbook.html"> {# 遍历后台定义的留言字段 #} {% for item in fields %} {# 根据字段类型生成表单元素 #} {% endfor %} <button type="submit">提交留言</button> </form> {% endguestbook %}动态生成表单字段:
guestbook标签返回的fields数组包含了每个自定义字段的详细信息,如item.Name(字段显示名)、item.FieldName(表单提交名)、item.Type(字段类型,如text,textarea,radio等)、item.Required(是否必填)和item.Items(选择类型字段的选项)。你可以根据item.Type来动态生成不同的HTML表单元素:{% for item in fields %} <div> <label>{{item.Name}} {% if item.Required %}<span style="color:red">*</span>{% endif %}</label> <div> {% if item.Type == "text" or item.Type == "number" %} <input type="{{item.Type}}" name="{{item.FieldName}}" placeholder="{{item.Content}}" {% if item.Required %}required{% endif %}> {% elif item.Type == "textarea" %} <textarea name="{{item.FieldName}}" placeholder="{{item.Content}}" rows="5" {% if item.Required %}required{% endif %}></textarea> {% elif item.Type == "radio" %} {% for val in item.Items %} <label><input type="radio" name="{{item.FieldName}}" value="{{val}}" {% if loop.index == 1 %}checked{% endif %}> {{val}}</label> {% endfor %} {# 更多类型如 checkbox, select 类似处理 #} {% endif %} </div> </div> {% endfor %}这种方式能够让你在后台灵活调整留言板字段,而前端无需修改代码。
留言提交: 构建好留言表单后,用户就可以方便地提交他们的留言