By Example: CHtml | Wiki | Yii Framework

“By Example” cookbook pages will provide coding examples for many of the commonly used classes within Yii. We will try to provide as many usage examples as possible for keep these pages as helpful as possible.

Smarthead will be pulling these from the forum when he is not finding the answers on his own. Please request examples using the comments below or ask for an example in the forum. Thanks.

Avaiable methods:

CHtml::link()
CHtml::button()
CHtml::textField()
CHtml::listData()
CHtml::dropDownList()

CHtml::link() method

public static string link(string $text, mixed $url=’#’, array $htmlOptions=array ( ))

Generates a hyperlink tag.

Example 1: Linking to a controller action

HTML Output:

Link Text

Example 2: Linking to a controller action with querystring parameters

‘value1’)); ?>

HTML Output:

Link Text

Example 3: Linking to a controller action with multiple querystring parameters

‘value1’,
‘param2’=>’value2’,
‘param3’=>’value3’)); ?>

HTML Output:

Link Text

Example 4: Link opening a new page

‘value1’), array(‘target’=>’_blank’); ?>

HTML Output:

Link Text

Example 5: Linking to a controller action inside the actual controller (Suppose you are in the PostController/view and wants to link to PostController/create)

Just remove the ‘controller’ part from the string

If you are linking to an action from another controller, use the syntax of the former examples.

Example 6: Linking to a controller action from the site root (Suppose you are inside a module and wants to make the link from a controller of the root application)

In this case, add an slash “/” at the start of the string url

This makes more sense if you are working with modules.

Example 7: Linking to a controller action from another module

Replace below the module-id with desired module id .

Example 8: Linking to a controller action from the same module

This is useful when you want to make absolute paths avoiding to use static module names.

module->id}/controller/action’)); ?>

Example 9: Linking to a controller action via POST with confirmation dialog

Delete actions created using gii require the delete request be sent via POST to help prevent deleting objects by accident. Below is an example how to create a link that sends the request via POST and also asks for confirmation. Where you are redirected after the delete depends on your delete action. Note that the id link parameter below is a GET type parameter (submit URL will be something like http://example.com/post/delete/id/100).

array(‘delete’, ‘id’=>$data->ID), ‘confirm’ => ‘Are you sure?’)); ?>

If you are using CSRF protection in your application do not forget to add csrf parameter to the htmlOptions array.

array(‘delete’, ‘id’=>$data->ID), ‘confirm’ => ‘Are you sure?’, ‘csrf’=>true)); ?>

Example 10: Linking to a controller action via POST with POST parameters

If you need to make POST request with arbitary link with additional POST parameters you should use following code (submit URL will be something like http://example.com/blog/deletePost/param/100).

array(‘blog/deletePost’, ‘param’=>100),
‘params’=>array(‘id’=>$post->id, ‘status’=>Post::STATUS_DELETED_BY_OWNER),
‘csrf’=>true,
)); ?>

CHtml::button() method

public static string button(string $label=’button’, array $htmlOptions=array ( ))

Generates a button.

Example 1: Connecting a button to a controller action

array(‘controller/action’))); ?>

HTML Output:

/**/

CHtml::textField() method

public static function textField($name,$value=”,$htmlOptions=array())

Generates a textfield.

Example 1: Generating an empty textfield, just with a name

Example 2: Generating a textfield with name and value

Example 3: Generating a textfield with customized id, width and maxlength

‘idTextField’,
‘width’=>100,
‘maxlength’=>100); ?>

*Note: use ‘cols’ instead of ‘width’ when working with textareas

Example 4: Generating a disabled textfield

‘disabled’); ?>

CHtml::listData() method

public static function listData($models,$valueField,$textField,$groupField=”)

Generates data for dropDownList and listBox, using the format $key=>$value.

Example 1: Generating a list data for categories

findAll();

// format models resulting using listData
$list = CHtml::listData($models,
‘category_id’, ‘category_name’);

print_r($list);

HTML Output (Example):

array(“1” => “Arts”, “2” => “Science”, “3” => “Culture”);

Example 2: Generating an ordered list data for categories using findAll parameter

findAll(
array(‘order’ => ‘category_name’));

$list = CHtml::listData($models,
‘category_id’, ‘category_name’);

print_r($list);

HTML Output (Example):

array(“1” => “Arts”, “3” => “Culture”, “2” => “Science”);

CHtml::dropDownList() method

public static function dropDownList($name,$select,$data,$htmlOptions=array())

Generates a dropdown list.

$name: A name for the dropdownList; $select: selected item from the $data $data: an array of the type $key => $value (the possible values of you dropdownlist); $htmlOptions: another options.

Example 1: Generating a simple dropdownlist for gender

‘Male’, ‘F’ => ‘Female’));

The $select parameter must contain value ‘M’ or ‘F’.

Example 2: Generating a simple dropdownlist for gender with an ’empty’ element.

This example will avoid the dropdownlist to be blank when no value, outputing some proper information to the user

‘Male’, ‘F’ => ‘Female’),
array(’empty’ => ‘(Select a gender)’));

Example 3: Using data from a model function.

It is better to have you gender list definition inside your model definition.

At model:

public function getGenderOptions(){
return array(‘M’ => ‘Male’, ‘F’ => ‘Female’);
}

At view:

genderOptions,
array(’empty’ => ‘(Select a gender’));

Example 4: Using data from db

In order to create a dropdownList with data from db, you have to combine it with listData method that will format models from db into array of $key=>$value.

This part is equal to listData Example 2:

// retrieve the models from db
$models = categories::model()->findAll(
array(‘order’ => ‘category_name’));

// format models as $key=>$value with listData
$list = CHtml::listData($models,
‘category_id’, ‘category_name’);

Now, we generate our dropdowList from the $list variable

‘(Select a category’));

Example 5: Generating dropdownlist with option groups.

If you need to generate dropdownlist using both optgroup and option tags use the following code.

array(
‘mazda-rx7’=>’RX7’,
‘mazda-rx5’=>’RX5’,
),
‘Volvo’=>array(
‘volvo-b9tl’=>’B9TL’,
‘volvo-l90e-radlader’=>’L90E Radlader’,
),
)); ?>

Code above should generate following HTML.

RX7
RX5

B9TL
L90E Radlader

You can find CHtml class at yii/framework/web/helpers/CHtml.php.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s