Positioning script fragments on the webpage
When turning a Page, or Dynamic Component Presentations, into a publishable webpage, SDL Tridion may insert fragments of script code into the template output during publishing. You may want to control where SDL Tridion Sites inserts the various script fragments, for example if SDL Tridion Sites puts its script fragments above your content.
Inserting page directives at the top of your page
Often, you will want to insert a set of page directives at the top of the page. Such an instruction typically configures regional settings or sets scripting options.
For example, in ASP.NET, the instruction <%@ Page CodePage="1251" CultureInfo="sr-Cyrl-CS" %> represents the page directives "set the code page to Cyrillic (Windows)" and "set the culture to Serbian (Serbia, Cyrillic)".
To ensure that this set of page directives appears at the top of the webpage, insert a special tag called tcdl:Dependency of the following format anywhere in your Page Template output:
<tcdl:Dependency section="tcdl.directives" value="Codepage='1251' CultureInfo='sr-Cyrl-CS'" />
In this tag, section="tcdl.directives" indicates that you want to add something to the page directives, and the value attribute contains the directives you wish to add. By adding this tag anywhere in your Page Template output, you ensure that the value appears in a page directive at the top of the page.
Mixing your content with script fragments generated by SDL Tridion Sites
For any number of reasons, you may want to place your own content before, between or after the script fragments generated by SDL Tridion Sites. For example, if you use ASP.NET Master Pages, you may wish to place a root element at the top of your webpage; that is above any script fragments generated by SDL Tridion Sites. Alternatively, you may wish to put any other piece of text anywhere on your webpage for whatever reason. In such cases, position the script fragments generated by SDL Tridion Sites yourself using a special tag called tcdl:PositionMarker.
These are the tcdl:PositionMarker elements you must insert, and the order in which they should be executed:
<tcdl:PositionMarker section="tcdl.directives" /><tcdl:PositionMarker section="tcdl.imports" /><tcdl:PositionMarker section="tcdl.declaration" /><tcdl:PositionMarker section="tcdl.initialization" /><tcdl:PositionMarker section="tcdl.finalization" />
Note the following:
- All but the last of these
tcdl:PositionMarkerelements must appear before the actual webpage starts, while the<tcdl:PositionMarker section="tcdl.finalization" />tag must appear after the actual webpage ends. - If you use
tcdl:PositionMarkerelements, you must specify all of them. - By default, the script fragments are inserted surrounded by script delimiters (say,
<%and%>). To remove these script delimiters (because you want to insert a script fragment inside your own script code), add an attributeuseCodeMarkerto thetcdl:PositionMarkerelement and set it tofalse. For example:<tcdl:PositionMarker section="tcdl.imports" useCodeMarker="false">.
Example
The following example shows the output produced by a Page Template, which positions a root element called myRootElement under the page directives, but above all imports, declarations and initializations:
<tcdl:PositionMarker section="tcdl.directives"/>
<!-- Place the page directives at the very top of the webpage -->
<!-- Add the property "ASPCompat='true'" to the page directives. -->
<!-- This statement may appear anywhere on the Page Template output. -->
<!-- It does not appear on the published webpage. -->
<tcdl:Dependency section="tcdl.directives" value="ASPCompat='true'" unique="true"/>
<!-- Insert the custom root element below the directives -->
<myRootElement>
<!-- Insert tcdl:PositionMarker elements in the order prescribed -->
<!-- Imports go here -->
<tcdl:PositionMarker section="tcdl.imports"/>
<!-- Declarations go here -->
<tcdl:PositionMarker section="tcdl.declaration"/>
<!-- Initialization goes here -->
<tcdl:PositionMarker section="tcdl.initialization"/>
<!-- Normal content of the webpage -->
<p>The opinions expressed on this forum do not necessarily
reflect the opinions of Snartron, Inc.</p>
<!-- Finalization goes here -->
<tcdl:PositionMarker section="tcdl.finalization"/>
<!-- Closing the root element -->
</myRootElement>
The actual published webpage will contain something like the following:
<%@ Page ASPCompat='true'%>
<!-- Place the page directives at the very top of the webpage -->
<!-- Add the property "ASPCompat='true'" to the page directives. -->
<!-- This statement may appear anywhere on the Page Template output. -->
<!-- It does not appear on the published webpage. -->
<!-- Insert the custom root element below the directives -->
<myRootElement>
<!-- Insert tcdl:PositionMarker elements in the order prescribed -->
<!-- Imports go here -->
<%@ Import namespace="Tridion.ContentDelivery.Linking"%>
<%@ Import namespace="Tridion.ContentDelivery.WAI"%>
<!-- Declarations go here -->
<%
ComponentLink componentLink = null;
ComponentPresentationAssembler componentPresentationAssembler = null;
%>
<!-- Initialization goes here -->
<%
componentLink = new ComponentLink();
componentPresentationAssembler = new ComponentPresentationAssembler("tcm:2-48-64",Page);
<!-- Normal content of the webpage -->
<p>The opinions expressed on this forum do not
necessarily reflect the opinions of Snartron, Inc.</p>
<!-- Finalization goes here -->
<%
componentLink.Dispose();
componentPresentationAssembler.Dispose();
%>
<!-- Closing the root element -->
</myRootElement>