Location: PHPKode > projects > Open Power Template > docs/Opt/syntax.cdata.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="pl">
<head>
	<meta http-equiv="content-type" content="text/html; charset=utf-8" />
	<meta name="robots" content="all" />

	<title>CDATA sections - Open Power Template</title>
	
	<link rel="stylesheet" type="text/css" href="design/generic.css" media="all"  />
	<link rel="stylesheet" type="text/css" href="design/print.css" media="print" />
	<!--[if lte IE 6]><link rel="stylesheet" href="design/ie.css" type="text/css" /><![endif]-->	
	<!--[if IE 7]><link rel="stylesheet" href="design/ie7.css" type="text/css" /><![endif]-->
</head>
<body>

<div id="wrap">
	<div id="header">
		<h1>Open Power Template 2.0</h1>
		<h2>CDATA sections</h2>
		<p class="generated">@ 02.09.2010</p>
		<p class="location"><a href="index.html"><strong>User manual</strong></a> &raquo; <a href="syntax.html">Template syntax</a> &raquo; <a href="syntax.cdata.html">CDATA sections</a></p>
	</div>
	
	<div id="content"><dl class="location"><dt><a href="syntax.html">3. Template syntax</a><br/>3.2. CDATA sections</dt><dd class="prev">3.1. Compiler modes<br/><a href="syntax.compiler-modes.html">&laquo; Previous</a></dd><dd class="next">3.3. XML Prolog and DTD<br/><a href="syntax.prolog.html">Next &raquo;</a></dd></dl>	<h1>3.2. CDATA sections</h1><p>A CDATA section is a special part of the XML document that is visible by the parser, but it is not processed. It allows to keep XML tags as a static text that will not be parsed. By default, OPT parses the CDATA section , like in XML - the content is displayed with the surrounding delimiters, but not parsed. However, sometimes we do not want that:</p>

<pre class="xml"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;script</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;text/javascript&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #339933;">&lt;![CDATA[</span>
<span style="color: #339933;">    document.write('Text ]]&gt;</span>{$info}<span style="color: #339933;">&lt;![CDATA[ - here is some info from the script.');</span>
<span style="color: #339933;">]]&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/script<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre>

<p>Of course, OPT will destroy such code, because the JavaScript will be full of opening and closing CDATA tags. The problem can be solved with the <code>opt:literal</code> instruction. Now, the CDATA sections are still not parsed, but at least their tags are not rewritten to the output:</p>

<pre class="xml"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;script</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;text/javascript&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;opt:literal<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><span style="color: #339933;">&lt;![CDATA[</span>
<span style="color: #339933;">    document.write('Text ]]&gt;</span>{$info}<span style="color: #339933;">&lt;![CDATA[ - here is some info from the script.');</span>
<span style="color: #339933;">]]&gt;</span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/opt:literal<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/script<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre>

<p>By default, <code>opt:literal</code> produces CDATA tags around its content, so the final result is:</p>

<pre class="xml"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;script</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;text/javascript&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #339933;">&lt;![CDATA[</span>
<span style="color: #339933;">    document.write('Text SOME SCRIPT INFO - here is some info from the script.');</span>
<span style="color: #339933;">]]&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/script<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre>

<p><code>opt:literal</code> can also produce dynamic comment content or be transparent.</p>

<blockquote class="warning">
  <p>JavaScript and CSS content enclosed with CDATA section can cause problems with some older browsers so your code may not work properly. It is recommended to use <code>mode="comment_cdata"</code> attribute on <code>opt:literal</code> instruction to solve this problem. More details can be found in <a href="syntax.instructions.literal.html" title="3.7.14. opt:literal">chapter about opt:literal</a> and on <a href="https://developer.mozilla.org/en/Properly_Using_CSS_and_JavaScript_in_XHTML_Documents#Using_CDATA_Instead_of_Comments" title="Properly Using CSS and JavaScript in XHTML Documents - Using CDATA Instead of Comments">Mozilla Developer Center</a> website.</p>
</blockquote>
<h4>See also:</h4><ul><li><a href="syntax.instructions.literal.html">3.7.14. opt:literal</a></li><li><a href="https://developer.mozilla.org/en/Properly_Using_CSS_and_JavaScript_in_XHTML_Documents#Using_CDATA_Instead_of_Comments"> Mozilla Developer Center - Properly Using CSS and JavaScript in XHTML Documents</a></li></ul><dl class="location location-bottom"><dt>3.2. CDATA sections<br/><a href="syntax.html">3. Template syntax</a></dt><dd class="prev"><a href="syntax.compiler-modes.html">&laquo; Previous</a><br/>3.1. Compiler modes</dd><dd class="next"><a href="syntax.prolog.html">Next &raquo;</a><br/>3.3. XML Prolog and DTD</dd></dl>		</div>
	
	<div id="footer">
		<p>Copyright &copy; <a href="http://www.invenzzia.org/">Invenzzia Group 2008-2009</a></p>
		<p>Available under the terms of license: <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation License 1.2</a></p>
		<p>Generated by <strong>TypeFriendly 0.1.4</strong> by <a href="http://www.invenzzia.org/">Invenzzia</a></p>
	</div>
</div>

</body>
</html>
Return current item: Open Power Template