Difference between #8 and #14 of
By Example: CHtml

Revision #14 has been created by ricardograna on Oct 7, 2009, 7:12:11 PM with the memo:

Added dropDownList
« previous (#8) next (#15) »

Changes

Title unchanged

By Example: CHtml

Category unchanged

Tutorials

Yii version unchanged

Tags unchanged

Content changed

"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.
 
 
## CHtml::link() method
 
 
```php 
public static string link(string $text, mixed $url='#', array $htmlOptions=array ( ))
 
```
 
Generates a hyperlink tag.
 
 
***
 
 
### Example 1: Linking to a controller action
 
 
```php 
<?php echo CHtml::link('Link Text',array('controller/action')); ?>
 
```
 
 
#### HTML Output:
 
 
 
```php 
<a href="index.php?r=controller/action">Link Text</a>
 
```
 
 
***
 
 
### Example 2: Linking to a controller action with querystring parameters
 
 
```php 
<?php echo CHtml::link('Link Text',array('controller/action',
 
                                         'param1'=>'value1')); ?>
 
```
 
 
#### HTML Output:
 
 
 
```php 
<a href="index.php?r=controller/action&param1=value1">Link Text</a>
 
```
 
 
***
 
 
### Example 3: Linking to a controller action with multiple querystring parameters
 
 
```php 
<?php echo CHtml::link('Link Text',array('controller/action',
 
                                         'param1'=>'value1',
 
                                         'param2'=>'value2',
 
                                         'param3'=>'value3')); ?>
 
```
 
 
#### HTML Output:
 
 
 
```php 
<a href="index.php?r=controller/action&param1=value1&param2=value2&param3=value3">Link Text</a>
 
```
 
   
 
_  
 
 
_  
 
 
 
## 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
 
 
```php 
<?php echo CHtml::button('Button Text', array('submit' => 'controller/action')); ?>
 
```
 
 
#### HTML Output:
 
 
 
```php 
<input id="yt0" type="button" value="Button Text" name="yt0"/>
 
<script type="text/javascript">
 
/*<![CDATA[*/
 
jQuery(document).ready(function() {
 
             jQuery('#yt0').click(function( {
 
                            jQuery.yii.submitForm(
 
                                     this,
 
                                     'controller/action',{}
 
                                          );return false;});
 
                                  });
 
/*]]>*/
 
</script>
 
```
Avaiable methods:<br>
 
 
- CHtml::link()
 
 
- CHtml::button()
 
 
- CHtml::dropDownList()
 
 
<br>
 
 
<a name="link"></a>
 
## CHtml::link() method
 
 
 
```php 
public static string link(string $text, mixed $url='#', array $htmlOptions=array ( ))
 
```
 
Generates a hyperlink tag.
 
 
***
 
 
### Example 1: Linking to a controller action
 
 
 
```php 
<?php echo CHtml::link('Link Text',array('controller/action')); ?>
 
```
 
 
 
#### HTML Output:
 
 
 
```php 
<a href="index.php?r=controller/action">Link Text</a>
 
```
 
 
 
***
 
 
### Example 2: Linking to a controller action with querystring parameters
 
 
```php 
<?php echo CHtml::link('Link Text',array('controller/action',
 
                                         'param1'=>'value1')); ?>
 
```
 
 
#### HTML Output:
 
 
 
```php 
<a href="index.php?r=controller/action&param1=value1">Link Text</a>
 
```
 
 
***
 
 
### Example 3: Linking to a controller action with multiple querystring parameters
 
 
```php 
<?php echo CHtml::link('Link Text',array('controller/action',
 
                                         'param1'=>'value1',
 
                                         'param2'=>'value2',
 
                                         'param3'=>'value3')); ?>
 
```
 
 
#### HTML Output:
 
 
 
```php 
<a href="index.php?r=controller/action&param1=value1&param2=value2&param3=value3">Link Text</a>
 
```
 
   
 
***
 
 
### Example 4: Link opening a new page
 
 
```php 
<?php echo CHtml::link('Link Text',array('controller/action',
 
                   'param1'=>'value1'), array('target'=>'_blank'); ?>
 
```
 
 
#### HTML Output:
 
 
 
```php 
<a target="_blank" href="index.php?r=controller/action&param1=value1">Link Text</a>
 
```
 
 
***
 
 
### 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
 
 
```php 
<?php echo CHtml::link('Link Text',array('action')); ?>
 
```
 
 
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
 
 
```php 
<?php echo CHtml::link('Link Text',array('/controller/action')); ?>
 
```
 
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 .
 
 
```php 
<?php echo CHtml::link('Link Text',array('/module-id/controller/action')); ?>
 
```
 
 
***
 
 
### 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.
 
 
```php 
<?php echo CHtml::link('Link Text',array('/{$this->module->id}/controller/action')); ?>
 
```
 
 
_  
 
 
_  
 
 
 
<a name="button"></a>
 
## 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
 
 
```php 
<?php echo CHtml::button('Button Text', array('submit' => 'controller/action')); ?>
 
```
 
 
#### HTML Output:
 
 
 
```php 
<input id="yt0" type="button" value="Button Text" name="yt0"/>
 
<script type="text/javascript">
 
/*<![CDATA[*/
 
jQuery(document).ready(function() {
 
             jQuery('#yt0').click(function( {
 
                            jQuery.yii.submitForm(
 
                                     this,
 
                                     'controller/action',{}
 
                                          );return false;});
 
                                  });
 
/*]]>*/
 
</script>
 
```
 
 
 
_  
 
 
_  
 
 
 
<a name="textField"></a>
 
## CHtml::textField() method
 
 
~~~
 
public static function textField($name,$value='',$htmlOptions=array())
 
~~~
 
Generates a textfield.
 
 
***
 
 
### Example 1: Generating an empty textfield, just with a name
 
 
```php 
<?php echo CHtml::textField('Text'); ?>
 
```
 
 
***
 
### Example 2: Generating a textfield with name and value
 
 
```php 
<?php echo CHtml::textField('Text', 'some value'); ?>
 
```
 
 
***
 
### Example 3: Generating a textfield with customized id, width and maxlength
 
 
```php 
<?php echo CHtml::textField('Text', 'some value',
 
 array('id'=>'idTextField', 
 
       'width'=>100, 
 
       'maxlength'=>100); ?>
 
```
 
 
*Note: use 'cols' instead of 'width' when working with textareas
 
 
***
 
### Example 4: Generating a disabled textfield 
 
 
```php 
<?php echo CHtml::textField('Text', 'some value', 
 
    array('disabled'=>true); ?>
 
```
 
 
 
 
_  
 
 
_  
 
 
 
<a name="textField"></a>
 
## 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
 
 
```php 
<?php echo CHtml::dropDownList('listname', $select, 
 
              array('M' => '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
 
 
```php 
<?php echo CHtml::dropDownList('listname', $select, 
 
              array('M' => '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:
 
 
```php 
public function getGenderOptions(){
 
    return array('M' => 'Male', 'F' => 'Female');
 
}
 
```
 
 
At view:
 
 
```php 
<?php echo CHtml::dropDownList('listname', $select, 
 
              $model->genderOptions,
 
              array('empty' => '(Select a gender'));         
 
```
 
 
***
 
You cand find CHtml class at yii/framework/web/helpers/CHtml.php
79 0
74 followers
Viewed: 990 589 times
Version: 1.1
Category: Tutorials
Written by: jonah
Last updated by: glicious
Created on: Sep 25, 2009
Last updated: 8 years ago
Update Article

Revisions

View all history