Page Title

I have a multi-lang site and want to have a customized getPageTitle function, but because _pageTitle is private I can’t do so. I want to have something like this:




public function getPageTitle()

{

	if($this->pageTitle !== null)

		return $this->_pageTitle;

	else

	{

		$name = ucfirst(basename($this->getId()));

		if($this->getAction() !== null && strcasecmp($this->getAction()->getId(), $this->defaultAction))

			return $this->_pageTitle = Yii::t(Yii::app()->name).' - '.Yii::t(ucfirst($this->getAction()->getId())).' '.Yii::t($name);

		else

			return $this->_pageTitle = Yii::t(Yii::app()->name).' - '.Yii::t($name);

	}

}



Edit: I don’t think I understood your proposal first time, please ignore this post until I find some solution.


$pageTitle=parent::getPageTitle();

$name=ucfirst(basename($this->getId()));

if($this->getAction()!==null && strcasecmp($this->getAction()->getId(),$this->defaultAction))

{

	if($pageTitle == Yii::app()->name.' - '.ucfirst($this->getAction()->getId()).' '.$name)

		return $this->_pageTitle = Yii::t(Yii::app()->name).' - '.Yii::t(ucfirst($this->getAction()->getId())).' '.Yii::t($name);

	else

		return $pageTitle;

}

else

{

	if($pageTitle == Yii::app()->name.' - '.$name)

		return $this->_pageTitle = Yii::t(Yii::app()->name).' - '.Yii::t($name);

	else

		return $pageTitle;

}

I didn’t find more elegant ways to accomplish this.

I’d propose for developers to add a method for retriving just the page title or null if it’s not set.

You can put it in a BaseController and override getPageTitle().

I know where to put it. I’m looking for a normal way to getPageTitle, not a way like pestaa proposed, since it’s a hack. And this is a minus for i18n of projects on Yii Framework. The most efficient way I found was overloading runAction method in BaseController:




public function runAction($action)

{

  $name = ucfirst(basename($this->getId()));

  if($action !== null && strcasecmp($action->getId(), $this->defaultAction))

    $this->pageTitle = Yii::t('title', Yii::app()->name).' - '.Yii::t('title', ucfirst($action->getId()).' '.$name);

  else

    $this->pageTitle = Yii::t('title', Yii::app()->name).' - '.Yii::t('title', $name);


  parent::runAction($action);

}