Ricardo Fiorani
Ricardo Fiorani

Reputation: 782

Yii - How do a make a link with a class if the current page is the same of the link?

For example, I have this structure :

<div id="menuPainelSuperior">
 <a href="<?= CController::createUrl('site/seu_cadastro')?>">Cadastro</a>
 <a href="<?= CController::createUrl('site/lista_clientes') ?>">Clientes</a>
 <a href="<?= CController::createUrl('site/projetos') ?>">Projetos</a>
 <a href="<?= CController::createUrl('site/orcamentos') ?>">Orçamentos</a>
</div>

if the current page is "domain.com/site/seu_cadastro" I want the first link then to have the following class : "activePageMenu"

Do anyone knows how to do it ?

Upvotes: 0

Views: 602

Answers (2)

bingjie2680
bingjie2680

Reputation: 7773

Have you considered using the build in CMenu to do this??

Update: Pitchinnate's answer is also a way to do that.

Upvotes: 1

Pitchinnate
Pitchinnate

Reputation: 7556

You could put an if statement on the link to see if the current controller and action is the same as the controller and action you are putting in the link. You get them like this:

$controller = Yii::app()->controller->id;
$action =  Yii::app()->controller->action->id;
$class = '';
if($controller == 'site' && $action == 'seu_cadastro') $class = 'activePageMenu';
echo CHtml::link('Cadastro',array('site/seu_cadastro'),array('class'=>$class));

Upvotes: 2

Related Questions