我以我的风格解决了这个问题。也许它不是完美的,但它解决了我的最大目的。
我创建了一个名为 getMyPageTitle() 的辅助函数。
在那里我写下了页面标题的逻辑。
逻辑的基本部分是我在配置文件中假设了一个默认标题。
<?php
function getMyPageTitle(){
$title = '';
$ci = &get_instance();
$default_title = 'My Site';// you can load it from a config file,and that is better option
// get the view name
$view = $this->uri->segment(2);
// check if controller set a page tile
/* If you want to set a page title for a particular page then always
* named that variable 'pageTitle' as $data['pageTitle'] = 'My page title | For this page'
*/
global $pageTitle;
if($pageTitle){// the controller sets the page title
$title = $pageTitle;
}elseif($view !== ''){//you can include your view name in the page title,,and the view name is not blanks
$title = $default_title.' | '. $view;
}/**
* You can write several else if , before or after this or
* can change the total logic as you want
*/
else{
$title = $default_title;
}
return $title;
}
?>
在视图中使用这个
<title><?= getMyPageTitle()?></title>
我认为这解决了目的。