Tinytemplate (Velocity Plus) is about to launch

Source: Internet
Author: User
Tags tojson

Originally did not write a template engine of the plan, because according to my understanding, has always known this "language" level of the engine, the difficulty is very large. Always feel that their level is not enough, so I do not dare to have this idea. Until a large number of use of velocty, encountered velocty many satisfactory places, but also helpless, back to the JSP, and heart not gan. Therefore, it is expected to find a grammatical structure close to velocty, but there is no velocity of these inconvenient template language. So into a template language group, a group of big boys are at least a template language of the author, so the author in the inside expressed their expectations, the big boys have introduced their own template engine, so the author one by one to see the source code, look at the document. Say the actual, look at the document, feel very good, have their own characteristics, look at the grammar is also good, in addition to some of their own special attention to the point is not outside, its parts are very good. But the distance from their own demand or there is a gap, how to do it? So we are ready to find a closest template engine to carry out a certain expansion, pick to pick the Jetbrick this template language (in this jetbrick to extend a strong heartfelt thanks!! )。
There are several reasons why you should choose this:
    • ANTLR language file Writing is very clear, for me this kind of antlr blindness, can also understand, can even divert modify, this is very important, in the late author made a considerable grammatical improvement, this aspect has the extremely embodiment
    • Code quality is good, using sonar for analysis, the results are quite good, in all the template language that the author has seen, counted as the choice
    • The grammatical structure is very close to velocity, which is very important to me, because my idea is that the velocity syntax is quite acceptable, close to velocity syntax, and some users of velocity can easily switch
    • The test case is quite perfect, and that's what it looked like, but finally, after the tiny template engine was completed, several bugs were found using its test case, which was very effective.

So download the source code, the initial review is good, but in the implementation of the points and note the gap between the claim:
First I hope is weak type, Jetbrick is strong type, performance is improved but the development process will be more inconvenient.
In addition, because the Jetbrick author expects strongly typed inference in the compiler, the content of the runtime is strongly coupled with the content of the compile period.
There are also some language features that belong to personal hobbies and some differences, so decide to write one yourself.

Syntax Specification in order to better explain the syntax of tiny, so the whole process of contrast velocity
Description The content in,<> must have at least one, [] in the content representation can have no. References expression ${-expression}
An expression is a value that is the result of the last operation, and a variable can be used in an expression
= = differs from velocity, where curly braces must be there, cannot be omitted, "-" is not allowed to appear
${a+b-c+d}
${"abc" +1}
${user.name}
${USER.ITEMS[1].COUNT+3}
${user.func (1,2,3,4) +map.def+map["AA"]}
Variable
<a->[_a-za-z0-9]
= = differs from velocity, "-" is not allowed to appear
Example:
Legal

    • Abc
    • Ab_c
    • Ab9_

Illegal

    • 9bc
    • _bc
    • A-B

Property ValueThe attribute syntax is the same as the variable name
The difference curly braces must have, the "-" number can not have
The property value is actually an expression, so it's also possible to write this.
${a. ("AA" +BB)}, if BB has a value of 3, is equivalent to ${a.aa3}, equivalent to A. " Aa3 "
instruction #set-assignment instructionFormat:
#set (Ref=[",']arg[",'] )
Example
#{set} is not supported
Variable name can not be added in front of $
#set (aa=1)
#set (Aa=2,b=2,c=aa+2,d=func ("info"))
#set (AA=[AA, "BB", 2,3,4]
#set (aa={"AA": 1,bb:2,aa.bb.func () +3:5}
#if/#elseif/#else-judgment statementFormat:
#if ([Condition])[Output] [#elseif ([Condition])[Output]] * [#[{]Else[}] [Output]]#[{]End[}]
Usage:
#if (AA)
#end
#if (aa| | Dbo
#end
This can be either a logical expression or a non-logical expression
The situation is as follows:
If it is null, FALSE
If it's a Boolean, look at True/false.
If it's a string, look at the length >0
If it's collection, look at size>0.
If it's iterator, look at Hasnext.
If it's an array, look at the length >0
If it's enumerator, look at hasmoreelements.
If it's a map look at size
In other cases, it returns true
All of the following logical expressions support
Operator Name
Symbol
Alternative Symbol
Example
Equals number
==
eq
#if ($foo = = 42)
Equals String
==
eq
#if ($foo = = "Bar")
Object equivalence
==
eq
#if ($foo = = $bar)
Not Equals
!=
Ne
#if ($foo! = $bar)
Greater Than
>
Gt
#if ($foo > 42)
Less Than
<
Lt
#if ($foo < 42)
Greater Than or Equal to
> =
Ge
#if ($foo >= 42)
Less Than or Equal to
< =
Le
#if ($foo <= 42)
Boolean not
!
Not
#if (! $foo)
Notes:

    • = = equals is used.
    • You can avoid being separated from the display in the following way:

