| <!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>macro, nested, return - 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="macro, nested, return"> |
| <meta property="og:locale" content="en_US"> |
| <meta property="og:url" content="http://example.com/ref_directive_macro.html"> |
| <link rel="canonical" href="http://example.com/ref_directive_macro.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="ref.html"><span itemprop="name">Reference</span></a></li><li class="step-2" itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><a class="label" itemprop="item" href="ref_directives.html"><span itemprop="name">Directive Reference</span></a></li><li class="step-3" itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><a class="label" itemprop="item" href="ref_directive_macro.html"><span itemprop="name">macro, nested, return</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="ref_directive_userDefined.html"><span>Previous</span></a><a class="paging-arrow next" href="ref_directive_function.html"><span>Next</span></a></div><div class="title-wrapper"> |
| <h1 class="content-header header-section1" id="ref_directive_macro" itemprop="headline">macro, nested, return</h1> |
| </div></div><div class="page-menu"> |
| <div class="page-menu-title">Page Contents</div> |
| <ul><li><a class="page-menu-link" href="#autoid_104" data-menu-target="autoid_104">Synopsis</a></li><li><a class="page-menu-link" href="#autoid_105" data-menu-target="autoid_105">Description</a><ul><li><a class="page-menu-link" href="#autoid_106" data-menu-target="autoid_106">nested</a></li><li><a class="page-menu-link" href="#autoid_107" data-menu-target="autoid_107">return</a></li></ul></li></ul> </div><a name="ref.directive.macro"></a> |
| |
| |
| |
| |
| <h2 class="content-header header-section2" id="autoid_104">Synopsis</h2> |
| |
| |
| |
| <pre class="metaTemplate"> |
| <code class="inline-code"><#macro <em class="code-color">name</em> <em class="code-color">param1</em> <em class="code-color">param2</em> <em class="code-color">... paramN</em>> |
| <em class="code-color">...</em> |
| <#nested <em class="code-color">loopvar1</em>, <em class="code-color">loopvar2</em>, <em class="code-color">...</em>, <em class="code-color">loopvarN</em>> |
| <em class="code-color">...</em> |
| <#return> |
| <em class="code-color">...</em> |
| </#macro></code> |
| </pre> |
| |
| |
| <p>Where:</p> |
| |
| <ul> |
| <li> |
| <code class="inline-code"><em class="code-color">name</em></code>: name |
| of macro variable. It's not an expression. However, it can be |
| written as a string literal, which is useful if the macro name |
| contains reserved characters, for example <code class="inline-code"><#macro |
| "foo-bar"><em class="code-color">...</em></code>. Note that |
| this string literal does not expand interpolations (as |
| <code class="inline-code">"${foo}"</code>). |
| </li> |
| |
| <li> |
| <code class="inline-code"><em class="code-color">param1</em></code>, |
| <code class="inline-code"><em class="code-color">param2</em></code>, ...etc.: |
| the name of the <a href="dgui_misc_var.html">local |
| variables</a> store the parameter values (not expression), |
| optionally followed by <code class="inline-code">=</code> and the default |
| value (that's an expression). The default value can even be |
| another parameter, for example <code class="inline-code"><#macro section title |
| label=title></code>. |
| </li> |
| |
| <li> |
| <code class="inline-code"><em class="code-color">paramN</em></code>, the |
| last parameter, may optionally include a trailing ellipsis |
| (<code class="inline-code">...</code>), which indicates the macro takes a |
| variable number of parameters. If called using named parameters, |
| <code class="inline-code"><em class="code-color">paramN</em></code> will be a |
| hash containing all of the undeclared key/value pairs passed to |
| the macro. If called using positional parameters, |
| <code class="inline-code"><em class="code-color">paramN</em></code> will be a |
| sequence of the extra parameters. |
| </li> |
| |
| <li> |
| <code class="inline-code"><em class="code-color">loopvar1</em></code>, |
| <code class="inline-code"><em class="code-color">loopvar2</em></code>, ...etc.: |
| Optional. The values of <a href="dgui_misc_var.html">loop |
| variables</a> that the <code class="inline-code">nested</code> directive |
| wants to create for the nested content. These are |
| expressions. |
| </li> |
| </ul> |
| |
| <p>The <code class="inline-code">return</code> and <code class="inline-code">nested</code> |
| directives are optional and can be used anywhere and for any times |
| between the <code class="inline-code"><#macro |
| <em class="code-color">...</em>></code> and |
| <code class="inline-code"></#macro></code>.</p> |
| |
| <p>Parameters without default value must precede parameters with |
| default value |
| (<code class="inline-code"><em class="code-color">paramName</em>=<em class="code-color">defaultValue</em></code>).</p> |
| |
| |
| |
| |
| |
| <h2 class="content-header header-section2" id="autoid_105">Description</h2> |
| |
| |
| <p>Creates a macro variable (in the current namespace, if you |
| know namespace feature). If you are new to macros and user-defined |
| directives you should read the <a href="dgui_misc_userdefdir.html">the tutorial about user-defined |
| directives</a>.</p> |
| |
| <p>Macro variable stores a template fragment (called macro |
| definition body) that can be used as <a href="ref_directive_userDefined.html#ref.directive.userDefined">user-defined directive</a>. |
| The variable also stores the name of allowed parameters to the |
| user-defined directive. You must give value for all of those |
| parameters when you use the variable as directive, except for |
| parameters that has a default value. The default value will be used |
| if and only if you don't give value for the parameter when you call |
| the macro.</p> |
| |
| <p>The variable will be created at the beginning of the template; |
| it does not mater where the <code class="inline-code">macro</code> directive is |
| placed in the template. Thus, this will work:</p> |
| |
| |
| |
| <div class="code-wrapper"><pre class="code-block code-template"><#-- call the macro; the macro variable is already created: --> |
| <@test/> |
| ... |
| |
| <#-- create the macro variable: --> |
| <#macro test> |
| Test text |
| </#macro></pre></div> |
| |
| <p>However, if the macro definitions are inserted with |
| <code class="inline-code">include</code> directive, they will not be available |
| until FreeMarker has executed the <code class="inline-code">include</code> |
| directive.</p> |
| |
| <p>Example: Macro without parameters:</p> |
| |
| |
| |
| <div class="code-wrapper"><pre class="code-block code-template"><#macro test> |
| Test text |
| </#macro> |
| <#-- call the macro: --> |
| <@test/></pre></div> |
| |
| <p>Output:</p> |
| |
| |
| |
| <div class="code-wrapper"><pre class="code-block code-output"> Test text |
| </pre></div> |
| |
| <p>Example: Macro with parameters:</p> |
| |
| |
| |
| <div class="code-wrapper"><pre class="code-block code-template"><#macro test foo bar baaz> |
| Test text, and the params: ${foo}, ${bar}, ${baaz} |
| </#macro> |
| <#-- call the macro: --> |
| <@test foo="a" bar="b" baaz=5*5-2/></pre></div> |
| |
| <p>Output:</p> |
| |
| |
| |
| <div class="code-wrapper"><pre class="code-block code-output"> Test text, and the params: a, b, 23 |
| </pre></div> |
| |
| <p>Example: Macro with parameters and default parameter |
| values:</p> |
| |
| |
| |
| <div class="code-wrapper"><pre class="code-block code-template"><#macro test foo bar="Bar" baaz=-1> |
| Test text, and the params: ${foo}, ${bar}, ${baaz} |
| </#macro> |
| <@test foo="a" bar="b" baaz=5*5-2/> |
| <@test foo="a" bar="b"/> |
| <@test foo="a" baaz=5*5-2/> |
| <@test foo="a"/></pre></div> |
| |
| <p>Output:</p> |
| |
| |
| |
| <div class="code-wrapper"><pre class="code-block code-output"> Test text, and the params: a, b, 23 |
| Test text, and the params: a, b, -1 |
| Test text, and the params: a, Bar, 23 |
| Test text, and the params: a, Bar, -1 |
| </pre></div> |
| |
| <p>Example: A more complex macro.</p> |
| |
| |
| |
| <div class="code-wrapper"><pre class="code-block code-template"><#macro list title items> |
| <p>${title?cap_first}: |
| <ul> |
| <#list items as x> |
| <li>${x?cap_first} |
| </#list> |
| </ul> |
| </#macro> |
| <@list items=["mouse", "elephant", "python"] title="Animals"/></pre></div> |
| |
| <p>Output:</p> |
| |
| |
| |
| <div class="code-wrapper"><pre class="code-block code-output"> <p>Animals: |
| <ul> |
| <li>Mouse |
| <li>Elephant |
| <li>Python |
| </ul> |
| </pre></div> |
| |
| <p>Example: A macro with support for a variable number of named |
| parameters:</p> |
| |
| |
| |
| <div class="code-wrapper"><pre class="code-block code-template"><#macro img src extra...> |
| <img src="/context${src?html}" |
| <#list extra?keys as attr> |
| ${attr}="${extra[attr]?html}" |
| </#list> |
| > |
| </#macro> |
| <@img src="/images/test.png" width=100 height=50 alt="Test"/></pre></div> |
| |
| <p>Output:</p> |
| |
| |
| |
| <div class="code-wrapper"><pre class="code-block code-output"> <img src="/context/images/test.png" |
| alt="Test" |
| height="50" |
| width="100" |
| ></pre></div> |
| |
| |
| |
| |
| |
| |
| <h3 class="content-header header-section3" id="autoid_106">nested</h3> |
| |
| |
| <a name="ref.directive.nested"></a> |
| |
| <p>The <code class="inline-code">nested</code> directive executes the |
| template fragment between the start-tag and end-tags of the |
| user-defined directive. The nested part can contain anything what |
| is valid in templates; interpolations, directives, ...etc. It is |
| executed in the context where the macro was called from, rather |
| than in the context of the macro definition body. Thus, for |
| example, you don't see the local variables of the macro in the |
| nested part. If you don't call the <code class="inline-code">nested</code> |
| directive, the part between the start-tag and end-tags of the |
| user-defined directive will be ignored.</p> |
| |
| <p>Example:</p> |
| |
| |
| |
| <div class="code-wrapper"><pre class="code-block code-template"><#macro do_twice> |
| 1. <#nested> |
| 2. <#nested> |
| </#macro> |
| <@do_twice>something</@do_twice></pre></div> |
| |
| <p>Output:</p> |
| |
| |
| |
| <div class="code-wrapper"><pre class="code-block code-output"> 1. something |
| 2. something |
| </pre></div> |
| |
| <p>The nested directive can create loop variables for the |
| nested content. For example:</p> |
| |
| |
| |
| <div class="code-wrapper"><pre class="code-block code-template"><#macro do_thrice> |
| <#nested <strong>1</strong>> |
| <#nested <strong>2</strong>> |
| <#nested <strong>3</strong>> |
| </#macro> |
| <@do_thrice <strong>; x</strong>> |
| ${<strong>x</strong>} Anything. |
| </@do_thrice></pre></div> |
| |
| <p>This will print:</p> |
| |
| |
| |
| <div class="code-wrapper"><pre class="code-block code-output"> 1 Anything. |
| 2 Anything. |
| 3 Anything. |
| </pre></div> |
| |
| <p>A more complex example:</p> |
| |
| |
| |
| <div class="code-wrapper"><pre class="code-block code-template"><#macro repeat count> |
| <#list 1..count as x> |
| <#nested <strong>x, x/2, x==count</strong>> |
| </#list> |
| </#macro> |
| <@repeat count=4 ; <strong>c, halfc, last</strong>> |
| ${<strong>c</strong>}. ${<strong>halfc</strong>}<#if <strong>last</strong>> Last!</#if> |
| </@repeat></pre></div> |
| |
| <p>The output will be:</p> |
| |
| |
| |
| <div class="code-wrapper"><pre class="code-block code-output"> 1. 0.5 |
| 2. 1 |
| 3. 1.5 |
| 4. 2 Last! |
| </pre></div> |
| |
| |
| |
| |
| |
| |
| |
| <h3 class="content-header header-section3" id="autoid_107">return</h3> |
| |
| |
| <a name="ref.directive.macro.return"></a> |
| |
| <p>With the <code class="inline-code">return</code> directive, you can leave |
| a macro or function definition body anywhere. Example:</p> |
| |
| |
| |
| <div class="code-wrapper"><pre class="code-block code-template"><#macro test> |
| Test text |
| <#return> |
| Will not be printed. |
| </#macro> |
| <@test/></pre></div> |
| |
| <p>Output:</p> |
| |
| |
| |
| <div class="code-wrapper"><pre class="code-block code-output"> Test text |
| </pre></div> |
| |
| <div class="bottom-pagers-wrapper"><div class="pagers bottom"><a class="paging-arrow previous" href="ref_directive_userDefined.html"><span>Previous</span></a><a class="paging-arrow next" href="ref_directive_function.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> |