
ADDTOZONE
%ADDTOZONE{
"zone"
...
}%
Zones are specific places in the output HTML that are marked by calls to the RENDERZONE macro. Zones are used to collect various content together, such as Javascript and CSS, that must be included in the output HTML in a specific order, and in a specific place.
You may create as many zones in addition to the standard head and script zones as you like. Interesting use cases in wiki applications:
id parameter to zone, which will later be expanded with RENDERZONE. id identifiers are unique within the zone that they are added to. An ADDTOZONE call may ensure that its content appears after the content of some other ADDTOZONE calls by specifying their ids in the requires parameter. requires may only list ids within the specified zone, except for the special case of head and script zones when {MergeHeadAndScriptZones} is set (read more).
Parameters:
requires parameter to resolve the ordering of dependencies within a zone. It does not work across zones. If you have an id in requires that cannot be resolved during sorting, then RENDERZONE will generate an HTML comment to mark the problem.
How to use the
Web browsers generally process the HTML on a page from top to bottom. When a
| ||||
| Deleted: | ||||||||
| < < | ||||||||
ADDTOZONE
%ADDTOZONE{
"zone"
...
}%
| ||||||||
| Added: | ||||||||
| > > |
Zones are specific places in the output HTML that are marked by calls to the RENDERZONE macro. Zones are used to collect various content together, such as Javascript and CSS, that must be included in the output HTML in a specific order, and in a specific place.
You may create as many zones in addition to the standard head and script zones as you like. Interesting use cases in wiki applications:
id parameter to zone, which will later be expanded with RENDERZONE. id identifiers are unique within the zone that they are added to. An ADDTOZONE call may ensure that its content appears after the content of some other ADDTOZONE calls by specifying their ids in the requires parameter. requires may only list ids within the specified zone, except for the special case of head and script zones when {MergeHeadAndScriptZones} is set (read more). | |||||||
| Parameters: | ||||||||
| Changed: | ||||||||
| < < |
| |||||||
| > > |
| |||||||
| Deleted: | ||||||||
| < < |
The only zones guaranteed to exist are head and script. Defaults to head. | |||||||
| ||||||||
| Changed: | ||||||||
| < < |
| |||||||
| > > |
| |||||||
| ||||||||
| Changed: | ||||||||
| < < |
| |||||||
| > > |
| |||||||
| ||||||||
| Changed: | ||||||||
| < < | defaults to the default section between STARTINCLUDE and STOPINCLUDE. | |||||||
| > > |
defaults to the default section between STARTINCLUDE and STOPINCLUDE. | |||||||
| Added: | ||||||||
| > > |
| |||||||
| Changed: | ||||||||
| < < |
What is a "Zone"? | |||||||
| > > |
Note: Foswiki uses the requires parameter to resolve the ordering of dependencies within a zone. It does not work across zones. If you have an id in requires that cannot be resolved during sorting, then RENDERZONE will generate an HTML comment to mark the problem. | |||||||
| Deleted: | ||||||||
| < < | Zones are specific places in the output HTML that are marked by calls to the RENDERZONE macro. Zones are used to collect various content together, such as Javascript and CSS, that must be included in the output HTML in a specific order, and in a specific place. | |||||||
| Changed: | ||||||||
| < < |
There are two special zones called head and script. The head zone is rendered
as part of the HTML head section. It is the catch-all container for any content supposed | |||||||
| > > |
How to use the | |||||||
| Deleted: | ||||||||
| < < |
to be placed into the HTML head section, except Javascript, which is collected in the
script zone. | |||||||
| Changed: | ||||||||
| < < |
All Javascript must always be added to the script zone exclusively, in order to
grant ordering constraints among scripts are resolved properly. Never add Javascript to
the head zone -- never add non-Javascript content to the script zone. | |||||||
| > > |
Web browsers generally process the HTML on a page from top to bottom. When a
<script> tag is encountered with a URL to some Javascript file,
processing of the page will stop while the file is fetched and executed before | |||||||
| Added: | ||||||||
| > > |
continuing. When a page makes heavy use of Javascript you can get a "blank
screen" effect in the browser while each script is downloaded. To avoid this
effect, <script> tags can be moved to the end of the HTML page, so that
the user may view the page content while scripts are being loaded. | |||||||
| Changed: | ||||||||
| < < |
Both zones are added to the HTML head section automatically just before the
closing </head> tag as if they were specified explicitly in the skin templates using:
<head> ... | |||||||
| > > |
Foswiki makes this move possible by providing the head and script zones.
These are automatic zones - they do not require a corresponding RENDERZONE.
| |||||||
| Deleted: | ||||||||
| < < | ||||||||
| Changed: | ||||||||
| < < |
You may create as many zones in addition to the standard head and script
zones as you like. For any non-standard zone specified in
ADDTOZONE you will also need to provide an appropriate
RENDERZONE. | |||||||
| > > |
Notionally the head and script zones correspond to a point just before the
HTML </HEAD> tag. Normally you should add CSS (and other HTML
<HEAD> content, such as <META>) to the head zone, and
Javascript <script> markup to the script zone. The setting | |||||||
| Added: | ||||||||
| > > | {MergeHeadAndScriptZones} in Configure controls what happens when RENDERZONE is called. | |||||||
| Changed: | ||||||||
| < < |
Interesting use cases in wiki applications:
| |||||||
| > > |
Normally, dependencies between the individual ADDTOZONE
statements are resolved within each zone. However, if
{MergeHeadAndScriptZones} is
enabled, then head content which requires an id that only exists in | |||||||
| Added: | ||||||||
| > > |
script (and vice-versa) will be re-ordered to satisfy any dependency. | |||||||
| Changed: | ||||||||
| < < |
Adding content to a zoneADDTOZONE adds content to a zone identified with theid parameter.
An id identifier is unique within the zone that they are added to.
When the same id is used in multiple calls to ADDTOZONE the
last call will win, that is previous content of the same id will be overwritten. | |||||||
| > > |
| |||||||
| Changed: | ||||||||
| < < |
Enforcing a linear order of content within a zone | |||||||
| > > |
Workign with | |||||||
| Deleted: | ||||||||
| < < |
An ADDTOZONE call may ensure that its content appears after the
content of some other ADDTOZONE calls by specifying their ids in
the requires parameter. The requires parameter constraints the linear order
of content added to a zone. When a zone is rendered, all ordering constraints
expressed via requires are satisfied. Those ids not found in a zone don't
have any influence on the final ordering. Missing ids aren't considered an error
rather than an over-specified ordering problem.
Working with | |||||||
In this mode, the head and script zones are treated separately. | ||||||||
| Changed: | ||||||||
| < < |
Even when head and script zones are treated separately, the head zone will | |||||||
| > > |
Working with | |||||||
| Deleted: | ||||||||
| < < |
always be rendered before the script zone, unless otherwise specified using RENDERZONE explicitly.
So any content in the script zone that depends on content placed into
the head zone is satisfied intrinsicly as they are both rendered as specified above.
Working with | |||||||
In this mode, the head and script zones are separate when adding to them,
but may be treated as merged when you call RENDERZONE if
there are any dependencies specified that only exist in the opposite zone. This
allows an ADDTOZONE{"head"...} to to successfully require an id that has
been added to script. | ||||||||
| Changed: | ||||||||
| < < | ||||||||
| > > |
Only add content to the script zone that is also legal in the <HEAD>. | |||||||
| Deleted: | ||||||||
| < < |
{MergeHeadAndScriptZones} is provided to
maintain compatibility with legacy extensions that use
ADDTOHEAD to add <script> markup and require content
that is now in the script zone. {MergeHeadAndScriptZones} will be removed
from a future version of Foswiki.
| |||||||
Example: Adding to a zone with missing dependencies | ||||||||
| Changed: | ||||||||
| < < |
You must ensure that no head content (and no inline Javascript) depends on
script content. Any such dependency will be ignored. | |||||||
| > > |
You must ensure that no head content (and no inline Javascript) depends on script content, or vice-versa. Any such dependency will be ignored. However, the HTML comment decoration which normally appears after each id's content in the rendered HTML will contain a small informative text to aid debuggingExample %ADDTOZONE{
"head" | |||||||
| Deleted: | ||||||||
| < < |
In real world application this isn't a problem as Javascript is never added
to the head zone or Javascript zone part of the script zone never really
depends on non-Javascript content part of the head zone.
HTML comment decoration which normally appears after each id's
content in the rendered HTML will contain a small informative text to aid
debugging.
Example
%ADDTOZONE{
"script" | |||||||
| text=" " requires="some-id-that-exists-in-script" id="MY::TEST" | ||||||||
| Changed: | ||||||||
| < < | }% | |||||||
| > > |
}% Result | |||||||
| Deleted: | ||||||||
| < < | Result | |||||||
| Changed: | ||||||||
| < < |
<-- MY::TEST: requires= missing ids: some-id-that-exists-in-script --> | |||||||
| > > |
<-- MY::TEST: requires= missing ids: some-id-that-exists-in-script -->On the other hand, as explained earlier - when {MergeHeadAndScriptZones} is enabled - Foswiki is able resolve such dependencies successfully. | |||||||
| Changed: | ||||||||
| < < |
Example: Adding Javascript to a pageMake sure that all inline Javascript code in the topic (if it is allowed) is added to the page using%ADDTOZONE{"script"...requires="library-id"}% | |||||||
| > > |
Note that if you do have an explicit call to %RENDERZONE{"head"}% in
your templates then the content expanded at that point will be the same content
as would be inserted before the </HEAD>. | |||||||
| Deleted: | ||||||||
| < < | with the appropriate library-id to guarantee a correct load order. For example, jQuery code should be added as follows: | |||||||
| Changed: | ||||||||
| < < |
%JQREQUIRE{"shake"}%
%ADDTOZONE{ | |||||||
| > > |
Example: Adding Javascript to a page
| |||||||
| Added: | ||||||||
| > > |
is added to the page using %ADDTOZONE{"script"...requires="library-id"}%
with the appropriate library-id to guarantee a correct load order. For example, jQuery code should be added as follows:
%JQREQUIRE{"shake"}%%ADDTOZONE{ | |||||||
| "script" id="MyApp::ShakePart" text=" " requires="JQUERYPLUGIN::SHAKE" | ||||||||
| Changed: | ||||||||
| < < | }% | |||||||
| > > |
}% where "MyApp::ShakePart" is a unique id to identify the text added to script; and JQUERYPLUGIN::SHAKE signifies that the content added with that identifier should appear beforehand. | |||||||
| Deleted: | ||||||||
| < < | ||||||||
| Changed: | ||||||||
| < < |
where "MyApp::ShakePart" is a unique id to identify the text added to | |||||||
| > > | ||||||||
| Deleted: | ||||||||
| < < |
script; and JQUERYPLUGIN::SHAKE signifies that the content added with that
identifier should appear beforehand. | |||||||
Example: Adding CSS to a page
%ADDTOZONE{"head"
id="MyCSS"
text="
<style type='text/css' media='all'>
@import url('%PUBURLPATH%/%SYSTEMWEB%/MyCSS/foo.css');
</style>"
}%
| ||||||||
| Deleted: | ||||||||
| < < | ||||||||
| See also RENDERZONE, | ||||||||
| Changed: | ||||||||
| < < | Using ADDTOZONE, | |||||||
| > > | Using ADDTOZONE | |||||||
| Deleted: | ||||||||
| < < |
Updating applications to use script zone | |||||||
<-- | ||||||||
| Deleted: | ||||||||
| < < | ||||||||
Copyright © by the contributing authors. All material on this site is the property of the contributing authors.