yii2: Getter function for View::$title so that Pjax requests don't change the page title.
As already mentioned yesterday in this old issue I would prefer to have a View::getTitle() function which returns the Views $title attribute.
And that this function is used where the title of the View is accessed e.g. in class Pjax.
Easy example: in my layout/main.php I have this code to add the application name to the page title:
<title><?= $this->title ? Html::encode(Yii::$app->name." | $this->title") : (Yii::$app->name.' | Home') ?></title>
And in the views something like:
$this->title = Yii::t('app', MyModel::label(2));
// or something else
$this->title = 'Some title';
With the code in the layout view file the application name is added to the title => Fine.
BUT on every Pjax request only the normal view title is used (so without the application name) and the page title is changed incorrectly. This is because only the $view->title is used.
(code from class Pjax)
if ($view->title !== null) {
echo Html::tag('title', Html::encode($view->title));
}
Do you have another suggestion how this issue can be fixed?
I mean other then extend the yii\web\View and yii\widget\Pjax classes as overwrite/extend them with functions?
Which would be cumbersome because in the pjax class the title is directly echoed in pjax::init so an setting of the title before parent::init() call would work. I would have to copy and paste the complete init function and change one line $view->title to $view->getTitle() and have to check for changes on every yii update to also re-do the changes to my extended pjax class.
Of course I could change every view where i set the title and add the application name directly. But in my opinion a getter (maybe also a setter) function for the title would be the easiest.
If the View Class has a getTitle() function (and this function would be used instead of $view->title) we could just overwrite the Class via components and extend it with e.g.
public function getTitle()
{
if ($this->title === null) {
return Yii::$app->name;
}
return Yii::$app->name .' | '.$this->title;
}
Then the Application Name (or something else) could be easily added to page and wouldn’t change after pjax requests.
This would also help if the title comes from the db (e.g. multi language) or would be different for e.g. mobile devices.
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Comments: 21 (19 by maintainers)
I use quick and simple solution to solve this problem.
As Pjax is Ajax request, you can add your site name to the title when request isAjax. I know this is not the best solution, but it might help for some people.
@SilverFire so you mean I should set the title in “every view” to get the application Name attached to it?
You also don’t understand it correctly.
There isn’t a
getTitle()function in thePjaxclass. There is also none in theViewClass!My request was that a getTitle function is added to the
ViewClass and that this function is used in thePjaxclassinit()function so this lines are changedI only have in my /layout/main.php
and in my rendered view e.g. the following title and a grid with pjax enabled.
So on the normal render the page title is “my application | test title” and after I sort the grid via pjax the title is only “test title”.
And as i have written above the title is echoed directly in the
Pjax::initso the only solution is to completely rewrite thePjax::initfunction and this could cause trouble if the source is changed in future versions.Please test it out for yourself and you will see what I mean. It was suggested to if we want to add the application name to the html page title to do this in our layout/main.php https://github.com/yiisoft/yii2/issues/662