|
List Info
Thread: Does headScript() Work Correctly?
|
|
| Does headScript() Work Correctly? |

|
2008-05-29 16:25:59 |
The headScript() and headLink() helpers are purportedly used
to collect
additions throughout the page-building process, then output
the results as a
group.
>
> You may specify a new script tag at any time. As noted
above, these may
> be links to outside resource files or scripts
theselves.
>
> <?php // adding scripts
>
$this->headScript()->appendFile('/js/prototype.js')
> ->appendScript($onloadScript);
> ?>
>
So you'd think by the description in the manual and by the
name of the
helper, that it's doing something special to inject the
results into the
header of the output.
But I see no evidence of that after attempting to use it.
These "head"
helpers are nothing of the sort. They're only containers
that get echoed to
the output stream as soon as the reference is encountered:
<?= $this->headScript() ?>
<?= $this->headLink() ?>
How is that useful? The calls to
$this->headScript()->prependXXX() are
processed later in the .phtml file, so that content never
gets rendered?
If this mechanism worked as expected, all the calls to these
headXXX helpers
in all views of the layout would be collected and
automatically injected
into the <head> section upon completion of the output,
but before being sent
to the client.
No special code would be needed in the application's layout
or view to
output it (which just breaks the mechanism, as we have seen,
because the
*current* contents of the headXXX helper get output, rather
than the
*future* contents as expected).
What am I missing? Is there an easy way to tweak ZF to get
the expected
behavior?
--
View this message in context: http://www.nabble.com/Does-h
eadScript%28%29-Work-Correctly--tp17546465p17546465.html
Sent from the Zend Framework mailing list archive at
Nabble.com.
|
|
| Re: Does headScript() Work Correctly? |

|
2008-05-29 16:37:35 |
|
The layout should really be the last thing that gets rendered,
immediately before the response is sent.
Thus, as your templates go around adding to head* helpers... the layout
parses after those actions do and the resulting containers are already
full when the echo is encountered in the layout.
Make sense?
Kevin McArthur
bfoust wrote:
talk.nabble.com" type="cite">
The headScript() and headLink() helpers are purportedly used to collect
additions throughout the page-building process, then output the results as a
group.
You may specify a new script tag at any time. As noted above, these may
be links to outside resource files or scripts theselves.
<?php // adding scripts
$this->headScript()->appendFile('/js/prototype.js')
->appendScript($onloadScript);
?>
So you'd think by the description in the manual and by the name of the
helper, that it's doing something special to inject the results into the
header of the output.
But I see no evidence of that after attempting to use it. These "head"
helpers are nothing of the sort. They're only containers that get echoed to
the output stream as soon as the reference is encountered:
<?= $this->headScript() ?>
<?= $this->headLink() ?>
How is that useful? The calls to $this->headScript()->prependXXX() are
processed later in the .phtml file, so that content never gets rendered?
If this mechanism worked as expected, all the calls to these headXXX helpers
in all views of the layout would be collected and automatically injected
into the <head> section upon completion of the output, but before being sent
to the client.
No special code would be needed in the application's layout or view to
output it (which just breaks the mechanism, as we have seen, because the
*current* contents of the headXXX helper get output, rather than the
*future* contents as expected).
What am I missing? Is there an easy way to tweak ZF to get the expected
behavior?
--
Kevin McArthur
StormTide Digital Studios Inc.
Author of the recently published book, "Pro PHP"
http://www.stormtide.ca
|
| Re: Does headScript() Work Correctly? |

