dede:feedback标签如何正确调用与使用?

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

Of course! dede:feedback is a core and very useful tag in the DedeCMS (织梦CMS) template engine. It's used to retrieve and display user feedback, which typically includes comments, guestbook entries, or other user-submitted messages.

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

Here's a detailed breakdown of what it is, its syntax, attributes, and practical examples.


What is dede:feedback?

The dede:feedback tag is a loop tag that queries the dede_feedback table in your DedeCMS database. It fetches a list of feedback entries and allows you to display them within your website's templates, such as on article pages, guestbooks, or dedicated feedback pages.


Basic Syntax

The dede:feedback tag always works as a loop. You place it within a {dede:feedback}...{/dede:feedback} block.

{dede:feedback}
    <!-- This is the loop item. It repeats for each feedback entry. -->
    <li>
        [field:username function="(@me=='guest' ? '游客' : @me)"/]
        说:<br />
        [field:msg/]
        <small>发表于:[field:pubdate function="MyDate('Y-m-d H:i',@me)"/]</small>
    </li>
{/dede:feedback}

Attributes (Parameters)

You can control the behavior of the dede:feedback tag using several attributes placed inside the opening tag.

dede:feedback
(图片来源网络,侵删)
Attribute Value Description Example
row number The number of feedback entries to display. {dede:feedback row='10'}
infolen number The maximum length of the feedback content (msg). (An alias for titlelen) {dede:feedback infolen='100'}
typeid number Filter feedback by a specific channel (arctype) ID. {dede:feedback typeid='1'}
aid number Filter feedback by a specific article ID. This is very common on article detail pages. {dede:feedback aid='123'}
type commend / good / bad Filter feedback by its type. commend is the most common for regular comments. {dede:feedback type='commend'}
ischeck 0 / 1 Filter by whether the feedback has been approved by an admin. 1 = checked/approved. 0 = unchecked/pending. {dede:feedback ischeck='1'}
orderby pubdate / id / rand The sorting order of the results. pubdate (default) = by publish date. id = by feedback ID. rand = random order. {dede:feedback orderby='pubdate'}

Available Fields (Variables)

Inside the {dede:feedback}...{/dede:feedback} loop, you can use the following fields to display specific pieces of information for each feedback entry.

Field Name Description Example Usage
[field:id/] The unique ID of the feedback entry. <span>ID: [field:id/]</span>
[field:aid/] The ID of the article associated with this feedback. <a href="/plus/view.php?aid=[field:aid/]">查看原文</a>
[field:typeid/] The ID of the channel (arctype) associated with this feedback.
[field:username/] The username of the person who left the feedback. [field:username/]
[field:msg/] The actual content/text of the feedback. This is the main field. [field:msg/]
[field:ip/] The IP address of the user who submitted the feedback. <small>IP: [field:ip/]</small>
[field:ischeck/] Whether the feedback is approved (1) or not (0).
[field:pubdate/] The publish date of the feedback (Unix timestamp). [field:pubdate function="MyDate('Y-m-d H:i',@me)"/]

Practical Examples

Example 1: Displaying Comments on an Article Page (Most Common Use)

This is the classic use case. You place this code in the article_article.htm template to show the comments for the currently viewed article.

<h3>评论列表 (共 {dede:feedback row='10' ischeck='1'}{dede:field name='total'/}{/dede:feedback} 条)</h3>
<ul class="comment-list">
    {dede:feedback aid='[field:id/]' row='20' ischeck='1' orderby='pubdate'}
    <li class="comment-item">
        <div class="comment-user">
            <strong>
                [field:username function="(@me=='guest' ? '游客' : @me)"/]
            </strong>
            <span class="comment-date">[field:pubdate function="MyDate('Y-m-d H:i',@me)"/]</span>
        </div>
        <div class="comment-content">
            [field:msg/]
        </div>
    </li>
    {/dede:feedback}
</ul>
<!-- If you want to show a "No comments yet" message -->
{dede:feedback aid='[field:id/]' ischeck='1' row='0'}
    <p class="text-muted">暂无评论,快来抢沙发吧!</p>
{/dede:feedback}

Explanation:

  • aid='[field:id/]': This is the key. It tells DedeCMS to get only the feedback for the article being viewed. [field:id/] on the article page refers to the current article's ID.
  • ischeck='1': This ensures you only display comments that have been approved by an administrator.
  • row='20': Limits the display to the 20 most recent comments.
  • orderby='pubdate': Sorts the comments from newest to oldest.

Example 2: A Simple Guestbook Page

For a dedicated guestbook page, you might not filter by aid.

dede:feedback
(图片来源网络,侵删)
<h2>网站留言板</h2>
<!-- The form to submit a new feedback entry is usually on the same page -->
<form action="/plus/feedback.php" method="post">
    <input type="hidden" name="action" value="send" />
    <input type="hidden" name="aid" value="0" /> <!-- aid=0 for guestbook -->
    <p>您的姓名:<input type="text" name="username" /></p>
    <p>留言内容:<textarea name="msg"></textarea></p>
    <p><button type="submit">提交留言</button></p>
</form>
<hr />
<h3>留言列表</h3>
{dede:feedback typeid='0' row='50' ischeck='1' orderby='pubdate'}
    <div class="guestbook-entry">
        <p>
            <strong>[field:username function="(@me=='guest' ? '游客' : @me)"/]</strong> 
            于 [field:pubdate function="MyDate('Y-m-d H:i',@me)"/] 说:
        </p>
        <blockquote>[field:msg/]</blockquote>
    </div>
{/dede:feedback}

Explanation:

  • typeid='0': Often used to represent a general guestbook or non-specific category. This can vary based on your site's setup.
  • The form uses /plus/feedback.php, which is the standard handler for submitting feedback in DedeCMS.

Example 3: Using PHP in Fields

Sometimes you need more control. You can use PHP functions within the fields.

{dede:feedback row='5'}
    <div>
        <!-- If username is 'guest', display 'Anonymous User', otherwise show the username -->
        User: [field:username function="(@me=='guest' ? 'Anonymous User' : @me)"/]<br />
        <!-- Format the date using a custom function -->
        Date: [field:pubdate function="strftime('%Y-%m-%d %H:%M:%S', @me)"/]<br />
        <!-- Automatically add links to URLs in the message -->
        Message: [field:msg function="preg_replace('/(http|https|ftp):\/\/([A-Za-z0-9\._\-]+\/?)\S*/', '<a href=\"\\0\" target=\"_blank\">\\0</a>', @me)"/]
    </div>
{/dede:feedback}

Important Considerations

  1. Approval System: DedeCMS has a built-in approval system for feedback. By default, new comments are not visible on the front-end until an administrator approves them in the DedeCMS backend ([Your_DedeCMS_Folder]/dede/feedback_main.php). Always use ischeck='1' on public-facing pages to avoid displaying unapproved content.
  2. Spam: Be prepared for spam. You might need to install additional spam protection plugins or regularly clean the dede_feedback table.
  3. Security: When displaying user-submitted content like [field:msg/], be mindful of Cross-Site Scripting (XSS) attacks. While DedeCMS has some basic filtering, for high-security sites, you might want to add additional sanitization. The function attribute is a good place to do this, for example, by stripping HTML tags: [field:msg function='htmlspecialchars(@me)'/].
-- 展开阅读全文 --
头像
虚拟空间如何安装织梦系统?
« 上一篇 今天
苹果电脑能安装织梦CMS吗?
下一篇 » 今天

相关文章

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

目录[+]