blob: b0a07e713899c01279a173e579de18680341fa67 [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>Directives - 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="Directives">
<meta property="og:locale" content="en_US">
<meta property="og:url" content="http://example.com/dgui_template_directives.html">
<link rel="canonical" href="http://example.com/dgui_template_directives.html">
<link rel="icon" href="favicon.png" type="image/png">
<link rel="stylesheet" type="text/css" href="docgen-resources/docgen.min.css?1594338519184">
</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_template.html"><span itemprop="name">The Template</span></a></li><li class="step-3" itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><a class="label" itemprop="item" href="dgui_template_directives.html"><span itemprop="name">Directives</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><li><a href="api/index.html">API</a></li><li><a href="../index.html">Home</a></li></ul></div></div></div> <div class="main-content site-width">
<div class="content-wrapper">
<div id="table-of-contents-wrapper" class="col-left">
<script>var breadcrumb = ["FreeMarker Manual","Template Author\'s Guide","The Template","Directives"];</script>
<script src="toc.js?1594338519184"></script>
<script src="docgen-resources/main.min.js?1594338519184"></script>
</div>
<div class="col-right"><div class="page-content"><div class="page-title"><div class="pagers top"><a class="paging-arrow previous" href="dgui_template_overallstructure.html"><span>Previous</span></a><a class="paging-arrow next" href="dgui_template_exp.html"><span>Next</span></a></div><div class="title-wrapper">
<h1 class="content-header header-section1" id="dgui_template_directives" itemprop="headline">Directives</h1>
</div></div><a name="term.designer.directive"></a><p>You use FTL tags to call <strong>directives</strong>. In the example you have called the
<code class="inline-code">list</code> directive. Syntactically you have done it with
two tags: <code class="inline-code">&lt;#list animals as being&gt;</code> and
<code class="inline-code">&lt;/#list&gt;</code>.</p><p>There are two kind of FTL tags:</p><ul>
<li>
<p>Start-tag:
<code class="inline-code">&lt;#<em class="code-color">directivename</em>
<em class="code-color">parameters</em>&gt;</code></p>
</li>
<li>
<p>End-tag:
<code class="inline-code">&lt;/#<em class="code-color">directivename</em>&gt;</code></p>
</li>
</ul><p>This is similar to HTML or XML syntax, except that the tag name
starts with <code class="inline-code">#</code>. If the directive doesn&#39;t have nested
content (content between the start-tag and the end-tag), you must use
the start-tag with no end-tag. For example you write <code class="inline-code">&lt;#if
<em class="code-color">something</em>&gt;<em class="code-color">...</em>&lt;/#if&gt;</code>,
but just <code class="inline-code">&lt;#include
<em class="code-color">something</em>&gt;</code> as FreeMarker knows
that the <code class="inline-code">include</code> directive can&#39;t have nested
content.</p><p>The format of the
<code class="inline-code"><em class="code-color">parameters</em></code> depends on
the
<code class="inline-code"><em class="code-color">directivename</em></code>.</p><p>In fact there are two types of directives: <a href="gloss.html#gloss.predefinedDirective">predefined directives</a> and
<a href="gloss.html#gloss.userDefinedDirective">user-defined
directives</a>. For user-defined directives you use
<code class="inline-code">@</code> instead of <code class="inline-code">#</code>, for example
<code class="inline-code">&lt;@mydirective
<em class="code-color">parameters</em>&gt;<em class="code-color">...</em>&lt;/@mydirective&gt;</code>.
Further difference is that if the directive has no nested content, you
must use a tag like <code class="inline-code">&lt;@mydirective
<em class="code-color">parameters</em> /&gt;</code>, similarly as in
XML (e.g. <code class="inline-code">&lt;img <em class="code-color">...</em>
/&gt;</code>). But user-defined directives is an advanced topic
that will be discussed <a href="dgui_misc_userdefdir.html">later</a>.</p><p>FTL tags, like HTML tags, must be properly nested. So the code
below is wrong, as the <code class="inline-code">if</code> directive is both inside
and outside of the nested content of the <code class="inline-code">list</code>
directive:</p>
<div class="code-wrapper"><pre class="code-block code-template">&lt;ul&gt;
<strong>&lt;#list animals as being&gt;</strong>
&lt;li&gt;${being.name} for ${being.price} Euros
<strong>&lt;#if user == &quot;Big Joe&quot;&gt;</strong>
(except for you)
<strong>&lt;/#list&gt;</strong> &lt;#-- WRONG! The &quot;if&quot; has to be closed first. --&gt;
<strong>&lt;/#if&gt;</strong>
&lt;/ul&gt;</pre></div><p>Note that FreeMarker doesn&#39;t care about the nesting of HTML
tags, only about the nesting of FTL tags. It just sees HTML as flat
text, it doesn&#39;t interpret it in any way.</p><p>If you try to use a non-existing directive (e.g., you mistype
the directive name), FreeMarker will decline to use the template and
produce an error message.</p><p>FreeMarker ignores superfluous <a href="gloss.html#gloss.whiteSpace">white-space</a> inside FTL tags. So you
can write this:</p>
<div class="code-wrapper"><pre class="code-block code-template"><span class="marked-text"><span class="marked-ftl-tag">&lt;#list<em><span class="marked-invisible-text">[BR]</span></em>
  animals       as<em><span class="marked-invisible-text">[BR]</span></em>
     being<em><span class="marked-invisible-text">[BR]</span></em>
