dede field global是什么?如何使用?

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

Of course. Let's break down the dede:field global tag in DedeCMS (织梦内容管理系统). This is one of the most fundamental and frequently used tags for theme development.

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

What is dede:field global?

The dede:field global tag is a DedeCMS template tag used to output the value of a specific field from the current document (article, page, etc.) being viewed.

The key here is "global". While dede:field is used on single pages, its counterpart, dede:global, is used to output values from the global configuration of the DedeCMS system (like site name, copyright, etc.). To avoid confusion, remember:

  • dede:field -> Field of the current article (e.g., article title, content, author).
  • dede:global -> Global system setting (e.g., {dede:global.cfg_webname/}).

Basic Syntax

The basic syntax of the dede:field tag is very simple:

{dede:field.字段名/}

You replace 字段名 (field name) with the actual name of the field you want to display from the dede_arctype or dede_archives database table.

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

Commonly Used dede:field Examples

Here are the most common fields you'll use when building a single article template (usually article_article.htm).

Article Title (title)

This is the most common use case. It displays the title of the article.

<h1>{dede:field.title/}</h1>

Article Content (body)

This displays the full content of the article, including any images or formatting.

<div class="article-content">
    {dede:field.body/}
</div>

Short Description (description)

This pulls the description you entered in the article's "description" field, often used for meta descriptions or article summaries.

dede field global
(图片来源网络,侵删)
<meta name="description" content="{dede:field.description function='html2text(@me)'/}" />

Note: Using function='html2text(@me)' is a good practice to strip any HTML tags from the description, making it clean for meta tags.

Keywords (keywords)

Displays the keywords associated with the article.

<meta name="keywords" content="{dede:field.keywords/}" />

Publishing Time (pubdate)

Displays the date and time the article was published. You can format this using PHP's date() function.

<!-- Basic output: 2025-10-27 10:00:00 -->
<p>发布时间:{dede:field.pubdate/}</p>
<!-- Formatted output: 2025年10月27日 -->
<p>发布时间:{dede:field.pubdate function='strftime("%Y年%m月%d日",@me)'/}</p>
<!-- Another common format: 2025-10-27 -->
<p>发布时间:{dede:field.pubdate function='strftime("%Y-%m-%d",@me)'/}</p>

Author (writer)

Displays the author's name.

<p>作者:{dede:field.writer/}</p>

Source (source)

Displays the source of the article.

<p>来源:{dede:field.source/}</p>

Article ID (id)

This is the unique ID number of the article in the dede_archives table. It's very useful for custom development.

<!-- The ID of this article is: {dede:field.id/} -->

Advanced Usage: Using Functions

The dede:field tag becomes much more powerful when you use its built-in function modifier. The syntax is:

{dede:field.字段名 function='自定义函数(@me)'/}
  • function='...': This attribute tells DedeCMS to run a PHP function on the field's value.
  • 自定义函数: The name of the PHP function you want to use (e.g., substr, nl2br).
  • @me: This is a special variable that represents the original value of the field before the function is applied.

Example 1: Trimming a Title

If you want to limit the title to a certain number of characters for a list view, but you're on a single page template, you might still use it for consistency.

<!-- Limit the title to 30 characters -->
<h1>{dede:field.title function='substr(@me, 0, 30)'/}</h1>

Example 2: Converting Line Breaks

The WYSIWYG editor often stores line breaks as <br /> tags. If you want to preserve user-entered line breaks as actual line breaks in the HTML source, you can use nl2br.

<!-- This will convert \n to <br> tags in the content -->
<div class="article-content">
    {dede:field.body function='htmlspecialchars(nl2br(@me))'/}
</div>

Note: htmlspecialchars is used here for security, preventing XSS attacks.

Example 3: Removing Images from Content

If you want to display the article text but exclude all images, you can use a regular expression.

<!-- This will remove all <img> tags from the content -->
<div class="article-text-only">
    {dede:field.body function='preg_replace("/<img[^>]+>/i", "", @me)'/}
</div>

Complete Example: A Single Article Template (article_article.htm)

Here’s how these tags come together in a typical article template file.

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">{dede:field.title/} - {dede:global.cfg_webname/}</title>
    <meta name="description" content="{dede:field.description function='html2text(@me)'/}" />
    <meta name="keywords" content="{dede:field.keywords/}" />
</head>
<body>
    <header>
        <h1><a href="{dede:global.cfg_cmsurl/}">{dede:global.cfg_webname/}</a></h1>
    </header>
    <div class="article-container">
        <!-- Article Title -->
        <h1 class="article-title">{dede:field.title/}</h1>
        <!-- Article Meta Info -->
        <div class="article-meta">
            <span>发布时间:{dede:field.pubdate function='strftime("%Y-%m-%d %H:%M",@me)'/}</span>
            <span>作者:{dede:field.writer/}</span>
            <span>来源:{dede:field.source/}</span>
            <span>浏览:{dede:field.click/}</span>
        </div>
        <!-- Article Content -->
        <div class="article-content">
            {dede:field.body/}
        </div>
        <!-- Related Articles (uses another tag, but common in this template) -->
        <div class="related-articles">
            <h3>相关文章</h3>
            {dede:likearticle titlelen='42' row='8'}
            <li><a href="[field:arcurl/]">[field:title/]</a></li>
            {/dede:likearticle}
        </div>
    </div>
    <footer>
        <p>Copyright &copy; {dede:global.cfg_webname/} - {dede:global.cfg_powerby/}</p>
    </footer>
</body>
</html>

Summary

Tag Purpose Example
{dede:field.title/} Outputs the article's title. <h1>{dede:field.title/}</h1>
{dede:field.body/} Outputs the full article content. <div>{dede:field.body/}</div>
{dede:field.pubdate/} Outputs the publication date. <p>{dede:field.pubdate/}</p>
{dede:field.writer/} Outputs the author's name. <p>作者: {dede:field.writer/}</p>
{dede:field.id/} Outputs the unique article ID. <input type="hidden" value="{dede:field.id/}">
{dede:field.字段名 function='函数(@me)'/} Modifies the field's value with a PHP function. {dede:field.title function='substr(@me, 0, 20)'/}
-- 展开阅读全文 --
头像
织梦CMS二级栏目为何不显示?
« 上一篇 2025-12-19
C语言相比C语言,究竟扩充了哪些特性?
下一篇 » 2025-12-19

相关文章

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

目录[+]