|
|
|
Bart,
I came across this as well. There really isn't a great way to deal with this directly as the calls to $this->headLink() are actual calls to PHP code which is going to obviously execute right then. Also, it is working as designed, simply the Layout changes the workflow for the overall content rendering. While there are MANY ways to program around this using the current system here is the way I've gotten around this in my code (of course this played into my overall method of building view scripts also): In your Layout script create something like this: <?= $this->doctype('XHTML1_STRICT') ?> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <?= $this->headTitle() ?> <?= $this->headMeta() ?> {HEADSCRIPT} {HEADLINK} <?= $this->headStyle() ?> </head> <body> <?= $this->layout()->content ?> <h2> layout content </h2> <?= $this->glow('My Account') ?> <?= $this->inlineScript() ?> </body> </html> Then create a filter that looks for the {HEADSCRIPT} and {HEADLINK} tags and calls the appropriate view helpers to render the actual content. Since filters are run after the view is rendered you should be good to go. I will also note you should only register the filters with the view associated with the layout instance. Here is an example of how a filter will look: class MyApp_Wiki_Filter_HeadLink implements Zend_Filter_Interface { protected $_view; public function setView($view) { $this->_view = $view; } public function filter($value) { if (preg_match("/{HEADLINK}/i", $value, $match)) { $replaceValue = $this->_view->headLink(); $value = preg_replace("/" . preg_quote($match[0], "/") . "/", $replaceValue, $value); } return $value; } } Hope that helps. Thanks, Mike The same thing happens when using the Dojo View Helper ( $this->dojo() ) and/or $this->headScript()
I hate to say it, folks, but this is just the way things work, and it's a matter of concurrency.
When you echo the view helper, it takes the current object state and renders it to a string notation. What you are presenting will not work because you're rendering the view helper, and then expecting that later calls will affect what you've already rendered. To get what you're looking for, we would need to compile templates, and have one template that is considered a master, and would still need to somehow indicate an order of operations for controls – i.e., you cannot echo a placeholder completely until all other updates are performed. You can actually update the placeholders within the layout script... so long as you do so prior to rendering the placeholder. So, in Bart's original example, if he had captured the results of $this->glow() before the <head> section, and then rendered the captured content later, it would work: <? $glow = $this->glow('My Account'); ?> <?= $this->doctype() ?> <html> <head> ... <?= $this->headLink() ?> <?= $this->headScript() ?> ... </head> <body> ... <?= $glow ?> ... </body> </html> This technique will work for all placeholders that you wish to use directly within your layout scripts. |
||||||||||||||||||||||||||||||||||||||||||||||||||
<?= $this->doctype('XHTML1_STRICT') ?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<?= $this->glow('My Account') ?>
<?= $this->headTitle() ?>
<?= $this->headMeta() ?>
<?= $this->headScript() ?>
<?= $this->headLink() ?>
<?= $this->headStyle() ?>
</head>
<body>
<?= $this->layout()->content ?>
<h2> layout content </h2>
<?= $this->inlineScript() ?>
</body>
</html>
Meaning if you append script before calling <?= $this->headScript() ?>, they will be appended. Again, this defeats the purpose (as you're not going to write the html contents in your HEAD section off course...)