Junaid
Junaid

Reputation: 2094

confusion in using YII CHtml::link and CHtml::image instead of HTML

I have started an application in YII, I want to use proper standards of YII but am just bit curious about the use of its CHTML Class. I think that using CHtml::link() or CHtml::image() instead of normal HTML code <a></a> and <img src... /> will cost more time to application on server side. I tried to look for some reason to use it but yet not success in finding any good resource on why should I use it and how its beneficial then traditional HTML. Like I can use all those methods for defining paths to actions in normal HTML code then why use it

any help or reference for clarification would be appreciated

Thanks

Upvotes: 4

Views: 2228

Answers (1)

Alfredo Castaneda Garcia
Alfredo Castaneda Garcia

Reputation: 1659

Let's talk about CHtml::link(). Its main advantage is that you can indicate a controller route and send some get variables by simply passing an array as paramater. Lets say you wanna go to the eat() action of your LivingController, with the variable 'meal' and its value being 'hamburgers'. It could simply be done like this:

> CHtml::link(array('LivingController/eat', 'meal'=>'hamburgers'));

If you want to express this using <a></a> only, then I don't need to tell you how much harder it would be. In addition, we should remember that a link generated using CHtml::link() will always work, even if you change the url format. That wouldn't happen with <a></a>: you would have to rewrite the url every time you change the url format.

The advantages of CHtml::image() are less clear, at least to me. Sincerely, I think it's just a matter of encapsulation.

Upvotes: 5

Related Questions