| <!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?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'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></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_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"><#list animals as being></code> and |
| <code class="inline-code"></#list></code>.</p><p>There are two kind of FTL tags:</p><ul> |
| <li> |
| <p>Start-tag: |
| <code class="inline-code"><#<em class="code-color">directivename</em> |
| <em class="code-color">parameters</em>></code></p> |
| </li> |
| |
| <li> |
| <p>End-tag: |
| <code class="inline-code"></#<em class="code-color">directivename</em>></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'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"><#if |
| <em class="code-color">something</em>><em class="code-color">...</em></#if></code>, |
| but just <code class="inline-code"><#include |
| <em class="code-color">something</em>></code> as FreeMarker knows |
| that the <code class="inline-code">include</code> directive can'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"><@mydirective |
| <em class="code-color">parameters</em>><em class="code-color">...</em></@mydirective></code>. |
| Further difference is that if the directive has no nested content, you |
| must use a tag like <code class="inline-code"><@mydirective |
| <em class="code-color">parameters</em> /></code>, similarly as in |
| XML (e.g. <code class="inline-code"><img <em class="code-color">...</em> |
| /></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"><ul> |
| <strong><#list animals as being></strong> |
| <li>${being.name} for ${being.price} Euros |
| <strong><#if user == "Big Joe"></strong> |
| (except for you) |
| <strong></#list></strong> <#-- WRONG! The "if" has to be closed first. --> |
| <strong></#if></strong> |
| </ul></pre></div><p>Note that FreeMarker doesn't care about the nesting of HTML |
| tags, only about the nesting of FTL tags. It just sees HTML as flat |
| text, it doesn'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"><#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> |
| ></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"></#list ></span></span></pre></div><p>You may not, however, insert white-space between the |
| <code class="inline-code"><</code> or <code class="inline-code"></</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"><</code> and |
| <code class="inline-code">></code> in the FTL tags and FTL comments, like |
| <code class="inline-code">[#if user == "Big |
| Joe"]<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"><if user |
| == "Big |
| Joe"><em class="code-color">...</em></if></code>). |
| However we don'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-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> |