blob: 0b9d5f2c57d824abfa1278584a4e89bb414627c7 [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>Defining variables in the template - 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="Defining variables in the template">
<meta property="og:locale" content="en_US">
<meta property="og:url" content="http://example.com/dgui_misc_var.html">
<link rel="canonical" href="http://example.com/dgui_misc_var.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_var.html"><span itemprop="name">Defining variables in the template</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_userdefdir.html"><span>Previous</span></a><a class="paging-arrow next" href="dgui_misc_namespace.html"><span>Next</span></a></div><div class="title-wrapper">
<h1 class="content-header header-section1" id="dgui_misc_var" itemprop="headline">Defining variables in the template</h1>
</div></div><p>As we have described, a template can use the variables defined
in the data-model. A template can also define variables outside the
data-model for its own use. These temporary variables can be created
and replaced using FTL directives. Note that each <a href="gloss.html#gloss.templateProcessingJob">template processing job</a>
has its own private set of these variables that exists while the given
page is being rendered. This variable set is initially empty, and will
be thrown away when the template processing job has been
finished.</p><p>You access a variable that you have defined in the template
exactly as if it were a variable in the data-model root. The variable
has precedence over any variable of the same name defined in the
data-model. That is, if you define a variable called ``foo&#39;&#39; and
coincidentally, there is a ``foo&#39;&#39; in the data-model as well, then the
variable created in the template will hide (not overwrite!) the
variable in the data-model root. For example,
<code class="inline-code">${foo}</code> will print the value of the variable created
in the template.</p><p>There are 3 kind of variables that are defined in a
template:</p><ul>
<li>
<p><strong>``plain&#39;&#39; variables</strong>: They
are accessible from everywhere in the template, or from the
templates inserted with <code class="inline-code">include</code> directive. You
can create and replace these variables with the <a href="ref_directive_assign.html#ref.directive.assign"><code>assign</code></a> or
<a href="ref_directive_macro.html#ref.directive.macro"><code>macro</code>
directives</a>.</p>
</li>
<li>
<p><strong>Local variables</strong>: They can
only be set inside a <a href="gloss.html#gloss.macroDefinitionBody">macro definition body</a>,
and are only visible from there. A local variable only exists for
the duration of a macro call. You can create and replace local
variables inside macro definition bodies with the <a href="ref_directive_local.html#ref.directive.local"><code>local</code>
directive</a>.</p>
</li>
<li>
<p><strong>Loop variables</strong>: Loop
variables are created automatically by directives like <a href="ref_directive_list.html#ref.directive.list"><code>list</code></a>, and
they only exist between the start-tag and end-tag of the
directive. <a href="ref_directive_macro.html#ref.directive.macro">Macro</a>
parameters are local variables, not loop variables.</p>
</li>
</ul><p>Example: Create and replace variables with
<code class="inline-code">assign</code>:</p>
<div class="code-wrapper"><pre class="code-block code-template">&lt;#assign x = 1&gt; &lt;#-- create variable x --&gt;
${x}
&lt;#assign x = x + 3&gt; &lt;#-- replace variable x --&gt;
${x}</pre></div><p>Output:</p>
<div class="code-wrapper"><pre class="code-block code-output">1
4</pre></div><p>Local variables hide (not overwrite) ``plain&#39;&#39; variables of the
same name. Loop variables hide (not overwrite) local and ``plain&#39;&#39;
variables of the same name. For example:</p>
<div class="code-wrapper"><pre class="code-block code-template">&lt;#assign x = &quot;plain&quot;&gt;
1. ${x} &lt;#-- we see the plain var. here --&gt;
&lt;@test/&gt;
6. ${x} &lt;#-- the value of plain var. was not changed --&gt;
&lt;#list [&quot;loop&quot;] as x&gt;
7. ${x} &lt;#-- now the loop var. hides the plain var. --&gt;
&lt;#assign x = &quot;plain2&quot;&gt; &lt;#-- replace the plain var, hiding does not mater here --&gt;
8. ${x} &lt;#-- it still hides the plain var. --&gt;
&lt;/#list&gt;
9. ${x} &lt;#-- the new value of plain var. --&gt;
&lt;#macro test&gt;
2. ${x} &lt;#-- we still see the plain var. here --&gt;
&lt;#local x = &quot;local&quot;&gt;
3. ${x} &lt;#-- now the local var. hides it --&gt;
&lt;#list [&quot;loop&quot;] as x&gt;
4. ${x} &lt;#-- now the loop var. hides the local var. --&gt;
&lt;/#list&gt;
5. ${x} &lt;#-- now we see the local var. again --&gt;
&lt;/#macro&gt;</pre></div><p>the output:</p>
<div class="code-wrapper"><pre class="code-block code-output">1. plain
2. plain
3. local
4. loop
5. local
6. plain
7. loop
8. loop
9. plain2
</pre></div><p>An inner loop variable can hide an outer loop variable:</p>
<div class="code-wrapper"><pre class="code-block code-template">&lt;#list [&quot;loop 1&quot;] as x&gt;
${x}
&lt;#list [&quot;loop 2&quot;] as x&gt;
${x}
&lt;#list [&quot;loop 3&quot;] as x&gt;
${x}
&lt;/#list&gt;
${x}
&lt;/#list&gt;
${x}
&lt;/#list&gt;</pre></div><p>the output:</p>
<div class="code-wrapper"><pre class="code-block code-output"> loop 1
loop 2
loop 3
loop 2
loop 1</pre></div><p>Note that the value of a loop variable is set by the directive
invocation that has created it (the <code class="inline-code">&lt;list
<em class="code-color">...</em>&gt;</code> tags in this case). There
is no other way to change the value of a loop variable (say, you can&#39;t
change its value with some kind of assignment directive). You can hide
temporarily a loop variable with another loop variable though, as you
have seen above.</p><p>Sometimes it happens that a variable hides the variable in the
data-model with the same name, but you want to read the variable of
the data-model. In this case you can use the <a href="dgui_template_exp.html#dgui_template_exp_var_special">special variable</a>
<code class="inline-code">globals</code>. For example, assume we have a variable
called <code class="inline-code">user</code> in the data-model with value ``Big
Joe&#39;&#39;:</p>
<div class="code-wrapper"><pre class="code-block code-template">&lt;#assign user = &quot;Joe Hider&quot;&gt;
${user} &lt;#-- prints: Joe Hider --&gt;
${.globals.user} &lt;#-- prints: Big Joe --&gt;</pre></div><p>For information about syntax of variables please read: <a href="dgui_template_exp.html">The Template/Expressions</a></p><div class="bottom-pagers-wrapper"><div class="pagers bottom"><a class="paging-arrow previous" href="dgui_misc_userdefdir.html"><span>Previous</span></a><a class="paging-arrow next" href="dgui_misc_namespace.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>