panza
(Admin)
August 21, 2009, 3:48pm
1
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);
}
}
pestaa
(Pestaa)
August 21, 2009, 4:22pm
2
Edit: I don’t think I understood your proposal first time, please ignore this post until I find some solution.
pestaa
(Pestaa)
August 21, 2009, 10:16pm
3
$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.
panza
(Admin)
August 23, 2009, 1:50pm
4
I’d propose for developers to add a method for retriving just the page title or null if it’s not set.
auxbuss
(Apps)
August 23, 2009, 7:33pm
5
You can put it in a BaseController and override getPageTitle().
panza
(Admin)
August 30, 2009, 8:46am
6
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);
}