blob: 9414830de73a77cdc279cd33e7d7140b47a8648d [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>The data-model at a glance - 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="The data-model at a glance">
<meta property="og:locale" content="en_US">
<meta property="og:url" content="http://example.com/dgui_quickstart_datamodel.html">
<link rel="canonical" href="http://example.com/dgui_quickstart_datamodel.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&#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_quickstart.html"><span itemprop="name">Getting Started</span></a></li><li class="step-3" itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><a class="label" itemprop="item" href="dgui_quickstart_datamodel.html"><span itemprop="name">The data-model at a glance</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","Getting Started","The data-model at a glance"];</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_quickstart_basics.html"><span>Previous</span></a><a class="paging-arrow next" href="dgui_quickstart_template.html"><span>Next</span></a></div><div class="title-wrapper">
<h1 class="content-header header-section1" id="dgui_quickstart_datamodel" itemprop="headline">The data-model at a glance</h1>
</div></div><p>As you have seen, the data-model is basically a tree. This tree
can be arbitrarily complicated and deep, for example:</p>
<div class="code-wrapper"><pre class="code-block code-data-model"><a name="example.qStart.dataModelWithHashes"></a>(root)
|
+- animals
| |
| +- mouse
| | |
| | +- size = &quot;small&quot;
| | |
| | +- price = 50
| |
| +- elephant
| | |
| | +- size = &quot;large&quot;
| | |
| | +- price = 5000
| |
| +- python
| |
| +- size = &quot;medium&quot;
| |
| +- price = 4999
|
+- test = &quot;It is a test&quot;
|
+- whatnot
|
+- because = &quot;don&#39;t know&quot;</pre></div><p>The variables that act as directories (the root,
<code class="inline-code">animals</code>, <code class="inline-code">mouse</code>,
<code class="inline-code">elephant</code>, <code class="inline-code">python</code>,
<code class="inline-code">whatnot</code>) are called <strong>hashes</strong>. Hashes store other variables (the so
called <a name="topic.dataModel.subVar"></a><em>subvariables</em>)
by a lookup name (e.g., &quot;animals&quot;, &quot;mouse&quot; or &quot;price&quot;).</p><p>The variables that store a single value
(<code class="inline-code">size</code>, <code class="inline-code">price</code>,
<code class="inline-code">test</code> and <code class="inline-code">because</code>) are called
<strong>scalars</strong>.</p><p><a name="topic.qStart.accessVariables"></a>When you want to
use a subvariable in a template, you specify its path from the root,
and separate the steps with dots. To access the
<code class="inline-code">price</code> of a <code class="inline-code">mouse</code>, you start from
the root and go into <code class="inline-code">animals</code>, and then go into
<code class="inline-code">mouse</code> then go into <code class="inline-code">price</code>. So you
write <code class="inline-code">animals.mouse.price</code>. When you put the special
<code class="inline-code">${<em class="code-color">...</em>}</code> codes around an
expression like this, you are telling FreeMarker to output the
corresponding text at that point.</p><p>There is one more important kind of variable: <strong>sequences</strong>. They are similar to hashes, but they
don&#39;t store names for the variables they contain. Instead, they store
the subvariables sequentially, and you can access them with a
numerical index. For example, in this data-model,
<code class="inline-code">animals</code> and <code class="inline-code">whatnot.fruits</code> are
sequences:</p>
<div class="code-wrapper"><pre class="code-block code-data-model"><a name="example.qStart.dataModelWithSequences"></a>(root)
|
+- animals
| |
| +- (1st)
| | |
| | +- name = &quot;mouse&quot;
| | |
| | +- size = &quot;small&quot;
| | |
| | +- price = 50
| |
| +- (2nd)
| | |
| | +- name = &quot;elephant&quot;
| | |
| | +- size = &quot;large&quot;
| | |
| | +- price = 5000
| |
| +- (3rd)
| |
| +- name = &quot;python&quot;
| |
| +- size = &quot;medium&quot;
| |
| +- price = 4999
|
+- whatnot
|
+- fruits
|
+- (1st) = &quot;orange&quot;
|
+- (2nd) = &quot;banana&quot;</pre></div><p>To access a subvariable of a sequence you use a numerical index
in square brackets. Indexes start from 0 (it&#39;s a programmer tradition
to start with 0), thus the index of the first item is 0, the index of
the second item is 1, and so on. So to get the name of the first
animal you write <code class="inline-code">animals[0].name</code>. To get the second
item in <code class="inline-code">whatnot.fruits</code> (which is the string
<code class="inline-code">&quot;banana&quot;</code>) you write
<code class="inline-code">whatnot.fruits[1]</code>.</p><p>Scalars can further divided into these categories:</p><ul>
<li>
<p>String: Text, that is, an arbitrary sequence of characters
such as &#39;&#39;m&#39;&#39;, &#39;&#39;o&#39;&#39;, &#39;&#39;u&#39;&#39;, &#39;&#39;s&#39;&#39;, &#39;&#39;e&#39;&#39; above. For example the
<code class="inline-code">name</code>-s and <code class="inline-code">size</code>-s are
strings above.</p>
</li>
<li>
<p>Number: It&#39;s a numerical value, like the
<code class="inline-code">price</code>-s above. The string
<code class="inline-code">&quot;50&quot;</code> and the number <code class="inline-code">50</code> are
two totally different things in FreeMarker. The former is just a
sequence of two characters (which happens to be readable as a
number for humans), while the latter is a numerical value that you
can use, say, in arithmetical calculations.</p>
</li>
<li>
<p>Date/time: A date or time. Like the date an animal were
captured, or the time the shop opens.</p>
</li>
<li>
<p>Boolean: A true/false (yes/no, on/off, etc.) thing. Like
animals could have a <code class="inline-code">protected</code> subvariable,
which store if the animal is protected or not.</p>
</li>
</ul><p>Summary:</p><ul>
<li>
<p>The data-model can be visualized as a tree.</p>
</li>
<li>
<p>Scalars store a single value. The value can be a string or a
number or a date/time or a boolean.</p>
</li>
<li>
<p>Hashes are containers that store other variables and
associate them with a unique lookup name.</p>
</li>
<li>
<p>Sequences are containers that store other variables in an
ordered sequence. The stored variables can be retrieved via their
numerical index, starting from 0.</p>
</li>
</ul><div class="bottom-pagers-wrapper"><div class="pagers bottom"><a class="paging-arrow previous" href="dgui_quickstart_basics.html"><span>Previous</span></a><a class="paging-arrow next" href="dgui_quickstart_template.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>