Yii: How to make a button with an html tag inside a tag

I'm trying to use some bootstrap features like icon glyphs in the Yii CHtml class, here is my code:

<?php echo CHtml::submitButton('<i class="icon-user"></i> Login', array( 'class' => 'btn btn-large pull-right' )); ?> 

But it does not β€œrecognize” the tag and simply displays the tag as shown below. enter image description here

Does anyone know how to get around it (without entering the html tags themselves).

Thanks guys.

+7
source share
5 answers

CHtml::submitButton creates a <input type="submit"> that cannot accept additional HTML as its content. However, you can do something to your taste with the help of CHtml::tag :

 echo CHtml::tag('button', array('class' => 'btn btn-large pull-right'), '<i class="icon-user"></i> Login'); 

This will result in a <button> tag that can accept arbitrary HTML as its content.

Update: As frostyterrier notes in the comments, there is a built-in CHtml::htmlButton method that allows you to do this even easier:

 echo CHtml::htmlButton('<i class="icon-user"></i> Login', array('class' => 'btn btn-large pull-right')); 
+11
source

Try setting 'encode' to false in the htmlOptions parameter.

 <?php echo CHtml::submitButton('<i class="icon-user"></i> Login', array( 'encode' => false, 'class' => 'btn btn-large pull-right' )); ?> 
+3
source

CHTML::submitbutton generates a <intput type="submit" /> , and you are trying to cram the HTML into its value attribute.

Why don't you just add the icon-user CSS class to the button itself?

 echo CHtml::submitButton('Login', array( 'class' => 'btn btn-large pull-right icon-user' )); 

If it is a class defined by Bootstrap, it should style the enter button well.

0
source

try it

 $this->widget("bootstrap.widget.TbButton", array( 'buttonType'=>'submit', 'label=>'Login', 'type'=>'success', //or default or warning... as you like 'icon'=>'user', 'size'=>'large', //small, mini, or just comment line for default )); 

: http://www.cniska.net/yii-bootstrap/#tbButton

0
source

First, you must go from subbmitButton to htmlButton, for example, write the following code:

 <?php echo CHtml::htmlButton('<span>Log in!</span>',array('type'=>'submit','class'=>'tbutton small pad')); ?> 
0
source

All Articles