Freemarker blank Processing

Source: Internet
Author: User

1 Overview

HTML and XML are not sensitive to white space, but it is a headache to have so many white spaces, and it is unnecessary to increase the size of the processed HTML file. Of course, it is still a big problem to output blank sensitive methods.

Freemarker provides the following tools to solve this problem:

  • A tool that ignores the blank space of some template files (the blank space is removed in the parsing phase ):
    • Strip white space: This feature automatically ignores unnecessary white space around the FTL tag. This feature can be used and disabled at any time through templates.
    • Fine-tune commands: T, RT, and Lt. Using these commands can clearly tell freemarker to ignore some blank spaces. Read the reference manual for more information.
    • FTL parameter strip_text: This removes all top-level text from the template. This is useful for templates. It only contains some defined macros (and some commands without output), because it can remove line breaks from macro definitions and other top-level commands, this improves the readability of the template.
  • Remove blank tools from output (remove near blank ):
    • Compress command

2 STRIP BLANK
If the template makes this feature possible, it will automatically ignore (that is, it is not printed out in the output) two typical redundant spaces:
  • Indentation blank and trailing blank at the end of the row (including line breaks) will be ignored, leaving only FTL labels (such as <@ mymacro/>, <# If...>) and FTL annotations (for example, <# -- blah -->), except for the ignored blank itself. For example, if a row contains only one <# If...>, the indentation before the tag and the line break after the tag are ignored. However, if this line contains <# If...> X, the blank space will not be ignored because this X is not an FTL tag. Note: according to these rules, the package on one line contains <# If...> <# list...>, empty white will be ignored, and a line with <# If...> <# list...> this won't happen because there is a blank space between two FTL labels.
    It is embedded blank, not indented or trailing blank.
  • The gaps between the following commands will be ignored: Macro, function, assign, global, local, FTL, and import, but only one blank or FTL comment between commands. In practice, it means that you can place blank lines between macro definition and Parameter definition, because the line spacing is for better readability, excluding unnecessary blank lines (line breaks ).

By default, the blank strip is enabled, and the programmer can leave it alone (it is recommended to do so ). Note that opening the strip blank does not reduce the execution efficiency of the template. The split blank operation is completed when the template is loaded. The split blank can be closed for a single line, that is, the NT command is used (for rows without blank removal ).

3. Use compress commands
Another method is to use the compress command, which is opposite to the blank strip. This job is directly based on the generated output content, rather than the template. That is to say, it dynamically checks the output content, instead of the program that generates the output FTL. It removes indentation, blank rows, and repeated spaces/tabs very strongly (read the reference manual for more information ). So for the following code:
<#compress><#assign users = [{"name":"Joe","hidden":false},<span style="white-space:pre"></span>{"name":"James Bond", "hidden":true},<span style="white-space:pre"></span>{"name":"Julia","hidden":false}]>List of users:<#list users as user>  <#if !user.hidden>  - ${user.name}  </#if></#list>That's all.</#compress>
List of users:- Joe- JuliaThat's all.

By default, user-defined commands named compress can exist in the data model (due to backward compatibility ). This is the same as the command. In addition to setting the single_line attribute, this removes all the line breaks introduced in it. In the last example, if you use <@ Compress
Single_line = true>... </@ compress> to replace <# compress>... </# compress>, the following output is displayed:
List of users: - Joe - Julia That's all.



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.