File: faq.xml

package info (click to toggle)
libfreemarker-java 2.3.13%2Bdebian1-1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 4,296 kB
  • ctags: 5,458
  • sloc: java: 42,735; xml: 1,352; jsp: 28; makefile: 14; sh: 10; python: 10
file content (203 lines) | stat: -rw-r--r-- 11,686 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<?xml version="1.0"?>
<faq title="The Old FreeMarker FAQ" adminMailTo="run2000@users.sourceforge.net" adminName="Nicholas Cull">
  <preface/>
  <topicgroup name="Assembling template data models">
    <topic id="basic" name="Can you give me some examples showing how to assemble a template data model?">
<p>The easiest way is to use <tt>SimpleHash</tt>, <tt>SimpleList</tt>, and 
<tt>SimpleScalar</tt>. For example, suppose you want to be able to use a 
variable called <tt>foo.bar.baz.bing</tt> in your template. You could make a 
structure of nested <tt>SimpleHash</tt> objects like this: </p>
<pre>SimpleHash foo = new SimpleHash();
SimpleHash bar = new SimpleHash();
SimpleHash baz = new SimpleHash();
SimpleScalar bing = new SimpleScalar("value of bing");
baz.put("bing", bing);
bar.put("baz", baz);
foo.put("bar", bar);
modelRoot.put("foo", foo);
</pre>
<p>What about nested lists? Suppose you wanted to write something like this in a 
template: </p>
<pre>&lt;list list1 as list2&gt;
    &lt;list list2 as list3&gt;
        &lt;list list3 as element&gt;
            ${element}&lt;br&gt;
        &lt;/list&gt;
    &lt;/list&gt;
&lt;/list&gt;
</pre>
<p>You could make nested <tt>SimpleList</tt>s like this: </p>
<pre>SimpleList list1 = new SimpleList();
for (int i = 0; i &lt; 3; i++) {
    SimpleList list2 = new SimpleList();
    for (int j = 0; j &lt; 3; j++) {
        SimpleList list3 = new SimpleList();
        for (int k = 0; k &lt; 3; k++) {
            list3.add(new SimpleScalar("test " +
                                       String.valueOf(i) +
                                       " " +
                                       String.valueOf(j) +
                                       " " +
                                       String.valueOf(k)));
        }
        list2.add(list3);
    }
    list1.add(list2);
}

modelRoot.put("list1", list1);
</pre>
<p>The template code above would then produce this output: </p>
<p>test 0 0 0<br />test 0 0 1<br />test 0 0 2<br />test 0 1 0<br />test 0 1 1<br />test 0 
1 2<br />test 0 2 0<br />test 0 2 1<br />test 0 2 2<br />test 1 0 0<br />test 1 0 
1<br />test 1 0 2<br />test 1 1 0<br /></p>
<p>etc.</p> 
    </topic>
    <topic id="xml" name="Can I use XML documents as data models?">
      <p>Yes, you can. In particular, you have to first build a <a href="http://www.jdom.org/" target="_top">JDOM</a> tree
      from your XML document, and afterwards you can use JDOM tree nodes as data models.
      The extensions library has a class that can wrap a JDOM nodes to expose them to FreeMarker as
      a template model. In order to wrap your JDOM document tree in a freemarker TemplateModel, it is 
      sufficient to do the following:</p>
      <pre>
...
org.jdom.Document document = getMyDocumentSomehow(...);
TemplateModel model = new freemarker.ext.jdom.NodeListModel(document);
...
      </pre>
      <p>In the example below, the templates will be able to access the XML document through the 
      "document" variable. The adapter supports all kinds of tree traversals and filterings,
      allows use of a node list as a TemplateListModel, and outputs XML fragments of contained nodes 
      when used as a string literal. It also features full XPath support.</p>
      <p>This FAQ page itself is created by applying a FreeMarker template to an XML document. Table
      of contents with links, topic grouping, and "Back to top" are all generated by the template.
      See the <tt>examples/jdom</tt> and <tt>examples/ant</tt>
      directories in FreeMarker distribution for example on using the XML support.</p>
    </topic>
    <topic id="reflect" name="What's a good way to wrap an existing object so that a template can call its methods?">
      <p>The extensions library has classes to wrap arbitrary Java objects
      as data models. In general, all you need to do to wrap your object <tt>obj</tt> into
      a TemplateModel is to call a single static factory method:</p>
      <pre>
...
TemplateModel model = freemarker.ext.beans.ReflectionUtilities.wrap(obj);
...
      </pre>
      <p>After this, you can write arbitrary method calls on the wrapped object, i.e. you
      can write <tt>${obj.getFoo()}</tt> in a template to execute method foo and place its
      result in the template output. Extensions library uses the JavaBeans introspector to discover available
      methods, so you can use bean property-style invocations as well: in previous example you could 
      have written <tt>${obj.foo}</tt> as well. If any property or method call returns an object,
      it is automatically wrapped for you, so you can chain invocations: <tt>{$obj.bar.baz.bing}</tt>
      would translate to what in Java would be <tt>obj.getBar().getBaz().getBing()</tt>.
      The framework automatically recognizes arrays as well as iterators, maps and collections
      from <tt>java.util</tt> package and provides them with additional capabilities (arrays,
      iterators, and collections act as list models and maps act as both lists of their entries
      and allow lookup when used as a method model). There is even a facility for invoking
      static methods (in case you ever needed <tt>System.currentTimeMillis()</tt> in a template).</p>
      <p>It is worth noting that the framework caches the results of introspection, so that 
      the negative performance impact resulting from reflected method lookup is minimized.</p>
      <p>For example on using the reflection wrapper, see the <tt>examples/webapp</tt>
      directory in the FreeMarker distribution.</p>
    </topic>
  </topicgroup>
  <topicgroup name="Integration">
    <topic id="servlet" name="Is there a HTTP servlet for FreeMarker I can use out-of-the-box?">
      <p>Yes, there is. The extensions library has the class <tt>freemarker.ext.servlet.FreemarkerServlet</tt>
      that you can use to provide template processing capabilities to your webapp. There is even a sample
      webapp under the <tt>examples/webapp</tt> directory of the distribution that should get you started.</p>
      <p>In general, you should only place the following inside your <tt>web.xml</tt> file:</p>
