cool hit counter Template engine in Groovy (II)_Intefrankly

Template engine in Groovy (II)


MarkupTemplateEngine is the most powerful implementation class of all Groovy's template classes. As the class name suggests, Markup is the syntax for the markup class, used in XML,HTML, etc.

Unlike other templates, MarkupTemplateEngine uses a different template than the others.

Remember the writing format we learned about earlier that StreamingMarkupBuilder and JsonBuilder used? MarkupTemplateEngine uses a writing style that is almost identical.

Let's start with a simple example:

Output results.

As you can see by this simple example, the output is in Markup format. In fact, the template is the Groovy DSL, all in legitimate Groovy syntax.

The MarkupTemplateEngine provides several important features.

Template syntax

Markup format classes are composed of tags (tag), tag properties (property) and content (text), and tags can contain sub-tags, all of which have corresponding syntax.

As can be seen, each method name is the label name, the attributes use key-value pairs, and the text is one of the parameters, and the position of the parameter does not matter and is automatically recognized; if it contains child nodes, it is enough to use closures to include them.

Commonly used functions

When studying the XML counterpart classes, you have already seen that StreamingMarkupBuilder contains methods for generating corresponding directives such as XML headers, etc. The Markup Template Engine class provides similar directive methods.

* yieldUnescaped

will output the given statement directly, without escaping the characters in it.

* yield

Contrary to the above, special characters will be escaped.

* head

These HTML tagging methods generate the corresponding tags.

* xmlDeclaration

method will generate a standard XML document header.

* comment

method to generate HTML comments.

* newLine

Generate a new row.

Template file


The implementation code is as follows.

The template file provides three forms of import instructions.

* include template: 'include.tpl'

Introduce another template file

* include unescaped: 'include.txt'

Introduce the file as is, without converting any characters

* include escaped: 'include.txt'

Conversion of introduced files according to conversion symbols

There are 3 other built-in methods that provide the same functionality.

* includeGroovy()

* includeEscaped()

* includeUnescaped()

Configuration items

The MarkupTemplateEngine allows the behavior of the engine to be specified using the configuration.

* declarationEncoding :

Specifies the encoding format for the output of the xmlDeclaration() method, the default is no encoding format.

Output at this time

If not set, the output is

* expandEmptyElements :

Whether to extend the empty tag, default is false, if true then the empty tag is represented by a pair of tags, e.g.

* useDoubleQuotes

Whether to use double quotes or not, the default is false, and the attribute value is wrapped in single quotes, and true is wrapped in double quotes.

* newLineString

The line break, by default, uses the system property line.separator, which is . When a custom value is set, the custom value is used.

For example:For

propernewLineString = '

' time, exports

* autoEscape

Whether to automatically escape, default is false. When set to true, incoming parameters are automatically escaped when they encounter an escape character.

* autoIndent

Whether to indent automatically. The default is false. When set to true, indent typesetting is performed using the indent notation specified by autoIndentString, according to the node's level of containment.

* autoIndentString

Indent the string, the default is 4 spaces. Used for indenting typesetting when autoIndent is true.

* autoNewLine

Set if or not auto-wrap, default is false,when true, then auto-wrap after each tag. For example:


If false then.

* baseTemplateClass

* locale

This is the localized content, which by default uses the host's Local.

There is a lot more to the MarkupTemplateEngine, and the length required is continued in the next section.

1、Indepth analysis of KubernetesLocalPersistentVolume II
2、What opportunities and challenges are presented to the insurance industry in the era of big data
3、Small details of oracle parallelism r2 notes 44 days
4、What is a CSS preprocessor
5、JSP custom tag library taglib

    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送