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

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:

source: (Required) This is the most critical attribute. It specifies the source of the data thatdede:trimwill use to determine how many times to loop. This is typically a PHP array variable.offset: (Optional) Specifies the number of items to skip from the beginning of thesourcearray before starting the loop. The default is0.
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 thearrayattribute){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.

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:
{dede:sql ...}executes the SQL query and gets the 5 newest articles. The result is stored as an array in memory.{dede:trim source='...'}: Here, we don't even need to specify thesourceattribute explicitly in this simple case becausedede:trimwill automatically use the array generated by the parentdede:sqltag.- The content inside
{dede:trim}is repeated for each of the 5 articles fetched by the query. - If the query returned 0 articles (e.g., your database is empty), the
dede:trimblock 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:
{dede:arclist idlist='...'}fetches the articles with IDs 10, 25, and 33.- The
{dede:trim}tag then loops through this resulting array of 3 items. - The HTML for the
.featured-boxis 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:
- The SQL query gets the top 10 articles.
offset='3'tellsdede:trimto skip the first 3 items in the array.- 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:trimwhen your loop's repetition count depends on dynamic data from your database or another tag. - Use
dede:loopwhen 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.