<pre>
  &lt;servlet&gt;
    &lt;servlet-name&gt;freemarker&lt;/servlet-name&gt;
    &lt;servlet-class&gt;freemarker.ext.servlet.FreemarkerServlet&lt;/servlet-class&gt;
    &lt;init-param&gt;
      &lt;param-name&gt;TemplatePath&lt;/param-name&gt;
      &lt;param-value&gt;/templates&lt;/param-value&gt;
    &lt;/init-param&gt;
    &lt;init-param&gt;
      &lt;param-name&gt;TemplateDelay&lt;/param-name&gt;
      &lt;param-value&gt;0&lt;/param-value&gt;
    &lt;/init-param&gt;
    &lt;init-param&gt;
      &lt;param-name&gt;NoCache&lt;/param-name&gt;
      &lt;param-value&gt;true&lt;/param-value&gt;
    &lt;/init-param&gt;
    &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
  &lt;/servlet&gt;

  &lt;servlet-mapping&gt;
    &lt;servlet-name&gt;freemarker&lt;/servlet-name&gt;
    &lt;url-pattern&gt;/templates/*&lt;/url-pattern&gt;
  &lt;/servlet-mapping&gt;
</pre>
    <p>This will map all URLs beginning with /templates/ to templates in your <tt>webapp/templates</tt> directory.
    The servlet readily provides access to ServletContext, HttpSession, HttpServletRequest and HttpServletRequest 
    parameters in the template data model. If you need to have additional variables available in your data model, 
    just subclass the servlet and override the <tt>preTemplateProcess()</tt> method to shove any additional data
    you need into the model before the template gets processed.</p>
    </topic>
    <topic id="ant" name="Can I use FreeMarker template processing in Ant?">
      <p>Yes, you can. In particular, there is a FreeMarker task for Ant that will get a set of XML
      files, run them through a FreeMarker template, and output the resulting files in a target 
      directory. This method is ideal for generating documentation, SQL scripts, or any other
      transformation requried during your build process. It is a viable alternative to Ant's
      <tt>xslt</tt> task. The Ant task is part of the extensions library. See the <tt>examples/ant</tt>
      directory in FreeMarker distribution for a sample <tt>build.xml</tt> file that employs FreeMarker.</p>
    </topic>
  </topicgroup>
  <topicgroup name="Features">
    <topic id="arithmetic" name="Is it possible to do integer arithmetic in templates?">
      <p>Yes. As of 2.0, FreeMarker supports numerics without casting in and
      out of TemplateScalarModel. The new TemplateNumberModel intrace means
      that numbers of any given precision are supported, not just integers.
      </p>
      <p>The basic arithmetic operators are supported,
      and meaningful comparison can be made between numbers. Internally, 
      FreeMarker uses BigDecimals to ensure that no precision is lost when 
      performing arithmetic operations.  See the <a href="docs/manual/index.html" target="_top">manual</a> for further details.</p>
    </topic>
    <topic id="editorMangle" name="I'm using a visual HTML editor that mangles template tags. Will you change the template language syntax to accomodate my editor?">
<p>I won't change the standard version, because a lot of templates depend on it. 
However, FreeMarker is designed to make it easy for you to change the syntax to 
suit your needs. Changing the tag delimiters, making tags case-insensitive, and 
representing operators differently would be simple changes to make. Take a look 
at <tt>freemarker.template.compiler.FMParser.jj</tt>, for more details.
Let me know if you need further explanation of anything in the source code.</p>
<p>My view is that the editors that break template code are themselves broken; 
you'll probably have the same problem if you try to use them with other template 
systems. A good editor should ignore, not mangle, what it doesn't understand. </p>
     </topic>
     <topic id="structureInfo" name="How can my Java classes ask a template for information about its structure (e.g. a list of all the variables)?">
<p>This isn't possible, because variable names can be dynamically generated from 
data (see the <a 
href="docs/manual/expressions.html">manual</a>). 
However, there's a more important reason why FreeMarker doesn't support this. 
The design of FreeMarker is based on the idea of a separation between business 
objects and presentation objects. This separation takes two forms: </p>
<ol>
  <li>The templates know what data to expect, but they don't know how it's 
  generated. </li>
  <li>The business objects know what data to produce, but they don't know how 
  it's going to be displayed. Therefore, they don't know anything about 
  templates. </li>
</ol>
<p>Since the business objects don't rely on the templates, if you need to use 
them with some other presentation system, you won't have to rewrite your 
application. </p>
     </topic>
  </topicgroup>
  <topicgroup name="Legal issues">
    <topic id="license" name="If we distribute FreeMarker with our product, do we have to release the source code for our product?">
      <p>No. As of 2.0, FreeMarker is released under the BSD license. This 
      means that source or binary distributions may be made freely, and
      can be included in other products, whether commercial or open source.</p>
      <p>The only restrictions apply to the copyright of FreeMarker itself,
      and the use of FreeMarker or its contributors as endorsements of
      your own product. See the <a href="LICENSE.txt">LICENSE</a> for
      further details.</p>
      <p>If you use FreeMarker, I hope you'll send me a link to some information about 
      your product, but that's optional as well.</p> 
    </topic>
  </topicgroup>
</faq>