&gt;</span><em><span class="marked-invisible-text">[BR]</span></em>
<span class="marked-interpolation">${being.name}</span> for <span class="marked-interpolation">${being.price}</span> Euros<em><span class="marked-invisible-text">[BR]</span></em>
<span class="marked-ftl-tag">&lt;/#list    &gt;</span></span></pre></div><p>You may not, however, insert white-space between the
<code class="inline-code">&lt;</code> or <code class="inline-code">&lt;/</code> and the directive
name.</p><p>The complete list and description of all directives can be found
in the <a href="ref_directives.html">Reference/Directive Reference</a> (but I recommend that you
look at the chapter about expressions first).</p> <div class="callout note">
<strong class="callout-label">Note:</strong>
<p>FreeMarker can be configured to use <code class="inline-code">[</code> and
<code class="inline-code">]</code> instead of <code class="inline-code">&lt;</code> and
<code class="inline-code">&gt;</code> in the FTL tags and FTL comments, like
<code class="inline-code">[#if user == &quot;Big
Joe&quot;]<em class="code-color">...</em>[/#if]</code>. For more
information read: <a href="dgui_misc_alternativesyntax.html">Miscellaneous/Alternative (square bracket) syntax</a>.</p>
</div>
<div class="callout note">
<strong class="callout-label">Note:</strong>
<p>FreeMarker can be configured so that it understands predefined
directives without <code class="inline-code">#</code> (like <code class="inline-code">&lt;if user
== &quot;Big
Joe&quot;&gt;<em class="code-color">...</em>&lt;/if&gt;</code>).
However we don&#39;t recommend the usage of this mode. For more
information read: <a href="ref_depr_oldsyntax.html">Reference/Deprecated FTL constructs/Old FTL syntax</a></p>
</div>
<div class="bottom-pagers-wrapper"><div class="pagers bottom"><a class="paging-arrow previous" href="dgui_template_overallstructure.html"><span>Previous</span></a><a class="paging-arrow next" href="dgui_template_exp.html"><span>Next</span></a></div></div></div></div> </div>
</div>
<div class="site-footer"><div class="site-width"><div class="footer-top"><div class="col-left sitemap"></div><div class="col-right"><a class="xxe" href="http://www.xmlmind.com/xmleditor/" rel="nofollow" title="Edited with XMLMind XML Editor"><span>Edited with XMLMind XML Editor</span></a></div></div><div class="footer-bottom"> <p class="last-generated">
Last generated:
<time itemprop="dateModified" datetime="2020-07-09T23:48:39Z" title="Thursday, July 9, 2020 11:48:39 PM GMT">2020-07-09 23:48:39 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>