ページタイトルの設定をするビューヘルパーのHeadTitleについていろいろ。
基本
headTitleでページタイトルを設定。
controller:
$this->view->headTitle('Site Name');
Output:
<title>Site Name</title>
セパレーターの設定
setSeparatorを使用。
$this->view->headTitle()->setSeparator(' | ');
setSeparatorで設定した文字列はZend_View_Helper_HeadTitleクラス内でセットされたページタイトルの配列をimplodeするために使われるので、
タイトルが複数追加された場合はタイトル間全てにsetSeparatorの文字列が追加される。
$this->view->headTitle('Site Name');
$this->view->headTitle()->setSeparator(' | ');
$this->view->headTitle('Page Title');
Output:
<title>Site Name | Page Name</title>
append & prepend
headTitleで設定したタイトルは先に追加したもの順に左から並ぶ。
appendとprependを使えばタイトルの順番を入れ替えることが出来る。
prependは前に追加
$this->view->headTitle('Site Name');
$this->view->headTitle()->setSeparator(' | ');
$this->view->headTitle()->prepend('Page Name');
appendは後ろに追加
$this->view->headTitle('Page Title');
$this->view->headTitle()->setSeparator(' | ');
$this->view->headTitle()->append('Site Name');
Output:
<title>Page Title | Site Name</title>
setPrefix & setPostfix
名前の通りタイトル内の接頭語と接尾語の設定が出来るメソッド。
このメソッドはサイト名を先頭或いは最後尾に絶対配置したいという時に便利だと思う。
$this->view->headTitle()->setPostfix(' | Site Name');
$this->view->headTitle()->setSeparator(' « ');
$this->view->headTitle('Category Name');
$this->view->headTitle()->prepend('Page Title');
Output:
<title>Page Title « Category Name | Site Name</title>
$this->view->headTitle()->setPrefix('Site Name - ');
$this->view->headTitle()->setSeparator(' » ');
$this->view->headTitle('Category Name');
$this->view->headTitle('Page Title');
Output:
<title>Site Name - Category Name » Page Title</title>
ActionControllerでの設定例
Bootstrap.php
$view->headTitle()->setPostfix(' | Site Name');
$view->headTitle()->setSeparator(' « ');
application/congrollers/FooControlelr.php
class FooController extends Zend_Controller_Action
{
public function init()
{
$this->view->headTitle('Category Name');
}
public function postDispatch()
{
$this->view->headTitle()->prepend($this->view->pageTitle);
}
public function indexAction()
{
$this->view->pageTitle = 'Page Title';
}
}
index.phtml
<h2><?php echo $this->escape($this->pageTitle);?></h2>
Action_Helperを作る例
Bootstrap.php
protected function _initActionHelper()
{
Zend_Controller_Action_HelperBroker::addPath(APPLICATION_PATH .'/controllers/helpers');
}
application/controllers/helper/PageTitle.php
class Zend_Controller_Action_Helper_PageTitle extends Zend_Controller_Action_Helper_Abstract
{
public function init()
{
$this->controller = $this->getActionController();
$this->controller->view->headTitle()->setSeparator(' « ');
$this->controller->view->headTitle()->setPostfix(' | Site Name');
}
public function direct($str)
{
return $this->setTitle($str);
}
public function setTitle($str)
{
$this->controller->view->pageTitle = $str;
}
public function postDispatch()
{
$this->controller->view->headTitle()->prepend($this->controller->view->pageTitle);
}
}
application/congrollers/GuestbookController.php
public function indexAction()
{
$this->_helper->pageTitle('Page Title');
}
「[ZF] Zend_View HeadTitle method tips」への2件のフィードバック