Download presentation
Presentation is loading. Please wait.
Published byJulius Lucas Modified over 9 years ago
1
Freemarker ● Introduction ● Core features ● Java part example ● Template example ● Expressions ● Builtins ● Assigning value ● Conditions ● Loops ● Macros ● Import ● Include http://freemarker.sourceforge.net
2
What is Freemarker? ● Freemarker is a template engine ● It is used to generate text output ● Example: HTML, XML, email, SQL, Java code ● Programmer prepares environment in Java ● Designer writes template referencing the environment ● Freemarker produces output
3
Core Features ● Powerful template language ● Versatile data model - object wrappers ● Internationalization / localization ready ● Configurable and extensible ● Generic - output goes to any Writer ● Templates can be loaded from file, jar, web, db.. ● XML, Ant and Jython support
4
Java Part - Environment Person person = new Person(1234); person.setName("Jon Smith"); List phones = new ArrayList(); phones.add("333-5555-777"); phones.add("899-379-973"); person.setPhones(phones); Map env = new HashMap(); env.put("PERSON", person);
5
Java Part - Configuration Configuration config = new Configuration(); config.setObjectWrapper(BEANS_WRAPPER); config.setOutputEncoding("ISO-8859-2"); config.setDirectoryForTemplateLoading(templates); config.setSetting("number_format", "0"); config.setSharedVariable("TODAY", new Date()); config.setTemplateExceptionHandler(DEBUG_HANDLER);
6
Java Part - Process Template Template template = config.getTemplate("person.ftl"); FileWriter writer = new FileWriter("person.txt"); template.process(env, writer); writer.close();
7
Template Example Dear ${PERSON.name}, you have successfully registered with account id ${PERSON.id}. You have set following contact phone numbers: ${phone}, ${TODAY?string.medium}, Prague
8
FTL Expressions ● Expressions are usually surrounded by ${} ● Variable is accessed by its name ${TODAY} ● Support for JavaBean properties, map keys and collection indexes ${PERSON.phones[1]} ● Arithmetics: +, -, *, /, %, floating point supported ● Logical operations: &&, ||, !, ==, !=,, = ● Sequence slices: ${PERSON.name[2..]}
9
FTL - Built-ins ● Function appended after expression with ? char ● Undefined: default, exists, if_exists, has_content ● String: html, xml, url, left_pad, right_pad, length, split, replace, index_of, matches, word_list ● Sequence: first, last, reverse, size, sort ● Hashes: keys, values ● XML: parent, children, root, ancestors, node_type
10
FTL - Assigning Value ● ● value ● Possible to create/overwrite top level variable in specifed (or default) namespace.
11
FTL - Conditions ●,, ● Expression must evaluate to boolean ● There is no ${} inside directive tags ● is pair tag, it must be closed ● Character < must be escaped or surrounded by () No No Yes ● switch, case, default, break directives supported
12
FTL - Loops ●, ● Iterates over each element in sequence ● Local variable item is introduced within tags ● Variables item_has_next and item_index are available ${phone_index}. ${phone}
13
FTL - Macros ●,, ● Macro is a repetitive template fragment ● Default values, variable number of parameters ● Parameter passing by name or positional Hello
14
FTL - Import ● Imports given library into the namespace ● Namespaces allows reusability without conflicts
15
FTL - Include ● Include is used to merge specified template ● Parent directories may be searched wap/cz/header.ftl, cz/header.ftl, header.ftl ● Localization support - like ResourceBundles header.ftl_cs_CZ, header_cs.ftl, header.ftl
16
FTL - XML support ● You can parse XML or wrap DOM tree root.put( "doc", NodeModel.parse(file))); ● You can access elements by name, attribute with @ ${doc.invoice.person.name} ${item} - ${item.@price} USD ● Declarative support available via
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.