dede field body

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

Of course! The dede:field.body tag is one of the most fundamental and frequently used tags in the DedeCMS (织梦CMS) template system. It's used to display the main content of an article or a document.

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

Let's break it down in detail.


What is dede:field.body?

In simple terms, dede:field.body is a DedeCMS template tag that fetches and displays the full body content of an article from the dede_addonarticle table (the default table for articles).

  • dede:: This is the prefix for all DedeCMS template tags.
  • field: This indicates that we are retrieving a specific field from the article's database record.
  • body: This is the name of the specific field we want, which holds the main content (the text, images, HTML, etc.) of the article.

Basic Usage

You place the dede:field.body tag directly into your article template file (e.g., article_article.htm) where you want the content to appear.

Example (article_article.htm):

dede field body
(图片来源网络,侵删)
<!DOCTYPE html>
<html>
<head>{dede:field.title/}</title>
</head>
<body>
    <h1>{dede:field.title/}</h1>
    <div class="info">
        发布时间:{dede:field.pubdate function='MyDate('Y-m-d H:i:s', @me)'/} | 作者:{dede:field.author/}
    </div>
    <hr>
    <!-- This is where the main content of the article will be displayed -->
    <div class="article-content">
        {dede:field.body/}
    </div>
    <hr>
    <div class="related-articles">
        {dede:likearticle title='相关文章' row='5'}
        <a href="[field:arcurl/]">[field:title/]</a>
        {/dede:likearticle}
    </div>
</body>
</html>

In this example, {dede:field.body/} will be replaced by the actual content of the article when the page is generated.


Advanced Usage and Parameters

The basic {dede:field.body/} tag is powerful, but you can enhance it with parameters to control its output, especially for SEO and mobile responsiveness.

Parameter: description

This parameter automatically generates a summary or description from the beginning of the article's body content. It's extremely useful for the description meta tag in your HTML <head> section.

Example (article_article.htm in the <head> section):

dede field body
(图片来源网络,侵删)
<head>{dede:field.title/}</title>
    <meta name="description" content="{dede:field.body function='html2text(@me)'/}" />
    <meta name="keywords" content="{dede:field.keywords/}" />
</head>
  • function='html2text(@me)': This is a crucial function. The body field contains HTML tags. The html2text function strips all these HTML tags, leaving only the plain text, which is perfect for a meta description. Without this, your description would be cluttered with <p>, <img>, etc.

Parameter: style

This parameter is used to automatically add CSS classes to elements within the body content. The most common use is for images, especially when you want to make images responsive.

Example:

<div class="article-content">
    {dede:field.body style='img:article-img;/p:p-content;'/}
</div>

How it works:

  • img:article-img;: This finds all <img> tags within the body content and adds the class article-img to them. You can then define .article-img in your CSS to make images responsive (e.g., max-width: 100%; height: auto;).
  • p:p-content;: This finds all <p> tags and adds the class p-content to them.

Parameter: maxlength

This parameter truncates the body content to a specified number of characters. It's useful for showing a short excerpt on a list page, although {dede:field.description/} is often a better choice for this.

Example:

<div class="short-excerpt">
    {dede:field.body function='cn_substr(@me, 200)'/}...
</div>
  • function='cn_substr(@me, 200)': This is a more flexible way to achieve a similar result. cn_substr is a DedeCMS function that cuts a string to a specified length (200 characters in this case). The @me variable represents the current value of the field (the full body content).

Best Practices and Common Issues

a. Handling Images and Paths

A very common issue with dede:field.body is that images added to the article editor might have paths relative to the admin panel (e.g., /uploads/allimg/...). When viewed on the front end, these paths can break.

Solution: Use the body field's replace function to correct the paths.

<div class="article-content">
    {dede:field.body replace='{cmspath}/'/}
</div>
  • replace='{cmspath}/': This function finds all occurrences of {cmspath} in the body content and replaces them with the actual root path of your website (e.g., http://www.yoursite.com). This ensures all images and linked resources load correctly on the front end.

b. Using PHP Code in the Body

By default, DedeCMS strips PHP code from the body field for security reasons. If you need to allow PHP code (e.g., for embedding a script or complex dynamic content), you must modify the system.

Warning: This is a security risk and should be done with caution.

  1. Edit the config.cache.inc.php file.
  2. Find the line $cfg_notallowstr = '<iframe,script,meta,link,object';.
  3. Remove 'script' from the list. It should look like this: $cfg_notallowstr = '<iframe,meta,link,object';.
  4. Now, PHP code like <?php echo 'Hello World'; ?> will be executed when the page is loaded.

c. SEO and Performance

  • Lazy Loading: For better performance, especially on image-heavy articles, you should implement lazy loading. You can do this by adding the loading="lazy" attribute to all images in the body. The style parameter is perfect for this.

    <div class="article-content">
        {dede:field.body style='img:img lazyload;'/}
    </div>

    Then, in your CSS, you would have a rule for .lazyload. You would also need to include a JavaScript library (like lazysizes) to handle the actual lazy loading logic.

  • Content Formatting: Ensure your CSS styles the content within .article-content properly (e.g., sets a good font size, line height, and max-width for readability).


Summary Table

Tag / Syntax Description Example
{dede:field.body/} Basic: Displays the full HTML content of the article. <div>{dede:field.body/}</div>
{dede:field.body function='html2text(@me)'/} SEO: Strips HTML to create a plain text description. <meta name="description" content="{dede:field.body function='html2text(@me)'/}" />
{dede:field.body style='img:my-class;'/} Styling: Adds a CSS class to all images in the content. <div>{dede:field.body style='img:responsive-img;'/}</div>
{dede:field.body replace='{cmspath}/'/} Path Correction: Fixes image paths for the front end. <div>{dede:field.body replace='{cmspath}/'/}</div>
{dede:field.body function='cn_substr(@me, 150)'/} Truncation: Shows only the first 150 characters of the content. <p>{dede:field.body function='cn_substr(@me, 150)'/}...</p>

By understanding and using these variations of dede:field.body, you can have full control over how your article content is displayed, making your DedeCMS website more functional, SEO-friendly, and user-friendly.

-- 展开阅读全文 --
头像
dede field name是什么标签?
« 上一篇 01-10
织梦程序如何修改?
下一篇 » 01-10

相关文章

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

目录[+]