FreeMarker notes Chapter 2 values and types, freemarker Chapter 2

Source: Internet
Author: User

FreeMarker notes Chapter 2 values and types, freemarker Chapter 2
2.1 Basic Content 2.1.1 Introduction 2.1.2 What is a value

It is similar to the numeric type in programming languages.

2.1.3 what is a type? 2.1.4 the data model is hash table 2.2 type 2.2.1 introduction 2.2.2 scalar

Scalar is the most basic and simple numeric type. It can be a string, number, Boolean value, or date;

2.2.3 container

The container types include: HashMap, ArrayList, and set (the set is a restricted sequence and cannot obtain the size of the set, the sub-variables in the set cannot be retrieved through the index, but they can still be traversed through the list Command );

2.2.4 subroutine 2.2.4.1 methods and functions

Assuming that the programmer places a method variable avg in the data model, it can be used to calculate the average value of numbers. If 3 and 5 are given as parameters, expected result 4 is displayed when avg is accessed;

The average of 3 and 5 is: ${avg(3,5)}

Output:

The average of 3 and 5 is: 4

What is the difference between methods and functions? They have no relationship, but they do not have any relationship at all. Methods are derived from data models (they reflect Java object methods), while functions are defined in templates (using function commands-this is also an advanced topic ), however, they can be used in the same way.

2.2.4.2 user-defined commands

User-Defined commands (in other words, the label of FreeMarker ).

Suppose there is a variable box whose value is a user-defined command used to print some specific HTML information. This command defines a title and information.

<@box title="Attention!">    Too much copy-pasting may leads to     maintenance headaches.</@box>
2.2.4.3 comparison between functions/methods and user-defined commands

The choice between the two is a dilemma. In terms of experience, if it can be implemented, use custom commands instead of functions/methods. The features of the command are as follows:

  • The output (Return Value) is the tag (HTML, XML, etc). The main reason is that the function can automatically escape XML (this is because $ {...} But the output of user-defined commands is not (this is because <@…> Because its output is assumed to be a tag, so it is not escaped ).
  • Side effects are also very important. It does not return values.. For example, a command is used to add an entry to the server log. (In fact, you cannot get the return value of a custom command, but some types of feedback may be set as non-local variables ).
  • Will control the process(Like the list or if Command), but not in the function/method;
2.2.5 other 2.2.5.1 nodes

Node variables represent a node in the tree structure and are usually processed in XML format.

The main function of a node is to store topology information and metadata ).

 

Project
How does FreeMarker obtain the length of a string?

<#--
/*
* $ Id: checkboxlist. ftl 720258 2008-11-24 19: 05: 16Z musachy $
*
* Licensed to the Apache Software Foundation (ASF) under one
* Or more contributor license agreements. See the NOTICE file
* Distributed with this work for additional information
* Regarding copyright ownership. The ASF licenses this file
* To you under the Apache License, Version 2.0 (
* "License"); you may not use this file except T in compliance
* With the License. You may obtain a copy of the License
*
* Www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* Software distributed under the License is distributed on
* "As is" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for
* Specific language governing permissions and limitations
* Under the License.
*/
-->
<# Assign itemCount = 0/>
<# If parameters. list ???>
<@ S. iterator value = "parameters. list">
<# Assign itemCount = itemCount + 1/>
<# If parameters. listKey ???>
<# Assign itemKey = stack. findValue (parameters. listKey)/>
<# Else>
<# Assign itemKey = stack. findValue ('top')/>
</# If>
<# If parameters. listValue?>
<# As... full text>

Freemarker displays the data in the database on the page. How to write the code? I want the complete process.

Leave your mailbox and send your code ??

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.