htmlOptions explained for various controls.

You are viewing revision #2 of this wiki article.
This is the latest version of this article.
You may want to see the changes made in this revision.

« previous (#1)

Most controls that are rendered by CHtml have an argument called $htmlOptions. This argument is an array that holds the attributes of the HTML element. For example the following code:

<?php echo CHtml::link("Yii","http://www.yiiframework.com",array("style"=>"color: orange;")); ?>

Will render this link:

[html]
<a style="color: orange;" href="http://www.yiiframework.com">Yii</a> 

Some tags are more complicated, for example a drop down list is composed of multiple tags:

[html]
<select>
 <option>
 <option>
</select>

To get attributes onto the options, add a sub-array called "options" to your htmlOptions. For example to color each item in your drop down a different color this code:

$items = array("1" => "first", "2" => "second");
$options["options"] = array("1"=>array("style" => "color:red"), "2"=>array("style"=>"color:blue"));
echo CHtml::dropDownList("myDropDown", null, $items,$options);

Produces this html:

[html]
<select name="myDropDown" id="myDropDown"> 
<option value="1" style="color:red">first</option> 
<option value="2" style="color:blue">second</option> 
</select> 

Note: Please add to this wiki if you know of other special cases!