blob: 26df5e6b68ad678072af7e4cacfda495c51ed80f [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>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">&lt;#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>&gt;
<em class="code-color">...</em>
&lt;#nested <em class="code-color">loopvar1</em>, <em class="code-color">loopvar2</em>, <em class="code-color">...</em>, <em class="code-color">loopvarN</em>&gt;
<em class="code-color">...</em>
&lt;#return&gt;
<em class="code-color">...</em>
&lt;/#macro&gt;</code>
</pre>
<p>Where:</p>
<ul>
<li>
<code class="inline-code"><em class="code-color">name</em></code>: name
of macro variable. It&#39;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">&lt;#macro
&quot;foo-bar&quot;&gt;<em class="code-color">...</em></code>. Note that
this string literal does not expand interpolations (as
<code class="inline-code">&quot;${foo}&quot;</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&#39;s an expression). The default value can even be
another parameter, for example <code class="inline-code">&lt;#macro section title
label=title&gt;</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">&lt;#macro
<em class="code-color">...</em>&gt;</code> and
<code class="inline-code">&lt;/#macro&gt;</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&#39;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">&lt;#-- call the macro; the macro variable is already created: --&gt;
&lt;@test/&gt;
...
&lt;#-- create the macro variable: --&gt;
&lt;#macro test&gt;
Test text
&lt;/#macro&gt;</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">&lt;#macro test&gt;
Test text
&lt;/#macro&gt;
&lt;#-- call the macro: --&gt;
&lt;@test/&gt;</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">&lt;#macro test foo bar baaz&gt;
Test text, and the params: ${foo}, ${bar}, ${baaz}
&lt;/#macro&gt;
&lt;#-- call the macro: --&gt;
&lt;@test foo=&quot;a&quot; bar=&quot;b&quot; baaz=5*5-2/&gt;</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">&lt;#macro test foo bar=&quot;Bar&quot; baaz=-1&gt;
Test text, and the params: ${foo}, ${bar}, ${baaz}
&lt;/#macro&gt;
&lt;@test foo=&quot;a&quot; bar=&quot;b&quot; baaz=5*5-2/&gt;
&lt;@test foo=&quot;a&quot; bar=&quot;b&quot;/&gt;
&lt;@test foo=&quot;a&quot; baaz=5*5-2/&gt;
&lt;@test foo=&quot;a&quot;/&gt;</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">&lt;#macro list title items&gt;
&lt;p&gt;${title?cap_first}:
&lt;ul&gt;
&lt;#list items as x&gt;
&lt;li&gt;${x?cap_first}
&lt;/#list&gt;
&lt;/ul&gt;
&lt;/#macro&gt;
&lt;@list items=[&quot;mouse&quot;, &quot;elephant&quot;, &quot;python&quot;] title=&quot;Animals&quot;/&gt;</pre></div>
<p>Output:</p>
<div class="code-wrapper"><pre class="code-block code-output"> &lt;p&gt;Animals:
&lt;ul&gt;
&lt;li&gt;Mouse
&lt;li&gt;Elephant
&lt;li&gt;Python
&lt;/ul&gt;
</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">&lt;#macro img src extra...&gt;
&lt;img src=&quot;/context${src?html}&quot;
&lt;#list extra?keys as attr&gt;
${attr}=&quot;${extra[attr]?html}&quot;
&lt;/#list&gt;
&gt;
&lt;/#macro&gt;
&lt;@img src=&quot;/images/test.png&quot; width=100 height=50 alt=&quot;Test&quot;/&gt;</pre></div>
<p>Output:</p>
<div class="code-wrapper"><pre class="code-block code-output"> &lt;img src=&quot;/context/images/test.png&quot;
alt=&quot;Test&quot;
height=&quot;50&quot;
width=&quot;100&quot;
&gt;</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&#39;t see the local variables of the macro in the
nested part. If you don&#39;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">&lt;#macro do_twice&gt;
1. &lt;#nested&gt;
2. &lt;#nested&gt;
&lt;/#macro&gt;
&lt;@do_twice&gt;something&lt;/@do_twice&gt;</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">&lt;#macro do_thrice&gt;
&lt;#nested <strong>1</strong>&gt;
&lt;#nested <strong>2</strong>&gt;
&lt;#nested <strong>3</strong>&gt;
&lt;/#macro&gt;
&lt;@do_thrice <strong>; x</strong>&gt;
${<strong>x</strong>} Anything.
&lt;/@do_thrice&gt;</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">&lt;#macro repeat count&gt;
&lt;#list 1..count as x&gt;
&lt;#nested <strong>x, x/2, x==count</strong>&gt;
&lt;/#list&gt;
&lt;/#macro&gt;
&lt;@repeat count=4 ; <strong>c, halfc, last</strong>&gt;
${<strong>c</strong>}. ${<strong>halfc</strong>}&lt;#if <strong>last</strong>&gt; Last!&lt;/#if&gt;
&lt;/@repeat&gt;</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">&lt;#macro test&gt;
Test text
&lt;#return&gt;
Will not be printed.
&lt;/#macro&gt;
&lt;@test/&gt;</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>