{dede:feedback}标签具体实现什么功能?

99ANYc3cd6
预计阅读时长 26 分钟
位置: 首页 DEDE建站 正文

Of course! The {dede:feedback} tag is a core and very useful tag in the DedeCMS (织梦CMS) templating system. It's designed to display user-submitted feedback or comments on your website.

{dede:feedback}
(图片来源网络,侵删)

Here’s a complete guide covering its purpose, usage, parameters, and examples.


What is {dede:feedback}?

The {dede:feedback} tag is a loop tag used to fetch and display records from the dede_feedback table in your DedeCMS database. This table stores all the comments and feedback submitted by your visitors.

Its primary uses are:

  • Displaying recent comments on the homepage or a dedicated comments page.
  • Showing feedback for a specific article, member, or product.
  • Creating a "Recent Comments" sidebar widget.

Basic Syntax

The tag always works as a loop. You place it within a {dede:loop} or {dede:feedback} block, and inside the loop, you use field variables to display the specific data for each comment.

{dede:feedback}
(图片来源网络,侵删)
{dede:feedback}
    <!-- This is the loop container -->
    <!-- Display the commenter's name -->
    [field:username function="htmlspecialchars(@me)"/]
    <!-- Display the comment content -->
    [field:msg function="htmlspecialchars(@me)"/]
    <!-- Display the title of the article the comment was left on -->
    ([field:title/])
    <!-- Display the date of the comment -->
    ([field:time function="MyDate('Y-m-d H:i',@me)"/])
{/dede:feedback}

Available Field Variables (Inside the Loop)

These are the placeholders you use inside the {dede:feedback} loop to display different pieces of information for each comment.

Field Variable Description Common Usage
id The unique ID of the feedback record. -
aid The ID of the article the feedback is for. Often used to link back to the article.
typeid The ID of the category the article belongs to. -
username The name of the user who left the feedback. [field:username/]
msg The actual content of the feedback/comment. [field:msg/]
ip The IP address of the user who submitted the feedback. [field:ip/]
ischeck The check status (0 for unchecked, 1 for checked). -
dtime The Unix timestamp of when the feedback was submitted. [field:time function="MyDate('Y-m-d',@me)"/]
mid The member ID of the user (if they were logged in). -
arctitle The title of the article associated with the feedback. [field:arctitle/]
type The type of feedback (e.g., 'feedback', 'good'). -

Parameters (Attributes)

You can add parameters directly to the opening tag to control which feedback is retrieved and how it's displayed.

Parameter Description Example
row (Required) The number of feedback records to display. {dede:feedback row='5'}
msglen The length (in characters) of the feedback message to display. {dede:feedback msglen='50'}
typeid Show only feedback for articles in a specific category ID. {dede:feedback typeid='2'}
aid Show only feedback for a specific article ID. {dede:feedback aid='123'}
good Show only "good" (praise) feedback. Set to yes. {dede:feedback good='yes'}
hot Show only "hot" feedback. {dede:feedback hot='yes'}
orderby Sort the feedback results. Common values: dtime (by time), id (by ID). {dede:feedback orderby='dtime'}

Practical Examples

Example 1: Simple Recent Comments Widget

This is a very common use case for a website's sidebar. It shows the 5 most recent comments, with the commenter's name, a snippet of their message, and the article title.

<h3>最新评论</h3>
<ul class="recent-comments">
    {dede:feedback row='5' msglen='40' titlelen='30'}
        <li>
            <span class="user">[field:username/]</span>:
            <span class="msg">[field:msg/]</span>
            <br>
            <span class="article">评论文章: <a href="[field:arcurl/]">[field:title/]</a></span>
        </li>
    {/dede:feedback}
</ul>

Note: [field:arcurl/] is a useful DedeCMS variable that automatically generates the URL to the article.

{dede:feedback}
(图片来源网络,侵删)

Example 2: Comments for a Specific Article

To display comments only on the article's detail page, you can use the aid parameter. The {dede:field.id/} variable is perfect for this, as it holds the current article's ID.

<h3>本文评论 (共 {dede:feedback row='0' aid='[field:id/]'}{/dede:feedback} 条)</h3>
<ul class="article-comments">
    {dede:feedback row='10' msglen='100' aid='[field:id/]'}
        <li>
            <strong>[field:username/]</strong> 说:
            <p>[field:msg/]</p>
            <small>[field:time function="MyDate('Y-m-d H:i:s',@me)"/]</small>
        </li>
    {/dede:feedback}
</ul>

Example 3: Advanced Styling with PHP

Sometimes you need more control, like stripping HTML tags from the message or adding a default name for anonymous users. You can achieve this using the function parameter.

{dede:feedback row='3' msglen='60' titlelen='20'}
    <div class="feedback-item">
        <div class="feedback-header">
            <span class="feedback-user">
                <!-- If username is empty, display '游客' (Guest), otherwise display the username -->
                [field:username function="(@me == '' ? '游客' : @me)"/]
            </span>
            <span class="feedback-time">([field:time function="MyDate('m-d H:i',@me)"/])</span>
        </div>
        <div class="feedback-content">
            <!-- Remove any HTML tags from the message for security and display -->
            [field:msg function="htmlspecialchars(strip_tags(@me))"/]
        </div>
        <div class="feedback-article">
            来自文章: <a href="[field:arcurl/]">[field:title/]</a>
        </div>
    </div>
{/dede:feedback}

Important Considerations

  • Security: Always use htmlspecialchars() on user-submitted data like [field:msg/] and [field:username/] to prevent Cross-Site Scripting (XSS) attacks. The function="htmlspecialchars(@me)" is a safe practice.
  • Feedback Management: User-submitted feedback is not automatically published. It goes into the "waiting for check" queue. You must approve it in the DedeCMS backend (Comments -> Feedback Management) for it to appear on the front end.
  • Performance: If you have thousands of comments, be careful with using the tag without a typeid or aid filter on a high-traffic page, as it can put a strain on your database. Limiting with row is essential.
-- 展开阅读全文 --
头像
dede addoninfos
« 上一篇 01-09
织梦CMS后台登录失败怎么办?
下一篇 » 01-09

相关文章

取消
微信二维码
支付宝二维码

目录[+]