CHtml vs Basic HTML

Hi all,

I’ve been trying to find responses to this question in the Yii Forum with no success.

What is the best option to use and why, in the application views: the properties and methods of CHtml or basic HTML?

Here is an example of both:


<div class="cars-select">

    <?php echo CHtml::dropDownList('Cars', 'car_id', array(







            'volvo-l90e-radlader'=>'L90E Radlader',


    )); ?>


Basic HTML:

<div class="cars-select">

    <select name="Cars" id="Cars">

        <optgroup label="Mazda">

            <option value="mazda-rx7">RX7</option>

            <option value="mazda-rx5">RX5</option>


        <optgroup label="Volvo">

            <option value="volvo-b9tl">B9TL</option>

            <option value="volvo-l90e-radlader">L90E Radlader</option>




Thanks in advance to all participants!


Almost all of the CHtml methods will save you time once you get used to them.

CHtml::link for example, although it can be frustrating at first it does a lot of the heavy lifting for you.

If you haven’t already done so, this wiki is a must read.


Hi there,

Thanks for your reply.

I had already seen and analysed the wiki you recommended (in fact, the lines of code above are from there), which I found interesting and well explained. However, I’d like to know if it’s best for the system to simply write down basic HTML instead of using functions that the server will have to read, understand and run to get to the same basic HTML code that I could’ve written in the first place.

In terms of performance, which one is best?

Thanks very much.


in my opinion, i think the CHtml::xxxx() is good at generating dynamic html content and go well with the

ActiveRecord or FormModel,is mainly used to generate form element , or link (you see you shouldn’t manually to write the url string);

 in your above code block , i think if you use the CActiveForm or &#036;model in the same page you 'd better use CHtml helper class , you can get benefit from Chtml class such as client side validate ajax validate,error message display ,etc...  if this view do no matter to CActiveForm or &#036;model the BasicHtml way is also ok(for example you don't have a model class for CActiveForm). after all generate html code waste some time  <img src='' class='bbc_emoticon' alt=':)' />

Thanks for your help!


I have no data but I’m guessing that plain html would be faster, my concern is how long does it take to code. I can’t worry about a few milliseconds of server time.

I think testing would be the only way to address the performance issue.

I prefer CHtml, I find it extremely well thought out and a similar syntax between methods helps you guess if you are not entirely sure of parameters.


Thanks again for replying!

I think you’re right: Plain html should avoid some milliseconds of debugging. But, if I code using CHtml, as a new Yii developer, it’ll take time to learn how to use the class and get used to it, which would be the time a senior Yii developer would save… Anyway, I believe it is worth deep learning how to code with Yii available classes.

I guess I will go for it and start learning/using CHtml. Just hope that everything can be done that way, because it’d be quite frustrating to reach a coding stage in which I’d find myself stuck with simple html elements that cannot be created throw CHtml.

Thank you,


It’s not about using either CHtml or basic HTML, you should use both. Which one depends on the situation. CHtml is great for rendering dynamic Html in cases that the e.g. htmlOptions are passed from function to function before finally being rendered.

Myself I don’t use CHtml::openTag() unless I have htmlOptions that needs to be applied. I also never use CHtml::closeTag() because you don’t have any dynamic data for the closing tag. So it feel unnecessary to call a method for that.

To answer your question, Yii is a rapid application development framework which allows for writing web applications in a very short amount of time. If you find yourself using htmlOptions with basic html, you should instead use CHtml.

Last but not least, CHtml has some methods you can’t live without such as link, label, beginForm, etc. Also, lets not forget that Yii uses the CHtml methods already internally so a few more calls to a static class shouldn’t be worth spending more time on writing view logic.

Thank you chris! I understand now the differences and I think I’m gonna have to learn how to use CHtml.