3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\View\Helper
;
12 use Zend\I18n\Translator\Translator
;
13 use Zend\I18n\Translator\TranslatorAwareInterface
;
14 use Zend\View\Exception
;
17 * Helper for setting and retrieving title element for HTML head
19 class HeadTitle
extends Placeholder\Container\AbstractStandalone
implements
20 TranslatorAwareInterface
23 * Registry key for placeholder
27 protected $regKey = 'Zend_View_Helper_HeadTitle';
30 * Default title rendering order (i.e. order in which each title attached)
34 protected $defaultAttachOrder = null;
37 * Translator (optional)
41 protected $translator;
44 * Translator text domain (optional)
48 protected $translatorTextDomain = 'default';
51 * Whether translator should be used
55 protected $translatorEnabled = true;
58 * Retrieve placeholder for title element and optionally set state
60 * @param string $title
61 * @param string $setType
64 public function __invoke($title = null, $setType = null)
66 if (null === $setType) {
67 $setType = (null === $this->getDefaultAttachOrder())
68 ? Placeholder\Container\AbstractContainer
::APPEND
69 : $this->getDefaultAttachOrder();
72 $title = (string) $title;
74 if ($setType == Placeholder\Container\AbstractContainer
::SET
) {
76 } elseif ($setType == Placeholder\Container\AbstractContainer
::PREPEND
) {
77 $this->prepend($title);
79 $this->append($title);
87 * Render title (wrapped by title tag)
89 * @param string|null $indent
92 public function toString($indent = null)
94 $indent = (null !== $indent)
95 ?
$this->getWhitespace($indent)
98 $output = $this->renderTitle();
100 return $indent . '<title>' . $output . '</title>';
104 * Render title string
108 public function renderTitle()
112 if (null !== ($translator = $this->getTranslator())) {
113 foreach ($this as $item) {
114 $items[] = $translator->translate(
115 $item, $this->getTranslatorTextDomain()
119 foreach ($this as $item) {
124 $separator = $this->getSeparator();
127 $prefix = $this->getPrefix();
132 $output .= implode($separator, $items);
134 $postfix = $this->getPostfix();
139 $output = ($this->autoEscape
) ?
$this->escape($output) : $output;
145 * Set a default order to add titles
147 * @param string $setType
148 * @throws Exception\DomainException
151 public function setDefaultAttachOrder($setType)
153 if (!in_array($setType, array(
154 Placeholder\Container\AbstractContainer
::APPEND
,
155 Placeholder\Container\AbstractContainer
::SET
,
156 Placeholder\Container\AbstractContainer
::PREPEND
158 throw new Exception\
DomainException(
159 "You must use a valid attach order: 'PREPEND', 'APPEND' or 'SET'"
162 $this->defaultAttachOrder
= $setType;
168 * Get the default attach order, if any.
172 public function getDefaultAttachOrder()
174 return $this->defaultAttachOrder
;
177 // Translator methods - Good candidate to refactor as a trait with PHP 5.4
180 * Sets translator to use in helper
182 * @param Translator $translator [optional] translator.
183 * Default is null, which sets no translator.
184 * @param string $textDomain [optional] text domain
185 * Default is null, which skips setTranslatorTextDomain
188 public function setTranslator(Translator
$translator = null, $textDomain = null)
190 $this->translator
= $translator;
191 if (null !== $textDomain) {
192 $this->setTranslatorTextDomain($textDomain);
198 * Returns translator used in helper
200 * @return Translator|null
202 public function getTranslator()
204 if (! $this->isTranslatorEnabled()) {
208 return $this->translator
;
212 * Checks if the helper has a translator
216 public function hasTranslator()
218 return (bool) $this->getTranslator();
222 * Sets whether translator is enabled and should be used
224 * @param bool $enabled [optional] whether translator should be used.
228 public function setTranslatorEnabled($enabled = true)
230 $this->translatorEnabled
= (bool) $enabled;
235 * Returns whether translator is enabled and should be used
239 public function isTranslatorEnabled()
241 return $this->translatorEnabled
;
245 * Set translation text domain
247 * @param string $textDomain
250 public function setTranslatorTextDomain($textDomain = 'default')
252 $this->translatorTextDomain
= $textDomain;
257 * Return the translation text domain
261 public function getTranslatorTextDomain()
263 return $this->translatorTextDomain
;