By Example: CHtml

You are viewing revision #6 of this wiki article.
This version may not be up to date with the latest version.
You may want to view the differences to the latest version or see the changes made in this revision.

« previous (#5)next (#7) »

  1. CHtml::link() method
  2. Chtml::button() method

"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.

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
<?php echo CHtml::link('Link Text',array('controller/action')); ?>
HTML Output:
<a href="index.php?r=controller/action">Link Text</a>
Example 2: Linking to a controller action with querystring parameters
<?php echo CHtml::link('Link Text',array('controller/action',
                                         'param1'=>'value1')); ?>
HTML Output:
<a href="index.php?r=controller/action&param1=value1">Link Text</a>
Example 3: Linking to a controller action with multiple querystring parameters
<?php echo CHtml::link('Link Text',array('controller/action',
                                         'param1'=>'value1',
                                         'param2'=>'value2',
                                         'param3'=>'value3')); ?>
HTML Output:
<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 echo CHtml::button('Button Text', array('submit' => 'controller/action')); ?>
HTML Output:
<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>
79 0
74 followers
Viewed: 993 814 times
Version: Unknown (update)
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

Related Articles