|
2008-05-29 16:49:06 |
Kevin McArthur-2 wrote:
>
> The layout should really be the last thing that gets
rendered,
> immediately before the response is sent.
>
> Thus, as your templates go around adding to head*
helpers... the layout
> parses after those actions do and the resulting
containers are already
> full when the echo is encountered in the layout.
>
That would be nice.
But it's not working that way. Is there a better way to have
the layout
build it's components than using the partial helper? I'm
rendering the
layout like this:
...
<?= $this->headScript() ?>
<?= $this->headLink() ?>
<?= $this->headStyle() ?>
</head>
<body>
<?= $this->partial('partials/Header.phtml')
?>
<?= $this->layout()->content ?>
<?= $this->partial('partials/Footer.phtml')
?>
</body>
And the way it's working now, the
$this->headScript()->captureStart(), and
all the other headXXX() method calls are storing data that
never gets output
(unless I move those "<?= $this->headLink()
?>" lines to the bottom of the
layout file.
--
View this message in context: http://www.nabble.com/Does-h
eadScript%28%29-Work-Correctly--tp17546465p17546834.html
Sent from the Zend Framework mailing list archive at
Nabble.com.
|
|
| Re: Does headScript() Work Correctly? |

|
2008-05-29 16:53:28 |
I've used headScript with great success, typically, I'll
have a layout
define my js library and then have each template insert
it's
particular:
layout.phtml:
<?
$this->headScript()->prependFile('/js/jquery.js');
?>
... further down ...
<?= $this->headScript() ?>
search.phtml:
<?
$this->headScript()->appendFile('/js/search.js');
?>
This correctly generates all of my <script> tags.
- jake
On Thu, May 29, 2008 at 5:37 PM, Kevin McArthur
<kevin stormtide.ca> wrote:
> The layout should really be the last thing that gets
rendered, immediately
> before the response is sent.
>
> Thus, as your templates go around adding to head*
helpers... the layout
> parses after those actions do and the resulting
containers are already full
> when the echo is encountered in the layout.
>
> Make sense?
>
> Kevin McArthur
>
> bfoust wrote:
>
> The headScript() and headLink() helpers are purportedly
used to collect
> additions throughout the page-building process, then
output the results as a
> group.
>
>
>
>
>
> You may specify a new script tag at any time. As noted
above, these may
> be links to outside resource files or scripts
theselves.
>
> <?php // adding scripts
>
$this->headScript()->appendFile('/js/prototype.js')
> ->appendScript($onloadScript);
> ?>
>
>
>
> So you'd think by the description in the manual and by
the name of the
> helper, that it's doing something special to inject the
results into the
> header of the output.
>
> But I see no evidence of that after attempting to use
it. These "head"
> helpers are nothing of the sort. They're only
containers that get echoed to
> the output stream as soon as the reference is
encountered:
>
> <?= $this->headScript() ?>
> <?= $this->headLink() ?>
>
> How is that useful? The calls to
$this->headScript()->prependXXX() are
> processed later in the .phtml file, so that content
never gets rendered?
>
> If this mechanism worked as expected, all the calls to
these headXXX helpers
> in all views of the layout would be collected and
automatically injected
> into the <head> section upon completion of the
output, but before being sent
> to the client.
>
> No special code would be needed in the application's
layout or view to
> output it (which just breaks the mechanism, as we have
seen, because the
> *current* contents of the headXXX helper get output,
rather than the
> *future* contents as expected).
>
> What am I missing? Is there an easy way to tweak ZF to
get the expected
> behavior?
>
>
>
> --
>
> Kevin McArthur
>
> StormTide Digital Studios Inc.
> Author of the recently published book, "Pro
PHP"
> http://www.stormtide.ca
>
|
|
| Re: Does headScript() Work Correctly? |

|
2008-05-29 17:15:47 |
|
Are the calls to headScript in your layout partials or in your action
views?
Those partials will be processed as the layout is rendering.. If thats
not your intent try the action helper instead of the partial which
_should_ order correctly.
Kevin
bfoust wrote:
talk.nabble.com" type="cite">
Kevin McArthur-2 wrote:
The layout should really be the last thing that gets rendered,
immediately before the response is sent.
Thus, as your templates go around adding to head* helpers... the layout
parses after those actions do and the resulting containers are already
full when the echo is encountered in the layout.
That would be nice.
But it's not working that way. Is there a better way to have the layout
build it's components than using the partial helper? I'm rendering the
layout like this:
...
<?= $this->headScript() ?>
<?= $this->headLink() ?>
<?= $this->headStyle() ?>
</head>
<body>
<?= $this->partial('partials/Header.phtml') ?>
<?= $this->layout()->content ?>
<?= $this->partial('partials/Footer.phtml') ?>
</body>
And the way it's working now, the $this->headScript()->captureStart(), and
all the other headXXX() method calls are storing data that never gets output
(unless I move those "<?= $this->headLink() ?>" lines to the bottom of the
layout file.
--
Kevin McArthur
StormTide Digital Studios Inc.
Author of the recently published book, "Pro PHP"
http://www.stormtide.ca
|
| Re: Does headScript() Work Correctly? |

|
2008-05-29 17:17:42 |
Easily fixed. Just retrieve your output from your partials
before you
process your headXXX helpers.
<? $header = $this->partial('partials/Header.phtml')
?>
<? $footer = $this->partial('partials/Footer.phtml')
?>
<head>
<?= $this->headScript() ?>
<?= $this->headLink() ?>
<?= $this->headStyle() ?>
</head>
<body>
<?= $header ?>
<?= $this->layout()->content ?>
<?= $footer ?>
</body>
Cheers!
>
>
>
> Kevin McArthur-2 wrote:
>>
>> The layout should really be the last thing that
gets rendered,
>> immediately before the response is sent.
>>
>> Thus, as your templates go around adding to head*
helpers... the
>> layout
>> parses after those actions do and the resulting
containers are
>> already
>> full when the echo is encountered in the layout.
>>
>
> That would be nice.
>
> But it's not working that way. Is there a better way to
have the
> layout
> build it's components than using the partial helper?
I'm rendering the
> layout like this:
>
> ...
> <?= $this->headScript() ?>
> <?= $this->headLink() ?>
> <?= $this->headStyle() ?>
> </head>
>
> <body>
> <?=
$this->partial('partials/Header.phtml') ?>
>
> <?= $this->layout()->content ?>
>
> <?=
$this->partial('partials/Footer.phtml') ?>
> </body>
>
>
> And the way it's working now, the
$this->headScript()-
> >captureStart(), and
> all the other headXXX() method calls are storing data
that never
> gets output
> (unless I move those "<?= $this->headLink()
?>" lines to the bottom
> of the
> layout file.
>
> --
> View this message in context: http://www.nabble.com/Does-h
eadScript%28%29-Work-Correctly--tp17546465p17546834.html
> Sent from the Zend Framework mailing list archive at
Nabble.com.
>
--
Simon Mundy | Director | PEPTOLAB
""" " ""
"""""" ""
"" """""""
" "" """"" "
""""" "
"""""" "" "
202/258 Flinders Lane | Melbourne | Victoria | Australia |
3000
Voice +61 (0) 3 9654 4324 | Mobile 0438 046 061 | Fax +61
(0) 3 9654
4124
http://www.peptolab.com
|
|
| Re: Does headScript() Work Correctly? |

|
2008-05-29 21:09:34 |
Simon Mundy wrote:
>
>> I wish this feature had been better thought out by
the ZF team, but
>> I really appreciate your suggested workaround.
>
> I'm not quite sure what you mean by this - how would
you see the view
> helpers working any better?
>
It's a great idea to be able to send content (links, script
code, etc.) into
the <head> section from any sub-view of a layout (or
view). But it was
disappointing to find that this is not what happens when the
layout renders
its subviews with "include", "render",
or "partial".
Is there a clean way of saying, "Place the output of
view X.phtml "
and then only *after* all the views have been run, the
content of all the
headXXX() helpers (headScript, headLink, etc.) is injected
into the <head>
section of the output?
--
View this message in context: http://www.nabble.com/Does-h
eadScript%28%29-Work-Correctly--tp17546465p17550229.html
Sent from the Zend Framework mailing list archive at
Nabble.com.
|
|
| Re: Does headScript() Work Correctly? |

|
2008-05-29 23:17:41 |
I've personally come into the habit of putting headScript
echos as the
*last* thing in the layout, just before closing of the body
tag, and
inlineScript immediately after headScript.
It works just like in the head tag, but according to various
people
it should speed up page loading as any external resources
will not
be loaded before script tags that are encountered before it
are loaded.
It also fixes any issues with calling
headScript/inlineScript in the
layout.phtml itself, or in partials called by layout.phtml
before the end
of the body.
Maybe you could do this as well?
On Fri, 30 May 2008 05:09:34 +0300, bfoust <phpfoust gmail.com> wrote:
>
>
>
> Simon Mundy wrote:
>>
>>> I wish this feature had been better thought out
by the ZF team, but
>>> I really appreciate your suggested workaround.
>>
>> I'm not quite sure what you mean by this - how
would you see the view
>> helpers working any better?
>>
>
> It's a great idea to be able to send content (links,
script code, etc.)
> into
> the <head> section from any sub-view of a layout
(or view). But it was
> disappointing to find that this is not what happens
when the layout
> renders
> its subviews with "include",
"render", or "partial".
>
> Is there a clean way of saying, "Place the output
of view X.phtml "
> and then only *after* all the views have been run, the
content of all the
> headXXX() helpers (headScript, headLink, etc.) is
injected into the
> <head>
> section of the output?
|
|
| Re: Does headScript() Work Correctly? |

|
2008-05-30 01:12:02 |
Jani Hartikainen wrote:
>
> I've personally come into the habit of putting
headScript echos as the
> *last* thing in the layout, just before closing of the
body tag, and
> inlineScript immediately after headScript.
>
> It works just like in the head tag, but according to
various people
> it should speed up page loading as any external
resources will not
> be loaded before script tags that are encountered
before it are loaded.
>
> It also fixes any issues with calling
headScript/inlineScript in the
> layout.phtml itself, or in partials called by
layout.phtml before the end
> of the body.
>
> Maybe you could do this as well?
>
Yes, that works. Thank you.
Seems a little convoluted, though to have to structure your
pages that way
-- given the name of the helpers, it would only make sense
if all this was
handled automatically by the framework.
I tried using a filter, as Mike Coakley suggested
(http:
//framework.zend.com/issues/browse/ZF-3282), but
something strange
happens when you use the following code at the top of the
layout to add the
filter:
$filter = new Filter_HeadHelperFix();
$filter->setView($this);
$this->addFilter($filter);
Zend_View_Abstract::addFilter() doesn't store the filter
properly and
Zend_View later causes and exception when trying to use it.
This must not be
the correct way to add a filter to a layout?
The idea is to just place ""
where you want it placed,
and this filter will place it there after the page has been
generated. Here
is the Filter:
class Filter_HeadHelperFix implements Zend_Filter_Interface
{
protected $_view;
public function setView ($view)
{
$this->_view = $view;
}
public function filter ($pageOutput)
{
// $injectMarkup = join("n",
array($this->_view->headLink(),
$this->_view->headScript()));
// $pageOutput =
preg_replace("//",
$injectMarkup,
$pageOutput);
return $pageOutput;
}
}
--
View this message in context: http://www.nabble.com/Does-h
eadScript%28%29-Work-Correctly--tp17546465p17552280.html
Sent from the Zend Framework mailing list archive at
Nabble.com.
|
|
| Re: Does headScript() Work Correctly? |

|
2008-05-30 03:09:05 |
Thank you for all the suggestions.
In my quest to find an automated solution for headXXX()
helpers to insert
their content transparently, I arrived at the post-gen
filter method first
mentioned by Mike. Hopefully, a completely automated
solution will arise,
but for now, it requires only this one method call be added
to the top of
your layout:
$this->addFilter('HeadHelperOutput');
(and that the layout contain head and body tags)
And to place this file named 'HeadHelperOutput' in the
views/filters/ dir:
(note: there may be a way to change the default prefix
'Zend_View_Filter' to
just 'Filter', in which case both the file and class could
be renamed.)
<?php
/**
* $Id: $
* View/Layout output filter (inserts the headXXX() helper
output in the
<head> section
* where it belongs).
*
* author bfoust
* version 1
*/
/**
* Place the headXXX() helper output in the head section of
the output.
* (Actually, just replaces the tag,
wherever that is
in the file.
*
* Usage:
*
*/
class Zend_View_Filter_HeadHelperOutput implements
Zend_Filter_Interface
{
protected $_view;
public function setView ($view) // called by Zend_View
before the filter
is called.
{
$this->_view = $view;
}
/**
* Replace in the given text with the
content of the
headXXX() view helpers.
*
* param string $pageOutput
* return string html markup with
"" replaced
*/
public function filter ($pageOutput)
{
$injectMarkup = join("n", array(
$this->_view->headTitle()
, $this->_view->headMeta()
, $this->_view->headLink()
, $this->_view->headStyle()
, "</head>"
));
$pageOutput = preg_replace("|</head>|i",
$injectMarkup, $pageOutput);
//
// Place the script at the end (styles, however, need to
be seen before
// page can be displayed, anyway, so leave those in the
head section)
//
$injectMarkup = join("n", array(
$this->_view->headScript()
, "</body>"
));
$pageOutput = preg_replace("|</body>|i",
$injectMarkup, $pageOutput);
return $pageOutput;
}
}
?>
--
View this message in context: http://www.nabble.com/Does-h
eadScript%28%29-Work-Correctly--tp17546465p17553680.html
Sent from the Zend Framework mailing list archive at
Nabble.com.
|
|
[1-10]
|
|