dede trim函数如何正确使用及去除空格?

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

Of course! Let's break down the dede:trim tag in DedeCMS. It's a very useful but often misunderstood tag.

dedecms dede trim
(图片来源网络,侵删)

What is dede:trim?

The dede:trim tag is a conditional loop tag. Its primary function is to repeat a block of HTML or other content for a specific number of times, but only if a certain condition (usually related to the number of items retrieved) is met.

Think of it as a more specialized version of the dede:loop tag. While dede:loop simply repeats a block of code a set number of times, dede:trim repeats it only if the source data (like an array from a SQL query) contains enough items.

Syntax

The syntax for dede:trim is as follows:

{dede:trim}
    <!-- Content to be repeated -->
{/dede:trim}

It doesn't use any parameters like row or col directly in the opening tag. Instead, its behavior is controlled by two important attributes:

dedecms dede trim
(图片来源网络,侵删)
  1. source: (Required) This is the most critical attribute. It specifies the source of the data that dede:trim will use to determine how many times to loop. This is typically a PHP array variable.
  2. offset: (Optional) Specifies the number of items to skip from the beginning of the source array before starting the loop. The default is 0.

How It Works: The Core Logic

The dede:trim tag works by iterating over the source array you provide. For each item in that array, it executes the code block between {dede:trim} and {/dede:trim}.

Example: If your source array contains 5 items, the content inside the dede:trim block will be rendered 5 times.

This makes it extremely powerful when combined with other DedeCMS tags that return arrays, such as:

  • {dede:arclist} (using the array attribute)
  • {dede:sql} (the query result is an array)
  • Custom PHP code that assigns an array to a template variable

Practical Examples

Let's look at common scenarios where dede:trim is the perfect tool.

dedecms dede trim
(图片来源网络,侵删)

Example 1: Using with {dede:sql} (Most Common Use Case)

Imagine you want to display a list of the 5 most recent articles, but you want to wrap each one in a specific <div> class only if there are actually 5 articles to display.

Goal: Create a horizontal list of 5 recent articles. If there are fewer than 5, don't show the list at all.

Template Code:

<h3>Our Latest 5 Articles</h3>
{dede:sql sql="SELECT id, title, arcurl FROM dede_archives ORDER BY id DESC LIMIT 5"}
    {dede:trim}
        <div class="latest-item">
            <a href="[field:arcurl/]">[field:title/]</a>
        </div>
    {/dede:trim}
{/dede:sql}

Explanation:

  1. {dede:sql ...} executes the SQL query and gets the 5 newest articles. The result is stored as an array in memory.
  2. {dede:trim source='...'}: Here, we don't even need to specify the source attribute explicitly in this simple case because dede:trim will automatically use the array generated by the parent dede:sql tag.
  3. The content inside {dede:trim} is repeated for each of the 5 articles fetched by the query.
  4. If the query returned 0 articles (e.g., your database is empty), the dede:trim block would not be rendered at all, and you would see nothing between the <h3> and </dede:sql> tags.

Example 2: Using with {dede:arclist}

You can achieve a similar result with arclist by using its array attribute to pass the results to another tag.

Goal: Get 3 specific articles by their IDs and display them in a special box.

Template Code:

<h3>Featured Articles</h3>
{dede:arclist idlist='10,25,33' titlelen='50'}
    {dede:trim}
        <div class="featured-box">
            <h4><a href="[field:arcurl/]">[field:title/]</a></h4>
            <p>[field:description/]</p>
        </div>
    {/dede:trim}
{/dede:arclist}

Explanation:

  1. {dede:arclist idlist='...'} fetches the articles with IDs 10, 25, and 33.
  2. The {dede:trim} tag then loops through this resulting array of 3 items.
  3. The HTML for the .featured-box is rendered 3 times, once for each article.

Example 3: Using offset

The offset attribute is useful for pagination or skipping items.

Goal: Display articles 4, 5, and 6 from a list of 10.

Template Code:

{dede:sql sql="SELECT id, title FROM dede_archives ORDER BY id DESC LIMIT 10"}
    {dede:trim offset='3'}
        <p>[field:title/]</p>
    {/dede:trim}
{/dede:sql}

Explanation:

  1. The SQL query gets the top 10 articles.
  2. offset='3' tells dede:trim to skip the first 3 items in the array.
  3. The loop will then run for the remaining 7 items (items 4 through 10), rendering the <p> tag for each one.

dede:trim vs. dede:loop

This is a crucial distinction for DedeCMS developers.

Feature dede:trim dede:loop
Purpose Conditional Looping. Loops based on the number of items in a data source (like an array). Fixed Repetition. Loops a specific number of times, regardless of any external data.
Data Source Requires a source (usually an array from sql, arclist, etc.). Does not use a data source. It loops a fixed block of code.
Common Use Displaying a list of items from a database query. Creating simple, repeating layouts for design purposes (e.g., a 3-column grid of empty boxes).
Example {dede:sql}...{dede:trim}...{/dede:trim}{/dede:sql} {dede:loop row='3' col='1'}...{/dede:loop}

When to use which?

  • Use dede:trim when your loop's repetition count depends on dynamic data from your database or another tag.
  • Use dede:loop when you just need to repeat a piece of HTML a specific number of times for layout, and the content inside is static or manually filled.

Summary

The dede:trim tag is a powerful and flexible looping mechanism in DedeCMS. Its key takeaway is that it iterates over an array. It's the go-to choice for creating dynamic lists from SQL queries or arclist results, allowing you to cleanly separate your data retrieval from your presentation logic.

-- 展开阅读全文 --
头像
Linux C中access函数如何正确使用?
« 上一篇 今天
dede field title是什么?如何使用?
下一篇 » 今天

相关文章

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

目录[+]