#if (foo = = bar) It's true!#{else}it ' s not! #end </li>
Note: Unlike velocity: do not precede the variable name with the "$" symbol
#for-for Cycle InstructionsFormat:
# for (varname:collection) Display content 1 [#[{]else[}]] Display content 2#[{]End[}]
Indicates that the collection is looped, executes the display 1, and if the collection is empty, displays the contents 2
Note: Different from velocity: increased #else command
Can be the content of collection:
Arrays, collections, Iterator,enumeration,map, objects, and even null
If it is not a collection object, but only a normal object, it loops only a few times, which is better suited to sometimes returning a list, sometimes returning an object, avoids adding complex judgments.
#include-Include directivesFormat:
#include (Expression[,{aa:1,bb:2}])
Example:
#include ("/AA/BB/CC.VM")
#include ("/AA/BB/CC.VM")
Difference from velocity: used to render another page at its current location, with only one map to pass the parameters
#parse: Instruction has no meaning to be canceled #stop-stops the template engineFormat:
#stop [(Expresson)]
Usage:
#if (AA==BB)
#stop
#end
Equivalent to
#stop (AA==BB)
#break-stops the current directiveFormat:
#break [(Expresson)]
Usage:
#if (AA==BB)
#break
#end
Equivalent to
#break (AA==BB)
#continue [(Expresson)]
Usage:
#if (AA==BB)
#continue
#end
Equivalent to
#continue (AA==BB)
#evaluate-dynamically evaluates a string or reference is canceled because it is hard to use and is too small to be canceledAdd Command #[@]call
#call ("AA" + "BB")
Equivalent to
#aabb (+)
#call (Format ("sys%smdl%s", +)
Equivalent to
#Sys1Mdl2 (+)
# @call ("AA" + "BB", +)
...
#end
Equivalent to
# @aabb
...
#end
# @call (Format ("sys%smdl%s,")
...
#end
Equivalent to
# @Sys1Mdl2 (
...
#end
Name passing is also supported, see macro invocation
#define-Because the chicken ribs are canceled, all the variables in the caller are visible to the callee and no longer need to explicitly pass #macro-Define macrosFormat:
# MacroMacroName(Arg1 [, Arg2,arg3 ... argn])[VM-VTL code ...]#[{]End[}]
Unlike velocity: the macroname is enclosed in parentheses outside the brackets to avoid mixing with parameters, and the parameters must be separated by commas
There are two ways of calling a macro
#vmname (ARG1,ARG2)
# @vmname (Arg1,args)
....
#end
Different from Velocity: supports named calls:
such as the following way; #vmname (arg2=3), can also be mixed with: #vmname (1,2,arg5=3,arg4=4)
notes:
# #单行注释
#--...--# #*. *#, supported in both formats, is considered to be a #----When <!----> #更方便

Non-parsed token, the following content is output as-is
#[[
This have invalid syntax that would normally need "poor man's escaping" like:


      • #define ()

    • ${blah

]]#

New additions: i18n support:
$${AAA.BBB.CCC}

Represents the internationalization content that is displayed for AAA.BBB.CCC
Of course, there is a strong layout (page inheritance), easy to use (the VM will now be used), easy to expand (very easy to expand), small volume (the engine only 2000 lines of code), but also want something, you can heartily put forward.
What's NEW: Java object method Extension, that is, without modifying the original class, add to the Java class
For example, you can add the bold function to a string to make it bold in the following way
${"Leisurely is a good comrade". Bold ()}
You can also add methods such as Tojson,toxml to an object to output JSON or XML directly in the following way:
${user.tojson ()},${user.toxml ()}
Of course, there is still a little plan of the characteristics of the implementation, that is the layout of the way the decoration, perhaps some students do not understand, then leave some suspense it.

Tinytemplate (Velocity Plus) is about to launch

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.