blob: cce3eefe93602d438c59836ec24a4192fb17aa9c [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>list, break - 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="list, break">
<meta property="og:locale" content="en_US">
<meta property="og:url" content="http://example.com/ref_directive_list.html">
<link rel="canonical" href="http://example.com/ref_directive_list.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_list.html"><span itemprop="name">list, break</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_switch.html"><span>Previous</span></a><a class="paging-arrow next" href="ref_directive_include.html"><span>Next</span></a></div><div class="title-wrapper">
<h1 class="content-header header-section1" id="ref_directive_list" itemprop="headline">list, break</h1>
</div></div><div class="page-menu">
<div class="page-menu-title">Page Contents</div>
<ul><li><a class="page-menu-link" href="#autoid_82" data-menu-target="autoid_82">Synopsis</a></li><li><a class="page-menu-link" href="#autoid_83" data-menu-target="autoid_83">Description</a></li></ul> </div><a name="ref.directive.list"></a>
<h2 class="content-header header-section2" id="autoid_82">Synopsis</h2>
<pre class="metaTemplate">
<code class="inline-code">&lt;#list <em class="code-color">sequence</em> as <em class="code-color">item</em>&gt;
<em class="code-color">...</em>
&lt;/#list&gt;</code>
</pre>
<p>Where:</p>
<ul>
<li>
<code class="inline-code"><em class="code-color">sequence</em></code>:
Expressions evaluates to a sequence or collection
</li>
<li>
<code class="inline-code"><em class="code-color">item</em></code>: Name
of the <a href="dgui_misc_var.html">loop variable</a> (not
an expression)
</li>
</ul>
<h2 class="content-header header-section2" id="autoid_83">Description</h2>
<p>You can use the <code class="inline-code">list</code> directive to process a
section of template for each variable contained within a sequence.
The code between the start-tag and end-tag will be processed for the
1st subvariable, then for the 2nd subvariable, then for the 3rd
subvariable, etc until it passes the last one. For each such
iteration the loop variable will contain the current
subvariable.</p>
<p>There are two special loop variables available inside the list
loop:</p>
<ul>
<li>
<p><code class="inline-code"><em class="code-color">item</em>_index</code>:
This is a numerical value that contains the index of the current
item being stepped over in the loop.</p>
</li>
<li>
<p><code class="inline-code"><em class="code-color">item</em>_has_next</code>:
Boolean value that tells if the current item the last in the
sequence or not.</p>
</li>
</ul>
<p>Example 1:</p>
<div class="code-wrapper"><pre class="code-block code-template">&lt;#assign seq = [&quot;winter&quot;, &quot;spring&quot;, &quot;summer&quot;, &quot;autumn&quot;]&gt;
&lt;#list seq as x&gt;
${<strong>x_index</strong> + 1}. ${x}&lt;#if <strong>x_has_next</strong>&gt;,&lt;/#if&gt;
&lt;/#list&gt;</pre></div>
<p>will output:</p>
<div class="code-wrapper"><pre class="code-block code-output"> 1. winter,
2. spring,
3. summer,
4. autumn</pre></div>
<p>Example 2: You can use <code class="inline-code">list</code> to count
between two numbers, using a <a href="dgui_template_exp.html#dgui_template_exp_direct_seuqence">numerical range sequence
expression</a>:</p>
<div class="code-wrapper"><pre class="code-block code-template">&lt;#assign x=3&gt;
&lt;#list 1..x as i&gt;
${i}
&lt;/#list&gt;</pre></div>
<p>The output will be:</p>
<div class="code-wrapper"><pre class="code-block code-output"> 1
2
3
</pre></div>
<p>Note that the above example will not work as you may expected
if <code class="inline-code">x</code> is 0, as then it will print 0 and -1.</p>
<a name="ref.directive.list.break"></a>
<p>You can leave the <code class="inline-code">list</code> loop before it
passes the last subvariable of the sequence with the
<code class="inline-code">break</code> directive. For example this will print
``winter&#39;&#39; and ``spring&#39;&#39; only:</p>
<div class="code-wrapper"><pre class="code-block code-template">&lt;#list seq as x&gt;
${x}
&lt;#if x = &quot;spring&quot;&gt;<strong>&lt;#break&gt;</strong>&lt;/#if&gt;
&lt;/#list&gt;</pre></div>
<p><span class="marked-for-programmers">Note that if you turn on the
classic compatible mode, then the <code class="inline-code">list</code> accepts a
scalar as well and treats it as a single-element
sequence.</span></p>
<p><span class="marked-for-programmers">In general, it is best to avoid
using collection that wraps an <code class="inline-code">Iterator</code> as
parameters to <code class="inline-code">list</code> and use collection that wraps
<code class="inline-code">java.util.Collection</code> or sequence whenever
possible. There are situations however, when you only have an
<code class="inline-code">Iterator</code> at your disposal. Note that if you pass
an collection that wraps an <code class="inline-code">Iterator</code> to the
<code class="inline-code">list</code>, you can iterate over its elements only once
since <code class="inline-code">Iterator</code>s are by their nature one-off
objects. When you try to list a such collection variable for the
second time, an error will abort template
processing.</span></p>
<div class="bottom-pagers-wrapper"><div class="pagers bottom"><a class="paging-arrow previous" href="ref_directive_switch.html"><span>Previous</span></a><a class="paging-arrow next" href="ref_directive_include.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>