| <!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?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'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><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","Miscellaneous","Defining variables in the template"];</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_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'' and |
| coincidentally, there is a ``foo'' 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'' 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"><#assign x = 1> <#-- create variable x --> |
| ${x} |
| <#assign x = x + 3> <#-- replace variable x --> |
| ${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'' variables of the |
| same name. Loop variables hide (not overwrite) local and ``plain'' |
| variables of the same name. For example:</p> |
| |
| <div class="code-wrapper"><pre class="code-block code-template"><#assign x = "plain"> |
| 1. ${x} <#-- we see the plain var. here --> |
| <@test/> |
| 6. ${x} <#-- the value of plain var. was not changed --> |
| <#list ["loop"] as x> |
| 7. ${x} <#-- now the loop var. hides the plain var. --> |
| <#assign x = "plain2"> <#-- replace the plain var, hiding does not mater here --> |
| 8. ${x} <#-- it still hides the plain var. --> |
| </#list> |
| 9. ${x} <#-- the new value of plain var. --> |
| |
| <#macro test> |
| 2. ${x} <#-- we still see the plain var. here --> |
| <#local x = "local"> |
| 3. ${x} <#-- now the local var. hides it --> |
| <#list ["loop"] as x> |
| 4. ${x} <#-- now the loop var. hides the local var. --> |
| </#list> |
| 5. ${x} <#-- now we see the local var. again --> |
| </#macro></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"><#list ["loop 1"] as x> |
| ${x} |
| <#list ["loop 2"] as x> |
| ${x} |
| <#list ["loop 3"] as x> |
| ${x} |
| </#list> |
| ${x} |
| </#list> |
| ${x} |
| </#list></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"><list |
| <em class="code-color">...</em>></code> tags in this case). There |
| is no other way to change the value of a loop variable (say, you can'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'':</p> |
| |
| <div class="code-wrapper"><pre class="code-block code-template"><#assign user = "Joe Hider"> |
| ${user} <#-- prints: Joe Hider --> |
| ${.globals.user} <#-- prints: Big Joe --></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-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> |