dede field if 条件判断如何正确使用?

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

Of course! The dede:field tag in DedeCMS (织梦CMS) is a powerful and versatile tag used to display the value of a specific field from the current document or channel. It's one of the most frequently used tags in template development.

dede field if
(图片来源网络,侵删)

Let's break it down in detail.


Basic Syntax

The basic syntax of the dede:field tag is:

{dede:field name='field_name' /}
  • {dede:field ... /}: The opening and closing tags.
  • name='field_name': This is the required attribute. You must specify the name of the field you want to display.

The name Attribute (The Most Important Part)

The name attribute tells DedeCMS which piece of content to pull. The value you use depends on the context of the template (e.g., on an article detail page, on a list page, etc.).

A. On an Article Detail Page (article_article.htm)

On the page that displays a single full article, you can access all the fields of that specific article.

dede field if
(图片来源网络,侵删)

| Field Name | Description | Example | | :--- | :--- | :--- | | The article's title. | {dede:field.title/} | | pubdate | The publication date. | {dede:field.pubdate function='MyDate(@me, 'Y-m-d')'/} | | source | The source of the article. | {dede:field.source/} | | writer | The author of the article. | {dede:field.writer/} | | description | The article's summary/description. | {dede:field.description/} | | body | The full content of the article. | {dede:field.body/} | | click | The number of views/clicks. | {dede:field.click/} | | typeid | The ID of the category the article belongs to. | {dede:field.typeid/} | | arctitle | The title of the article (alternative to title). | {dede:field.arctitle/} | | keywords | The article's keywords. | {dede:field.keywords/} |

Example Usage in article_article.htm:

<h1>{dede:field.title/}</h1>
<div class="info">
    <span>发布时间:{dede:field.pubdate function='MyDate(@me, 'Y-m-d H:i')'/}</span>
    <span>来源:{dede:field.source/}</span>
    <span>作者:{dede:field.writer/}</span>
</div>
<div class="content">
    {dede:field.body/}
</div>

B. On a Category/List Page (list_*.htm)

On a list page (e.g., a category page or a search results page), dede:field is used to get information about the channel or category itself, not the individual articles in the list.

| Field Name | Description | | :--- | :--- | | The title of the current category/channel. | | description | The description of the current category/channel. | | keywords | The keywords of the current category/channel. | | id | The ID of the current category. | | typeid | The ID of the current category (same as id). | | sitepath | The path of the site. |

dede field if
(图片来源网络,侵删)

Example Usage in list_category.htm:

<h1>{dede:field.title/}</h1>
<p>{dede:field.description/}</p>
<!-- The loop to display articles in this category -->
{dede:list pagesize='10'}
    <a href="[field:arcurl/]">[field:title/]</a>
    <p>[field:description function='cn_substr(@me, 100)']...</p>
{/dede:list}

The function Attribute (Advanced Usage)

The function attribute is where dede:field becomes extremely powerful. It allows you to apply PHP functions to the field's value before displaying it.

Syntax: function='function_name(@me)'

  • @me: This is a special variable that represents the original value of the field.

Common function Examples:

Date Formatting

The pubdate field is stored as a Unix timestamp. You need to format it for display.

<!-- Display date as 'Year-Month-Day' -->
{dede:field.pubdate function='MyDate(@me, 'Y-m-d')'/}
<!-- Display date as 'Year-Month-Day Hour:Minute' -->
{dede:field.pubdate function='MyDate(@me, 'Y-m-d H:i')'/}

Trimming Text (e.g., for a summary)

Use cn_substr to cut a string to a specific length.

<!-- Get the description and limit it to 100 characters -->
{dede:field.description function='cn_substr(@me, 100)'/}

Removing HTML Tags

Use htmlspecialchars to display HTML code as plain text, or strip_tags to remove all HTML tags.

<!-- Display the body text without any HTML formatting -->
{dede:field.body function='strip_tags(@me)'/}

Custom PHP Functions

You can create your own PHP functions in /include/common.func.php and then use them.

Example: Let's say you add a function addHello($str) to common.func.php:

function addHello($str) {
    return 'Hello, ' . $str;
}

Then in your template, you can call it:

{dede:field.title function='addHello(@me)'/}
<!-- If the title is "World", this will display "Hello, World" -->

Special Case: dede:field with runphp='yes'

This is an advanced technique that allows you to execute complex PHP code directly within the tag. It's generally less recommended due to potential security risks, but it's very powerful.

Syntax:

{dede:field name='field_name' runphp='yes'}
    // Your PHP code here
    @me = ...; // You must assign the final output to @me
{/dede:field}

Example: Conditionally Display Content

Let's display the author's name only if it's not empty.

{dede:field name='writer' runphp='yes'}
    if (@me != '') {
        @me = '作者:' . @me;
    } else {
        @me = '佚名';
    }
{/dede:field}

Important: dede:field vs. arclist/list Tags

This is a common point of confusion for beginners.

Tag Use Case What it Displays
{dede:field} On a detail page (article_*.htm) or list page (list_*.htm). Displays a single, specific field from the current document/channel.
{dede:list} On a list page (list_*.htm). Loops through all articles in the current category. Inside its loop, you use [field:fieldname] to get fields for each article in the loop.
{dede:arclist} Can be used on any page (homepage, custom page, etc.). Loops through a specified list of articles. Inside its loop, you use [field:fieldname] to get fields for each article in the loop.

Key Difference:

  • On an article detail page, to get the title, you use {dede:field.title/}.
  • On a list page, inside a {dede:list} loop, to get the title of each article, you use [field:title/].

Summary

Feature Description Example
Purpose Display a single field's value from the current context. {dede:field.title/}
Context Detail page (article_*.htm) or List page (list_*.htm).
name Required. The name of the field to display. name='pubdate'
function Optional. Apply a PHP function to the value. function='MyDate(@me, "Y-m-d")'
runphp Optional. Execute custom PHP code. Use with caution. runphp='yes'
Loop Context Do not use inside {dede:list} or {dede:arclist} loops. Use [field:xxx] inside loops.
-- 展开阅读全文 --
头像
织梦更新文档没反应了?咋回事啊?
« 上一篇 今天
c语言写tinyproxy
下一篇 » 今天

相关文章

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

目录[+]