dede setparameter参数如何设置与使用?

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

Of course! The dede:php and dede:var tags, along with PHP's global keyword, are the primary ways to work with parameters in DedeCMS. This is often referred to as "setting parameters" or "passing variables" between templates.

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

Here’s a comprehensive breakdown of how it works, from the basic concept to practical examples.

The Core Concept: The $GLOBALS Array

In PHP, the $GLOBALS array is a superglobal variable that holds all variables available in the global scope. DedeCMS leverages this extensively to pass data between different parts of a template, especially from a list page to an item page.

The dede:php tag allows you to execute PHP code directly within your template, and dede:var allows you to assign a value to a variable.


Method 1: Setting a Parameter with dede:php (Most Common)

This is the most flexible and powerful method. You can set a global variable in one part of your template (e.g., a list page) and then retrieve it in another (e.g., an associated article page).

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

Syntax

{dede:php}
    $GLOBALS['your_variable_name'] = 'your_value';
    // Or you can assign a value from a DedeCMS field
    $GLOBALS['article_title'] = $fields['title'];
{/dede:php}

Example: Passing a Category Name to a Sub-Template

Let's say you have a list of articles and you want to display the category name in a specific way on every article in that list.

In your list_article.htm template:

You would set the global variable for each item in the list loop.

{dede:list pagesize='10'}
    <h2><a href="[field:arcurl/]">[field:title/]</a></h2>
    <p>[field:description/]...</p>
    <!-- 
      Here we set a global variable named 'current_category_name'
      for each article in the loop. The value comes from the channel's
      associated model or a custom field.
    -->
    {dede:php}
        $GLOBALS['current_category_name'] = 'News'; // You could also get this dynamically
    {/dede:php}
    {dede:include filename="article_inc.htm" /}
{/dede:list}

In your included article_inc.htm template:

Now, you can access that variable.

<div class="article-meta">
    This article belongs to the category: <strong>{dede:var name='current_category_name'/}</strong>
</div>

Method 2: Retrieving a Parameter with dede:var

The dede:var tag is used to retrieve the value of a variable that has been set in the $GLOBALS array. It's the "getter" to dede:php's "setter".

Syntax

{dede:var name='your_variable_name'/}

This will simply output the value of $GLOBALS['your_variable_name'].

Example: Using a Counter

You can use dede:php to create a counter and display it.

{dede:list pagesize='10'}
    {dede:php}
        // Check if the counter is set, if not, initialize it to 0
        if (!isset($GLOBALS['item_counter'])) {
            $GLOBALS['item_counter'] = 0;
        }
        // Increment the counter
        $GLOBALS['item_counter']++;
    {/dede:php}
    <p>
        This is item number: <strong>{dede:var name='item_counter'/}</strong><br>
        The title is: [field:title/]
    </p>
{/dede:list}

Method 3: The Special Case - global $cfg_cmspath;

Sometimes, what you're looking for isn't a custom variable, but a core DedeCMS configuration variable. These are already available as global PHP variables in your templates.

For example, to get the absolute path to your DedeCMS installation, you don't need to set it. You just need to declare it as global within a dede:php block.

Example: Displaying the CMS Path

{dede:php}
    // You must declare the global variable to use it inside this block
    global $cfg_cmspath;
    echo "The DedeCMS path is: " . $cfg_cmspath;
{/dede:php}

Commonly used global DedeCMS variables:

  • $cfg_cmspath: Absolute path to the DedeCMS root.
  • $cfg_basehost: Your website's base URL (e.g., http://www.yourdomain.com).
  • $cfg_maintype: The main channel ID.

Practical Use Case: Conditional Styling Based on Category

This is a very common real-world scenario. Let's say you want to add a different CSS class to articles based on their category ID.

In your list_article.htm:

We'll set a global variable css_class based on the category ID.

{dede:list pagesize='10'}
    {dede:php}
        // Get the current channel's typeid (category ID)
        $typeid = $typeid;
        if ($typeid == 1) {
            $GLOBALS['css_class'] = 'news-article';
        } elseif ($typeid == 2) {
            $GLOBALS['css_class'] = 'tutorial-article';
        } else {
            $GLOBALS['css_class'] = 'default-article';
        }
    {/dede:php}
    <!-- Now use the variable in your HTML -->
    <div class="article-item {dede:var name='css_class'/}">
        <h3><a href="[field:arcurl/]">[field:title/]</a></h3>
        <p>[field:info/]...</p>
    </div>
{/dede:list}

Resulting HTML:

<!-- For an article in category ID 1 -->
<div class="article-item news-article">
    <h3><a href="...">Article Title 1</a></h3>
    <p>Article summary...</p>
</div>
<!-- For an article in category ID 2 -->
<div class="article-item tutorial-article">
    <h3><a href="...">Article Title 2</a></h3>
    <p>Article summary...</p>
</div>

Important Considerations

  1. Security: Using dede:php can be a security risk if your website allows users with non-administrator roles to edit templates. It allows direct PHP execution. Only use it on trusted, admin-controlled templates.
  2. Performance: Overuse of dede:php can slow down your site because the PHP interpreter has to process the code on every page load. Use it judiciously.
  3. Readability: While powerful, embedding complex PHP logic directly into your HTML template can make the code harder to read and maintain for front-end developers. For very complex logic, consider creating a custom PHP function in include/extend.func.php and calling it from your template.

Summary

Method Purpose Example
dede:php Set a global variable or execute custom PHP logic. {dede:php}$GLOBALS['my_var'] = 'Hello';{/dede:php}
dede:var Get and display the value of a global variable. <p>{dede:var name='my_var'/}</p>
global $var; Access a pre-existing DedeCMS configuration variable. {dede:php}global $cfg_basehost; echo $cfg_basehost;{/dede:php}
-- 展开阅读全文 --
头像
dedetagparse功能是什么?如何使用?
« 上一篇 2025-12-19
dede:arcpagelist分页标签如何使用?
下一篇 » 2025-12-19

相关文章

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

目录[+]