Simple anchor (the not link kind)

Is this really the easiest way to do this?


CHtml::link('','',array('name'=>'top'));

to get this


<a name="top"></a>

It would be nice to be able to do something like this:


CHtml::anchor('top');

You can also use


CHtml::tag('a',array('name'=>'top'));

If this will be changed, lot of Yii users will be affected, especially the pioneers.

The simplest way is to create a shortcut for the CHtml function, like in your case:




if(!function_exists('a'))

{

	function a($name)

	{

		return CHtml::link('','',array('name'=>$name));

	}

}



by then you may use it like this:




a('top');



even simpler,isn’t it? :)

For more information on using shortcuts,see this wiki.

How would this affect people?

Thanks, I know how to create functions. My point was that a common thing like adding an anchor on a page isn’t the most straight forward thing in Yii. The link function isn’t really meant to make anchors, but it can be used (although not gracefully).

What’s wrong with making it simpler?

If the current function will be replaced, there will be a need to change the existing codes from all the files that used it. Unless of course, backward compatibility is considered, or the user will not use the updated yii framework.

Anyway, I think the reason why the functions of Yii are designed like that is because it would make the functions more flexible. In case of CHtml::link, not all users only need the name attribute to be created. Some only needs the text and the link and doesn’t care with the third parameter at all. It’s really a matter of preference, and you may put your preference in your very own functions.

Reading my reply again, I apologize if I make you look like an absolute beginner (you might be more advanced than me, I just started MVC and Yii altogether last year :) ). I actually want those absolute beginners, like I used to be, to find the example I have given if they needed one. At any rate, all I intent was to help. :)

Nope, there’s nothing wrong with making it simpler. Simple is beautiful, but not the point it will make things plain and rigid. Using the example you have given, I don’t think it would be able to handle any other options with creating the anchor aside from declaring its name attribute (even the link and the text are not there).

Even I got tired of typing Yii::app()->user->checkAccess() all the time in every link I have in view files, but still I won’t complain since there’s nothing wrong with it, and I can do something about it (by creating my own function).

Cheers! :)

Ah, I think I see the confusion. I wasn’t suggesting replacing the [font=“Courier New”]CHtml->link()[/font] function, I was just suggesting adding a [font=“Courier New”]CHtml->anchor()[/font] function.

Something as simple as adding an anchor (jumping around on the same page) should be simple. Yes, we can use


CHtml::tag('a', array('name'=>'top'))

to create an anchor, but [font=“Courier New”]CHtml->anchor()[/font] would be simple to use and doesn’t hurt anyone. After all, we have a [font=“Courier New”]CHtml::link()[/font] function and don’t have to use the [font=“Courier New”]CHtml::tag()[/font] for those.

I just feel it’s a basic feature of HTML that should be in the CHtml class to make people’s life easier, just like all the other helper functions (isn’t that the point of a framework?).