blob: 3f498ec90e407688de3ca855c94b0b9647ef58e4 [file] [log] [blame]
<!doctype html>
<!-- Generated by FreeMarker/Docgen from DocBook -->
<html lang="en" class="page-type-section">
<head prefix="og: http://ogp.me/ns#">
<meta charset="utf-8">
<title>Alternative (square bracket) syntax - FreeMarker Manual</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1">
<meta name="format-detection" content="telephone=no">
<meta property="og:site_name" content="FreeMarker Manual">
<meta property="og:title" content="Alternative (square bracket) syntax">
<meta property="og:locale" content="en_US">
<meta property="og:url" content="http://example.com/dgui_misc_alternativesyntax.html">
<link rel="canonical" href="http://example.com/dgui_misc_alternativesyntax.html">
<link rel="icon" href="favicon.png" type="image/png">
<link rel="stylesheet" type="text/css" href="docgen-resources/docgen.min.css?1594338517553">
</head>
<body itemscope itemtype="https://schema.org/Code">
<meta itemprop="url" content="http://example.com/">
<meta itemprop="name" content="FreeMarker Manual">
<!--[if lte IE 9]>
<div style="background-color: #C00; color: #fff; padding: 12px 24px;">Please use a modern browser to view this website.</div>
<![endif]--><div class="header-top-bg"><div class="site-width header-top"><a class="logo" href="http://example.com" role="banner"> <img itemprop="image" src="logo.png" alt="My Logo">
</a></div></div><div class="header-bottom-bg"><div class="site-width search-row"><a href="index.html" class="navigation-header">FreeMarker Manual</a><div class="navigation-header"></div></div><div class="site-width breadcrumb-row"><ul class="breadcrumb" itemscope itemtype="http://schema.org/BreadcrumbList"><li class="step-0" itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><a class="label" itemprop="item" href="index.html"><span itemprop="name">FreeMarker Manual</span></a></li><li class="step-1" itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><a class="label" itemprop="item" href="dgui.html"><span itemprop="name">Template Author&#39;s Guide</span></a></li><li class="step-2" itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><a class="label" itemprop="item" href="dgui_misc.html"><span itemprop="name">Miscellaneous</span></a></li><li class="step-3" itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><a class="label" itemprop="item" href="dgui_misc_alternativesyntax.html"><span itemprop="name">Alternative (square bracket) syntax</span></a></li></ul><div class="bookmarks" title="Bookmarks"><span class="sr-only">Bookmarks:</span><ul class="bookmark-list"><li><a href="alphaidx.html">Index</a></li><li><a href="gloss.html">Glossary</a></li><li><a href="ref.html">Reference</a></li><li><a href="app_faq.html">FAQ</a></li><li><a href="preface.html#test_target">Bőregér</a></li></ul></div></div></div> <div class="main-content site-width">
<div class="content-wrapper no-toc">
<div id="table-of-contents-wrapper" class="col-left">
</div>
<div class="col-right"><div class="page-content"><div class="page-title"><div class="pagers top"><a class="paging-arrow previous" href="dgui_misc_whitespace.html"><span>Previous</span></a><a class="paging-arrow next" href="pgui.html"><span>Next</span></a></div><div class="title-wrapper">
<h1 class="content-header header-section1" id="dgui_misc_alternativesyntax" itemprop="headline">Alternative (square bracket) syntax</h1>
</div></div> <div class="callout note">
<strong class="callout-label">Note:</strong>
<p>This feature exists since FreeMarker 2.3.4.</p>
</div>
<p>FreeMarker supports an alternative syntax, where
<code class="inline-code">[</code> and <code class="inline-code">]</code> is used instead of
<code class="inline-code">&lt;</code> and <code class="inline-code">&gt;</code> in FreeMarker
directives and comments, for example:</p><ul>
<li>
Calling predefined directive: <code class="inline-code">[#list animals as
being]<em class="code-color">...</em>[/#list]</code>
</li>
<li>
Calling user-defined directive: <code class="inline-code">[@myMacro
/]</code>
</li>
<li>
Comment: <code class="inline-code">[#-- the comment --]</code>
</li>
</ul><p>To use the alternative syntax instead of the default one, start
the template with the <a href="ref_directive_ftl.html"><code>ftl</code> directive</a>
using the alternative syntax. If you don&#39;t know what is the
<code class="inline-code">ftl</code> directive, just start the template with
<code class="inline-code">[#ftl]</code>, and remember that it should be the very
first thing in the file (except that <a href="gloss.html#gloss.whiteSpace">white-space</a> can precede it). For
example, this is how the last example of the <a href="dgui_quickstart_template.html">Getting Started</a> looks with
the alternative syntax (assuming it&#39;s a complete template, not just a
fragment):</p>
<div class="code-wrapper"><pre class="code-block code-template"><strong>[#ftl]</strong>
&lt;p&gt;We have these animals:
&lt;table border=1&gt;
&lt;tr&gt;&lt;th&gt;Name&lt;th&gt;Price
<strong>[#list animals as being]</strong>
&lt;tr&gt;
&lt;td&gt;
<strong>[#if being.size = &quot;large&quot;]</strong>&lt;b&gt;<strong>[/#if]</strong>
${being.name}
<strong>[#if being.size = &quot;large&quot;]</strong>&lt;/b&gt;<strong>[/#if]</strong>
&lt;td&gt;${being.price} Euros
<strong>[/#list]</strong>
&lt;/table&gt;</pre></div><p>The alternative (square bracket) and the default (angle bracket)
syntax are mutually exclusive within a template. That is, either the
whole template uses alternative syntax, or the whole template uses the
default syntax. If the template uses alternative syntax, things like
<code class="inline-code">&lt;#if <em class="code-color">...</em>&gt;</code> are
count as static text, not as FTL tags. Similarly, if the template uses
the default syntax, things like <code class="inline-code">[#if
<em class="code-color">...</em>]</code> count as static text, not as
FTL tags.</p><p>If you start the file with <code class="inline-code">[#ftl
<em class="code-color">...</em>]</code> (where the
<code class="inline-code"><em class="code-color">...</em></code> stands for the
optional parameters; of course <code class="inline-code">[#ftl]</code> works too)
the file will surely use the alternative (square bracket) syntax. If
you start the file with <code class="inline-code">&lt;#ftl
<em class="code-color">...</em>&gt;</code> the file will surely use
the normal (angle bracket) syntax. If there is no
<code class="inline-code">ftl</code> directive in the file, then the programmer
decides what the syntax will be by configuring FreeMarker <span class="marked-for-programmers">(programmers see
<code class="inline-code">Configuration.setTagSyntax(int)</code> in the API
javadocs)</span>. Most probably the programmers use the factory
default however. The factory default in 2.3.x is using the normal
syntax. The factory default in 2.4.x will be auto-detection, which
means that the first FreeMarker tag determines the syntax (it can be
anything, not just <code class="inline-code">ftl</code>).</p><div class="bottom-pagers-wrapper"><div class="pagers bottom"><a class="paging-arrow previous" href="dgui_misc_whitespace.html"><span>Previous</span></a><a class="paging-arrow next" href="pgui.html"><span>Next</span></a></div></div></div></div> </div>
</div>
<div class="site-footer"><div class="site-width"><div class="footer-bottom"> <p class="last-generated">
Last generated:
<time itemprop="dateModified" datetime="2020-07-09T23:48:37Z" title="Thursday, July 9, 2020 11:48:37 PM GMT">2020-07-09 23:48:37 GMT</time> </p>
<p class="copyright">
© <span itemprop="copyrightYear">1999</span>–2020
<a itemtype="http://schema.org/Organization" itemprop="copyrightHolder" href="https://apache.org/">The Apache Software Foundation</a> </p>
</div></div></div></body>
</html>