dede option是什么?如何使用?

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

Of course! The dede:option tag is a fundamental and frequently used tag in DedeCMS (织梦内容管理系统). It's primarily used to generate a list of options for an HTML <select> (dropdown) or <datalist> element.

Its most common use is to populate dropdown menus with data from a DedeCMS channel, which is perfect for things like category lists, archive lists, or other dynamic data sources.


Basic Syntax

The tag is very simple:

{dede:option}

When used on its own, it generates a series of <option value="...">...</option> lines.


Primary Use Case: Populating a Category Dropdown

This is the most important and common use for the dede:option tag. Let's say you have a channel for "Products" and you want to let users select a product category from a dropdown menu on a form.

Example: A Simple Category Dropdown

Imagine you have a channel named "Products" (ID = 1). You want to display all its categories in a dropdown.

<label for="category">Select a Category:</label>
<select name="category" id="category">
    <option value="0">Please Choose...</option>
    {dede:channel type='son' typeid='1'}
        <option value="[field:id/]">[field:typename/]</option>
    {/dede:channel}
</select>

Explanation:

  • {dede:channel type='son' typeid='1'}: This is the parent tag that provides the data. It gets all sub-categories (type='son') of the main channel with ID 1 (typeid='1').
  • [field:id/]: This is a field variable inside the loop. It outputs the unique ID of the category. This is perfect for the value attribute of the <option> tag.
  • [field:typename/]: This outputs the name of the category (e.g., "Laptops", "Monitors"). This is what the user sees in the dropdown.

Attributes of dede:option

The dede:option tag itself has very few attributes. Its behavior is almost entirely controlled by its parent tag, which is almost always {dede:channel}.

However, the most important attribute is reid, which is part of the {dede:channel} tag but directly affects the output of the option.

reid (Return ID)

This attribute tells {dede:channel} to list categories that are children of a specific category ID, rather than children of the main channel.

Example: Sub-categories of a specific category

Let's say you have a main "Products" category (ID 1) and a "Laptops" sub-category (ID 5). You want to show only the models of laptops, not all products.

<label for="model">Select a Laptop Model:</label>
<select name="model" id="model">
    <option value="0">Please Choose...</option>
    {dede:channel type='son' typeid='1' reid='5'}
        <option value="[field:id/]">[field:typename/]</option>
    {/dede:channel}
</select>

Explanation:

  • typeid='1': Start from the main "Products" channel.
  • reid='5': But, only list categories whose parent ID is 5 (the "Laptops" category).
  • This would output options for "Gaming Laptops", "Business Laptops", etc., assuming those were the sub-categories of ID 5.

Advanced Usage with Other Tags

While {dede:channel} is the most common parent, dede:option can technically be used with other tags that generate a list, such as {dede:arclist} or {dede:sql}. However, this is less common.

Example: Using {dede:sql} for Custom Dropdowns

Imagine you have a custom SQL table dede_member_jobs and you want to create a dropdown of job titles.

<label for="job_title">Job Title:</label>
<select name="job_title" id="job_title">
    <option value="0">Please Choose...</option>
    {dede:sql sql="SELECT id, title FROM `dede_member_jobs` ORDER BY title ASC"}
        <option value="[field:id/]">[field:title/]</option>
    {/dede:sql}
</select>

Explanation:

  • {dede:sql ...}: Executes a custom SQL query.
  • [field:id/]: Outputs the id column from your result set.
  • [field:title/]: Outputs the title column.

Complete Practical Example: An Archive Dropdown

A classic use case is creating a dropdown to filter articles by year and month.

<label for="archive">View Archives:</label>
<select name="archive" id="archive" onchange="window.location.href=this.value;">
    <option value="">-- Select Month --</option>
    {dede:arclist typeid='0' orderby='pubdate' subday='30'}
        <option value="[field:arcurl/]">
            [field:pubdate function="MyDate('Y-m',@me)"/]
        </option>
    {/dede:arclist}
</select>

Explanation:

  • {dede:arclist ...}: We use arclist to get articles from the main site (typeid='0').
  • orderby='pubdate': Sorts articles by their publication date.
  • subday='30': This is a key attribute. It limits the results to articles published within the last 30 days, effectively getting articles from the current month.
  • [field:arcurl/]: This is the URL of the article. Using it as the value makes the dropdown a navigation tool.
  • onchange="window.location.href=this.value;": This JavaScript makes the dropdown act like a link when a user selects an option.
  • [field:pubdate function="MyDate('Y-m',@me)"/]: This uses a DedeCMS function to format the date into a clean "Year-Month" format (e.g., "2025-10") for the user to see.

Summary Table

Attribute Tag Description Example
reid {dede:channel} Crucial for option. Specifies the parent category ID to list sub-categories from. {dede:channel reid='5'}
typeid {dede:channel} The ID of the parent channel to start from. {dede:channel typeid='1'}
type {dede:channel} Specifies the type of channel to list. 'son' is most common for sub-categories. {dede:channel type='son'}
sql {dede:sql} The custom SQL query to provide data for the options. {dede:sql sql="SELECT..."}

In conclusion, the dede:option tag is a simple but powerful tool for generating dynamic dropdown lists in DedeCMS. Its real power comes from its partnership with tags like {dede:channel} to pull live data from your site's structure.

-- 展开阅读全文 --
头像
c语言程序设计知识点总结
« 上一篇 03-29
dede writer是什么?
下一篇 » 03-29

相关文章

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

目录[+]