GGshow reloaded GGshow reloaded

July 4, 2012

Zend Framework – View Helper – Placeholder

Example usage for place holders:

  • doctype()
  • headTitle()
  • headMeta()
  • headLink()
  • headStyle()
  • headScript()
  • inlineScript()

DOCTYPE

$view->doctype('HTML5');

Title

$view->headTitle('Site Title');
$view->headTitle('Site Title')
	->setSeparator(' :: ');

Meta

$view->headMeta()->appendName('keywords', 'page, keywords')
	->appendName('description', 'page description')
	->appendName('author', 'author name');

Refresh

$view->headMeta()->appendHttpEquiv('Refresh','30');

Redirect

$view->headMeta()->appendHttpEquiv('Refresh','0;url=http://www.ggshow.com');

Disable client side cache

$view->headMeta()->appendHttpEquiv('expires','-1')
	->appendHttpEquiv('pragma', 'no-cache')
	->appendHttpEquiv('Cache-Control', 'no-cache');

Setting content type and character set

$view->headMeta()->setCharset('utf-8');
$view->headMeta()->appendHttpEquiv('Content-Type', 'text/html; charset=UTF-8')
	->appendHttpEquiv('Content-Language', 'en-US');

Favicon with headLink

$view->headLink(array('rel' => 'favicon', 'href' => 'favicon.ico'),'PREPEND');
$view->headLink(array('rel' => 'shortcut icon', 'type' => 'image/x-icon', 'href' => 'favicon.ico'),'PREPEND');

CSS with headLink

$view->headLink()->appendStylesheet('style.css');

// appendStylesheet, prependStylesheet, setStylesheet, offsetStylesheet, appendAlternative, prependAlternative, setAlternative, offsetAlternative

CSS with headStyle

$view->headStyle->captureStart();
?>
body { margin: 0; }
<?php
$view->headStyle->captureEnd();

JavaScript

$view->headScript()->appendFile(“script.js”);
$view->headScript()->prependFile(“script.js”);
$view->inlineScript()->appendFile(“script.js”);
$this->headScript()->appendFile('script.js', 'text/javascript', array('conditional' => 'lt IE 7') );

Example output (within layout/view)

echo $this->headTitle();
echo $this->headLink();
echo $this->headMeta();
echo htmlentities($view->headMeta()->setPostfix("n"));

Example layout with placeholders

<?php echo $this->doctype() ?>
<html>
	<head>
		<?php echo $this->headTitle() ?>
		<?php echo $this->headScript() ?>
		<?php echo $this->headStyle() ?>
	</head>
	<body>
		<?php echo $this->layout()->content; ?>
		<?php echo $this->inlineScript() ?>
	</body>
</html>

February 3, 2012

Duplicated placeholder output

Issue:

  • Zend Framework placeholder (headTitle, headMeta, headLink, headScript, headStyle, inlineScript) creates twice or multiple title/meta/link/script tags in HTML.
  • Duplicated title/meta/link/script tags generated from Zend Framework placeholder.
  • e.g.:

    source:

    <?php echo $this->headScript()->appendFile("/scripts/global.js") ?>
    <?php echo $this->headScript()->appendFile("/scripts/main.js") ?>

    output:

    <script src="/scripts/global.js" type="text/javascript"></script>
    <script src="/scripts/global.js" type="text/javascript"></script>
    <script src="/scripts/main.js" type="text/javascript"></script>

Reason:

  • Echo will output all elements in a placeholder.

Solution:

  • Echo only once for each placeholder.
  • e.g.:
    <?php $this->headLink()->appendStylesheet('/styles/global.css') ?>
    <?php $this->headLink()->appendStylesheet('/styles/main.css') ?>
    <?php echo $this->headLink() ?>
    
    <?php $this->headScript()->appendFile("/scripts/global.js") ?>
    <?php $this->headScript()->appendFile("/scripts/main.js") ?>
    <?php echo $this->headScript() ?>
Filed under: CSS,JavaScript,PHP,Web,Zend Framework — Tags: , , — GG @ 11:15 am

© 2024 GGSHOW | Powered by WordPress