Of course! The GetOptionList function is a very useful and frequently used function in DedeCMS (织梦CMS). It's designed to fetch and format the contents of a system parameter or a custom model field, typically one that is stored as a comma-separated list of key-value pairs.

Here’s a detailed breakdown of what it does, its parameters, and how to use it with practical examples.
What is GetOptionList()?
The primary purpose of GetOptionList() is to take a string that represents a list of options (like a select dropdown or radio button group) and convert it into an HTML <option> list.
It is most commonly used for:
- System Parameters: Displaying settings like "网站栏目列表" (Website Category List) or "专题列表" (Special Topic List).
- Custom Model Fields: Rendering options for custom fields that use the "枚举/单选" (Enumeration/Radio) or "枚举/多选" (Enumeration/Checkbox) type.
Function Syntax
The function is located in include/common.func.php.

function GetOptionList($string, $default = '')
Parameters:
-
$string(string): This is the required input. It's the source string containing the options.- Format: It must be in a specific format:
value1|name1,value2|name2,value3|name3,... - Example:
1|男,2|女,3|保密
- Format: It must be in a specific format:
-
$default(string, optional): This is the value that should be pre-selected by default in the HTML<option>list.- If you provide a value here, the corresponding
<option>tag will have theselected="selected"attribute. - If the value in
$stringdoesn't match$default, no option will be selected by default.
- If you provide a value here, the corresponding
Return Value:
- It returns a string containing the generated HTML
<option>tags. - If the input
$stringis empty or not in the correct format, it will return an empty string.
How to Use GetOptionList() (Examples)
Let's look at common scenarios where you would use this function.
Example 1: Using with a Static String
This is the most basic way to understand the function's core functionality.

// The source string in the required format $gender_options = "1|男,2|女,3|保密"; // Generate the option list, with '2' (女) as the default selected value $html_output = GetOptionList($gender_options, '2'); // Print the result echo $html_output;
Expected Output:
<option value="1">男</option> <option value="2" selected="selected">女</option> <option value="3">保密</option>
Example 2: Using with a DedeCMS System Parameter
This is a very common use case. Let's say you want to display a dropdown list of all main categories on your website in a template.
-
First, you need to get the parameter string. DedeCMS has a function
GetSysKeys()for this, but a more direct way is often to useGetChannelsonly()for channels orGetClasssonly()for categories. However, for general system parameters, you might find them stored in thedede_sysconfigtable.A more reliable method for getting the category list is to use Dede's built-in tags or functions. Let's use
GetSonIds()to get a list of all category IDs and then format it. For simplicity, let's assume we have a system parameter namedcfg_listddthat holds the string.For this example, let's simulate getting the category list string. A better function for this specific case is
GetChannelsonly().// Get a list of all channels (this is a simplified example) // In a real template, you'd use Dede tags. // Let's assume we have a string like this: $channel_options = "1|公司简介,2|产品中心,3|新闻资讯,4|联系我们";
-
Now, use
GetOptionList()in your PHP code:$channel_options = "1|公司简介,2|产品中心,3|新闻资讯,4|联系我们"; // Generate the option list, no default selection echo "<select name='typeid'>"; echo "<option value='0'>请选择栏目</option>"; echo GetOptionList($channel_options); echo "</select>";
Expected Output:
<select name='typeid'> <option value='0'>请选择栏目</option> <option value="1">公司简介</option> <option value="2">产品中心</option> <option value="3">新闻资讯</option> <option value="4">联系我们</option> </select>
Example 3: Using in a DedeCMS Template (Most Common)
This is where GetOptionList() truly shines. You'll use it within a template file (.htm) to display options from a custom field.
Scenario: You have a custom model for "Products" with a custom field named product_size (产品尺寸), which is a "单选" (Radio) field. The options you set in the Dede后台 (后台) were: S|小号,M|中号,L|大号,XL|加大号.
Step 1: Edit the Template File
Open the template file for the product detail page (e.g., article_product.htm). Find where you want to display the size options.
Step 2: Use the {dede:field} tag with function='GetOptionList()'
The function attribute of the {dede:field} tag allows you to call a PHP function on the field's value.
<div class="form-group">
<label for="size">选择尺寸:</label>
<select name="size" id="size" class="form-control">
<!--
{dede:field.size} is the value of the custom field.
In the database, this field would store the value, e.g., 'M'.
The function 'GetOptionList' needs the *list string*, not the single value.
This is a common point of confusion.
-->
<!--
CORRECT WAY:
You need to pass the *list definition* to the function.
This list definition is NOT stored in the database for the article.
It's stored in the model's field definition.
Therefore, this direct method in the template often doesn't work as expected
because the template doesn't have access to the raw list string.
A better approach is to pass the string manually if it's static,
or use a PHP block in the template.
-->
<!-- Method A: Hardcoded list (if it never changes) -->
{dede:php}
$size_options = 'S|小号,M|中号,L|大号,XL|加大号';
echo GetOptionList($size_options, $fields['size']); // Use $fields['size'] for default
{/dede:php}
<!-- Method B: More robust way using a dedicated function in an include file -->
<!-- Let's assume you've added a function in your custom module -->
</select>
</div>
Clarification for Template Usage:
The confusion arises because the template ({dede:field.size}) holds the selected value (e.g., M), but GetOptionList() needs the full list definition (e.g., S|小号,M|中号,L|大号,XL|加大号).
This list definition is not readily available in the template scope. Therefore, the most practical ways to use it are:
- Hardcoded String (as shown above): If the options are fixed, you can just put the string directly in your PHP block.
- Custom Function: Create a small PHP function in an include file (e.g.,
include/extend.func.php) that fetches the option string from the database based on the field name or ID, and then call that function from your template. This is the most flexible solution for dynamic content.
Important Considerations and Common Pitfalls
- Format is Crucial: If your
$stringis not invalue|name,value|name,...format, the function will fail and return an empty string. Check for extra spaces or commas. - Template Scope: Remember that in a template, you usually only have the single selected value of a field, not the entire list of options. You need to find a way to provide the full list string to the function.
- Security: The function itself does not perform sanitization. If the values come from user input, ensure they are properly sanitized before being used to prevent XSS attacks. However, since this function is typically used for backend-defined lists, this is less of a concern.
- Performance: For very long lists (thousands of options), generating the HTML on every page load can have a tiny performance impact. For most websites, this is negligible. For extremely large lists, consider other UI solutions like a searchable dropdown.
Summary
| Feature | Description |
|---|---|
| Purpose | To convert a comma-separated value|name string into an HTML <option> list. |
| Primary Use Case | Rendering custom model fields (radio/checkbox) and system parameters in DedeCMS templates. |
| Syntax | GetOptionList($string, $default = '') |
$string Format |
value1\|name1,value2\|name2,... (e.g., 1\|Red,2\|Blue,3\|Green) |
| Return Value | A string containing the HTML <option> tags. |
| Key Challenge | In templates, obtaining the full option list string, as the template usually only has the selected value. |
