| <!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>Scalars - 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="Scalars"> |
| <meta property="og:locale" content="en_US"> |
| <meta property="og:url" content="http://example.com/pgui_datamodel_scalar.html"> |
| <link rel="canonical" href="http://example.com/pgui_datamodel_scalar.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="pgui.html"><span itemprop="name">Programmer's Guide</span></a></li><li class="step-2" itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><a class="label" itemprop="item" href="pgui_datamodel.html"><span itemprop="name">The Data Model</span></a></li><li class="step-3" itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><a class="label" itemprop="item" href="pgui_datamodel_scalar.html"><span itemprop="name">Scalars</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","Programmer\'s Guide","The Data Model","Scalars"];</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="pgui_datamodel_basics.html"><span>Previous</span></a><a class="paging-arrow next" href="pgui_datamodel_parent.html"><span>Next</span></a></div><div class="title-wrapper"> |
| <h1 class="content-header header-section1" id="pgui_datamodel_scalar" itemprop="headline">Scalars</h1> |
| </div></div><p>There are 4 scalar types:</p><ul> |
| <li> |
| Boolean |
| </li> |
| |
| <li> |
| Number |
| </li> |
| |
| <li> |
| String |
| </li> |
| |
| <li> |
| Date |
| </li> |
| </ul><p>For each scalar type is a |
| <code class="inline-code">Template<em class="code-color">Type</em>Model</code> |
| interface, where <code class="inline-code"><em class="code-color">Type</em></code> is |
| the name of the type. These interfaces define only one method: |
| <code class="inline-code"><em class="code-color">type</em> |
| getAs<em class="code-color">Type</em>();</code>. This returns the |
| value of the variable with the Java type (<code class="inline-code">boolean</code>, |
| <code class="inline-code">Number</code>, <code class="inline-code">String</code> and |
| <code class="inline-code">Date</code> respectively).</p> <div class="callout note"> |
| <strong class="callout-label">Note:</strong> |
| |
| <p>For historical reasons the interface for string scalars is |
| called <code class="inline-code">TemplateScalarModel</code>, not |
| <code class="inline-code">TemplateStringModel</code>.</p> |
| </div> |
| <p>A trivial implementation of these interfaces are available in |
| <code class="inline-code">freemarker.template</code> package with |
| <code class="inline-code">Simple<em class="code-color">Type</em></code> class name. |
| However, there is no <code class="inline-code">SimpleBooleanModel</code>; to |
| represent the boolean values you can use the |
| <code class="inline-code">TemplateBooleanModel.TRUE</code> and |
| <code class="inline-code">TemplateBooleanModel.FALSE</code> singletons.</p> <div class="callout note"> |
| <strong class="callout-label">Note:</strong> |
| |
| <p>For historical reasons the class for string scalars is called |
| <code class="inline-code">SimpleScalar</code>, not |
| <code class="inline-code">SimpleString</code>.</p> |
| </div> |
| <p>Scalars are immutable within FTL. When you set the value of a |
| variable in a template, then you replace the |
| <code class="inline-code">Template<em class="code-color">Type</em>Model</code> |
| instance with another instance, and don't change the value stored in |
| the original instance.</p> |
| |
| |
| |
| |
| <h2 class="content-header header-section2" id="autoid_31">Difficulties with the date type</h2> |
| |
| |
| |
| |
| |
| |
| <p>There is a complication around date types, because Java API |
| usually does not differentiate <code class="inline-code">java.util.Date</code>-s |
| that store only the date part (April 4, 2003), only the time part |
| (10:19:18 PM), or both (April 4, 2003 10:19:18 PM). To display a |
| date variable as text correctly, FreeMarker must know what parts of |
| the <code class="inline-code">java.util.Date</code> stores meaningful information, |
| and what parts are unused. Unfortunately, the only place where the |
| Java API cleanly tells this, is with database handling (SQL), |
| because databases typically has separated date, time and timestamp |
| (aka date-time) types, and <code class="inline-code">java.sql</code> has 3 |
| corresponding <code class="inline-code">java.util.Date</code> subclasses for |
| them.</p> |
| |
| <p><code class="inline-code">TemplateDateModel</code> interface has two |
| methods: <code class="inline-code">java.util.Date getAsDate()</code> and |
| <code class="inline-code">int getDateType()</code>. A typical implementation of |
| this interface, stores a <code class="inline-code">java.util.Date</code> object, |
| plus an integer that tells the "database style type". The value of |
| this integer must be a constant from the |
| <code class="inline-code">TemplateDateModel</code> interface: |
| <code class="inline-code">DATE</code>, <code class="inline-code">TIME</code>, |
| <code class="inline-code">DATETIME</code> and <code class="inline-code">UNKNOWN</code>.</p> |
| |
| <p>What is <code class="inline-code">UNKNOWN</code>? As we told earlier, |
| <code class="inline-code">java.lang</code> and <code class="inline-code">java.util</code> |
| classes are usually converted automatically into |
| <code class="inline-code">TemplateModel</code> implementations, be so called |
| object wrappers. If the object wrapper faces a |
| <code class="inline-code">java.util.Date</code>, that is not an instance of a |
| <code class="inline-code">java.sql</code> date class, it can't decide what the |
| "database style type" is, so it uses <code class="inline-code">UNKNOWN</code>. |
| Later, if the template has to use this variable, and the "database |
| style type" is needed for the operation, it will stop with error. To |
| prevent this, for the problematic variables the template author must |
| help FreeMarker to decide the "database style type", by using the |
| <a href="ref_builtins_date.html#ref_builtin_date_datetype"><code>date</code>, |
| <code>time</code> or <code>datetime</code> |
| built-ins</a>. Note that if you use <code class="inline-code">string</code> |
| built-in with format parameter, as |
| <code class="inline-code">foo?string("MM/dd/yyyy")</code>, then FreeMarker don't |
| need to know the "database style type".</p> |
| <div class="bottom-pagers-wrapper"><div class="pagers bottom"><a class="paging-arrow previous" href="pgui_datamodel_basics.html"><span>Previous</span></a><a class="paging-arrow next" href="pgui_datamodel_parent.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> |