Monorail study note 13: nvelocity enhancements

Source: Internet
Author: User
I have previously reproduced an article: Velocity template user guide Chinese edition [go] This is based on the velocity in the first Java version. Later, some enhancements were provided in the. NET version of Castle. For details, see: Nvelocity improvements
This article uses some small examples to talk about the use of these enhancements.
( Some content is directly translated from Nvelocity improvements -_- )

1. Support for array parameters
For example, define the following method in controller: Public   Void Index ()
{
Propertybag. Add ("Instance",This);
}
Public   Static   String Welcome ( Params String [] ARGs)
{
ReturnString. Join ("-", ArgS );
}

Write in VM:$ Instance. Welcome ('Arg1','Arg2')

The following output is returned:
Arg1-arg2

2. built-in dictionary support
It is convenient to transmit parameters. For example, we usually use one of the following methods:$ Htmlhelper. labelfor ('Elementid','Name:',"% {Class = 'requestred', accesskey = 'n '}")

A dictionary is automatically generated, which contains two entries: class and accesskey.

The built-in dictionary can be used in many scenarios. For example, we define a method in controller: Public   String Dictionarytest ( String Name, idictionary attributes)
{
Stringbuilder sresult =   New Stringbuilder ( " <Input type = \ " Text \ " Name =' "   + Name +   " ' " );
Foreach ( Object Key In Attributes. Keys)
{
Object Value = Attributes [Key];
Sresult. append ( "   "   + Key +   " =' "   + Value +   " ' " );
}

Sresult. append ( " /> " );

Return Sresult. tostring ();
}

Then, call the following in the VM:$ Instance. dictionarytest ('id', "% {AA = 'aa1', value = 'aa2 ', value2 = 'aa3 '}")

Will generate an input box in the page, the specific HTMLCodeYes:<InputType= "Text"Name= 'Id'AA= 'Aa1'Value= 'Aa2'Value2= 'Aa3'/>

3. Better foreach functions (this function is better)
You can execute some statements before and after foreach. The specific syntax is as follows:# Foreach ($ I in $ items)
# Each (this is optional since its the default Section)
Text which appears for each item
# Before
Text which appears before each item
# After
Text which appears after each item
#
Text which appears between each two items
# Odd
Text which appears for every other item, including the first
# Even
Text which appears for every other item, starting with the second
# Nodata
Content rendered if $ items evaluated to null or empty
# Beforeall
Text which appears before the loop, only if there are items
Matching condition
# Afterall
Text which appears after the loop, only of there are items
Matching condition
# End

For example: # Foreach ($ person in $ people)
# Beforeall
< Table >
< Tr >
< Th > Name </ Th >
< Th > Age </ Th >
</ Tr >
# Before
< Tr
# Odd
Style = 'Color: gray' >
# Even
Style = 'color: white'>

# Each
< TD > $ Person. Name </ TD >
< TD > $ Person. Age </ TD >

# After
</ Tr >

#
< Tr > < TD Colspan = '2' > $ Person. Bio </ TD > </ Tr >

# Afterall
</ Table >

# Nodata
Sorry no person found
# End

When we have two records in $ people, the following HTML is generated: < Table >
< Tr >
< Th > Name </ Th >
< Th > Age </ Th >
</ Tr >
< Tr Style = 'Color: white' >
< TD > John </ TD >
< TD > 32 </ TD >
</ Tr >
< Tr > < TD Colspan = '2' > Monorail programmer </ TD > </ Tr >
< Tr Style = 'Color: gray' >
< TD > Jin </ TD >
< TD > 12 </ TD >
</ Tr >
< Tr > < TD Colspan = '2' > Castle guru </ TD > </ Tr >
</ Table >

When $ people is null, it will directly output:Sorry no person found

4. Improvement of enumeration types
for readability, you can compare enumerated types of text expressions by yourself.
example: Public Enum orderstatus
{< br> undefined,
created,
dispatched
}

The following comparison can be performed in the VM:# If ($ order = "undefined ")
Sorry, but we don't know this order.
# Elseif ($ order = "created ")
Your order is being processed. Hold on!
# Elseif ($ order = "dispatched ")
Your order has been dispatched through ups. Cross your fingers!
# End

(It seems a problem in the original article. I modified some code again.)

New functions in castle1.0 RC3:
1. in VM, methods and attributes are no longer case sensitive and do not need to be remembered during use.
2. The dictionary function is improved. You can directly use the following methods when calling the VM Dictionary (see the precedingBuilt-in dictionary support):
Key = 'value' Key = 1 Key = 1, 1.2 Key = '1' $ Key = 'value' Key = $ Value Key = 'some $ value'

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.