giovedì 26 maggio 2016

Tutorial su come usare le macro

Breve e interessante tutorial su come usare le macro su Kentico:
Da cui ho elaborato un esempio reale di controllo dei campi dipendenti  sui BizForm

if (Tipologia_utente.Value == "Privato" || Tipologia_utente.Value == "Professionista") {return false;} else {return true;}

Lista delle espressioni macro

Constant macros

ApplicationPath
Syntax: {%applicationpath%}
Returns: UrlHelper.GetApplicationPath().TrimEnd('/') - Application path
AppPath
Syntax: {%apppath%}
Returns: UrlHelper.GetApplicationPath().TrimEnd('/') - Application path
CurrentCulture
Syntax: {%currentculture%}
Returns: CultureHelper.GetPreferredCulture() - Current culture
CurrentDate
Syntax: {%currentdate%}
Returns: DateTime.Now.ToShortDateString()
CurrentDateTime
Syntax: {%currentdatetime%}
Returns: DateTime.Now
CurrentTime
Syntax: {%currenttime%}
Returns: DateTime.Now.ToShortTimeString()
CurrentUrl
Syntax: {%currenturl%}
Returns: UrlHelper.CurrentURL - Current URL (new in 5.5)
Domain
Syntax: {%domain%}
Returns: UrlHelper.GetCurrentDomain() - Domain
EmptyString
Syntax: {%EmptyString%}
Returns: String.Empty
Ip
Syntax: {%ip%}
Returns: HTTPHelper.GetUserHostAddress() - Client's IP address
Now
Syntax: {%now%}
Returns: DateTime.Now
Pi
Syntax: {%Pi%}
Returns: Math.PI
Zero
Syntax: {%Zero%}
Returns: 0
Example: {%Zero|(add)5%}

Context macros

CMSContext.CurrentAttachment
Syntax: {%cmscontext.currentattachment%}
CMSContext.CurrentDocument
Syntax: {%cmscontext.currentdocument%}
CMSContext.CurrentDocumentCulture
Syntax: {%cmscontext.currentdocumentculture%}
CMSContext.CurrentDocumentOwner
Syntax: {%cmscontext.currentdocumentowner%}
CMSContext.CurrentDocumentParent
Syntax: {%cmscontext.currentdocumentparent%}
CMSContext.CurrentPageInfo
Syntax: {%cmscontext.currentpageinfo%}
CMSContext.CurrentSite
Syntax: {%cmscontext.currentsite%}
CMSContext.CurrentTemplate
Syntax: {%cmscontext.currenttemplate%}
CMSContext.CurrentUser
Syntax: {%cmscontext.currentuser%}
CommunityContext.CurrentFriend
Syntax: {%communitycontext.currentfriend%}
CommunityContext.CurrentFriendship
Syntax: {%communitycontext.currentfriendship%}
CommunityContext.CurrentGroup
Syntax: {%communitycontext.currentgroup%}
ECommerceContext.CurrentCurrency
Syntax: {%ecommercecontext.currentcurrency%}
ECommerceContext.CurrentCustomer
Syntax: {%ecommercecontext.currentcustomer%}
ECommerceContext.CurrentCustomerCountry
Syntax: {%ecommercecontext.currentcustomercountry%}
ECommerceContext.CurrentCustomerState
Syntax: {%ecommercecontext.currentcustomerstate%}
ECommerceContext.CurrentExchangeRate
Syntax: {%ecommercecontext.currentexchangerate%}
ECommerceContext.CurrentProduct
Syntax: {%ecommercecontext.currentproduct%}
ECommerceContext.CurrentProductDepartment
Syntax: {%ecommercecontext.currentproductdepartment%}
ECommerceContext.CurrentProductInternalStatus
Syntax: {%ecommercecontext.currentproductinternalstatus%}
ECommerceContext.CurrentProductPublicStatus
Syntax: {%ecommercecontext.currentproductpublicstatus%}
ECommerceContext.CurrentProductSupplier
Syntax: {%ecommercecontext.currentproductsupplier%}
ECommerceContext.CurrentShoppingCart
Syntax: {%ecommercecontext.currentshoppingcart%}
ForumContext.CurrentForum
Syntax: {%forumcontext.currentforum%}
ForumContext.CurrentGroup
Syntax: {%forumcontext.currentgroup%}
ForumContext.CurrentPost
Syntax: {%forumcontext.currentpost%}
ForumContext.CurrentThread
Syntax: {%forumcontext.currentthread%}
MediaLibraryContext.CurrentMediaFile
Syntax: {%medialibrarycontext.currentmediafile%}
MediaLibraryContext.CurrentMediaLibrary
Syntax: {%medialibrarycontext.currentmedialibrary%}
PortalContext.ViewMode
Syntax: {%portalcontext.viewmode%}
SiteContext.CurrentUser
Syntax: {%sitecontext.currentuser%}

Macro operations

add
Syntax: |(add)<number>
Returns: Adds the value
append
Syntax: |(append)<string>
Returns: Appends (suffixes) the given string to the value
Example: {%Zero|(append)ABC%} returns 0ABC
clear
Syntax: |(clear)
Returns: Clears current value to produce empty result
contains
Syntax: |(contains)<string>
Returns: True if value contains given string
cos
Syntax: |(cos)
Returns: Cosine
Example: {%Something|(cos)%}
culture
Syntax: |(culture)<code>
Returns: Returns the value in specified culture
Example: {%SKUPrice|(culture)en-us%} writes the product price in an English culture formatting
default
Syntax: |(default)<value>
Returns: Default value (if value is not found)
Example: {%SKUPrice|N/A%} writes product price or N/A if the document is not a product
divide
Syntax: |(divide)<value>
Returns: Divides the value by the given number
Example: {%Something|(divide)3%}
encode
Syntax: |(encode)<true/false>
Returns: HTMLHelper.HTMLEncode
Example: {%DocumentName|(encode)true%} or {%DocumentName|(encode)%} writes the HTML encoded document name, such as Black & white
endswith
Syntax: |(endswith)<string>
Returns: True if value ends with given string
equals
Syntax: (equals)<value>
Returns: Returns “true” if the resolved value matches the given value, else returns “false”. Uses the (truevalue), (falsevalue) settings for the output.
Example: {%UserName|(equals)administrator%} writes true if the user is administrator
escape
Syntax: (operation)\|(operation)
Returns: Escapes the macro
Example: {%CurrentUserName|(append)\|(you)%}
falsevalue
Syntax: |(falsevalue)<value>
Returns: Output settings for the negative output of the comparisson
Example: {%UserName|(equals)administrator|(truevalue)Yes|(falsevalue)No%} writes No if the user is not administrator
format
Syntax: |(format)<format>
Returns: Formatted value
Example: {%SKUPrice|(format){0:f1}%} writes the product price with precision for one decimal place
getmatch
Syntax: |(getmatch)<regex>
Returns: Regex.Match
Example: {%CurrentDocument.DocumentName|(getmatch)[k-r]%} will result in "om" on "Home"
greaterthan
Syntax: |(greaterthan)<number>
Returns: True if current value is greater than given number
handlesqlinjection
Syntax: |(handlesqlinjection)
Returns: Resolver will handle the SQL injection of the result
jsescape
Syntax: |(jsescape)
Returns: Replaces quotes and \n (javascript escapes)
limitlength
Syntax: |(limitlength)<chars>
Returns: TextHelper.LimitLength
lowerthan
Syntax: |(lowerthan)<number>
Returns: True if current value is lower than given number
mappath
Syntax: |(mappath)
Returns: Maps the virtual path to the physical path
Example: Maps ~/Home.aspx to C:\Inetpub\wwwroot\KenticoCMS\Home.aspx
matches
Syntax: |(matches)<regex>
Returns: True if regular expression matched on the given value
multiply
Syntax: |(multiply)<number>
Returns: Multiplies the value
not
Syntax: |(not)
Returns: Negates current value
notequals
Syntax: |(notequals)<value>
Returns: Returns “false” if the resolved value matches the given value, else returns “true”. Uses the (truevalue), (falsevalue) settings for the output.
Example: {%UserName|(notequals)administrator%} – Writes false if the user is administrator
notrecursive
Syntax: |(notrecursive)
Returns: Explicitly disables the recursion of the macros
padleft
Syntax: |(padleft)<total width>, |(padleft)<total width>(with)<char>
Returns: Makes sure that the string has total width of characters, filling the blanks with space
Example: {%Zero|(add)5|(padleft)3(with)0%} returns 005
padright
Syntax: |(padright)<total width>, |(padright)<total width>(with)<char>
Returns: Makes sure that the string has total width of characters, filling the blanks with space
pow
Syntax: |(pow)<value>
Returns: Raises the first argument to the power of the second argument
Example: {%Something|(pow)2%}
prepend
Syntax: |(prepend)<string>
Returns: Prepends (prefixes) the given string to the value
Example: {%Zero|(prepend)ABC%} returns ABC0
regexreplace
Syntax: |(regexreplace)<regex>(with)<dest>
Returns: Replaces the source string with the destination string using regular expressions
replace
Syntax: |(replace)<src>(with)<dest>
Returns: Replaces the source string with the destination string
Example: {%CurrentDocument.DocumentName|(replace)e(with)er%} on "Home" will result in "Homer"
resolve
Syntax: |(resolve)
Returns: Resolves macros
resolvebbcode
Syntax: (resolvebbcode)<true/false>
Returns: Resolves the BB code in the result of the macro
Example: {%MessageText|(resolvebbcode)true%} or {%MessageText|(resolvebbcode)%} writes the resolved BB code such as conversion of [url]… to <a href="…
resolveurl
Syntax: |(resolveurl)
Returns: Resolves the virtual path or URL
Example: Converts ~/Home.aspx to /KenticoCMS/Home.aspx
saveas
Syntax: |(saveas)<key>
Returns: Saves current value to the selector (under current resolving context)
sin
Syntax: |(sin)
Returns: Sine
Example: {%Something|(sin)%}
sqlescape
Syntax: |(sqlescape)
Returns: Replaces ' with '' (SQL escapes)
sqrt
Syntax: |(sqrt)
Returns: Square root
Example: {%Something|(sqrt)%}
startswith
Syntax: |(startswith)<string>
Returns: True if value starts with given string
striptags
Syntax: |(striptags)
Returns: HTMLHelper.StripTags
substring
Syntax: |(substring)<start>, |(substring)<start>;<length>
Returns: The part of the string after the given index
Example: {%CurrentDocument.DocumentName|(substring)1;3%} on document "News" will give you "ews"
tan
Syntax: |(tan)
Returns: Tangent
Example: {%Something|(tan)%}
tobool
Syntax: (tobool)<default value>
Returns: Conversion to Boolean, uses the (truevalue), (falsevalue) settings as a result
Example: {?onlyvalid|(tobool)true?} writes false if onlyvalid querystring parameter is false, else returns true
todatetime
Syntax: (todatetime)<default value>
Returns: Conversion to DateTime, uses the (culture) settings
Example: {?targettime|(todatetime)?} converts the targettime query parameter to date time or DateTime.MinValue
todouble
Syntax: (todouble)<default value>
Returns: Conversion to Double (uses the culture settings)
Example: {?price|(todouble)10.2?} converts the price query parameter to double or 10.2
toguid
Syntax: (toguid)<default value>
Returns: Conversion to GUID
Example: {?userguid|(toguid)?} converts the userguid query parameter to Guid or Guid.Empty
toint
Syntax: (toint)<default value>
Returns: Converts the result to integer, if not successful, uses the default value.
Example: {?tagid|(toint)0?} writes the valid tagid from querystring or 0 if not valid
tolower
Syntax: |(tolower)<true>
Returns: Converts the result to lowercase
Example: {%DocumentName|(tolower)true%} or {%DocumentName|(tolower)%} writes black & white
toupper
Syntax: |(toupper)<true>
Returns: Converts the result to uppercase
Example: {%DocumentName|(toupper)true%} or {%DocumentName|(toupper)%} writes BLACK & WHITE
trim
Syntax: |(trim), |(trim)<chars>
Returns: Removes the white spaces on the beginning and end of the value
Example: {%EmptyString|(append)ABCD|(trim)AD%} returns BC
trimend
Syntax: |(trimend), |(trimend)<chars>
Returns: Does the trimming only on the end of the string
trimstart
Syntax: |(trimstart), |(trimstart)<chars>
Returns: Does the trimming only on the end of the string
truevalue
Syntax: |(truevalue)<value>
Returns: Output settings for the positive output of the comparisson
Example: {%UserName|(equals)administrator|(truevalue)Yes|(falsevalue)No%} writes Yes if the user is administrator
unresolveurl
Syntax: |(unresolveurl)
Returns: Unresolves the URL
urlencode
Syntax: |(urlencode)<true/false>
Returns: HttpUtility.UrlEncode
Example: {%DocumentName|(urlencode)true%} or {%DocumentName|(urlencode)%} writes the URL encoded document name, such as All%20items

Complex macros

Nested
Syntax: {%Something|(equals)Something|(truevalue){(1)%Something%(1)}%}
Returns: Nested macro
Example: {%DocumentName|(equals)Home|(truevalue){(1)%param%(1)}%}
Nested #2
Syntax: {%Something|(equals)Something|(truevalue){(1)%Something%(1)}%}
Returns: Nested macro
Example: {%DocumentMenuCaption|(equals)|(truevalue){(1)%DocumentName%(1)}|(falsevalue){(2)%DocumentMenuCaption%(2)}%}
Nested #3
Syntax: {%Something|(equals)Something|(truevalue){(1)%Something%(1)}%}
Returns: Nested macro
Example: {(1)%DocumentMenuCaption|(equals)|(truevalue){(2)%DocumentName%(2)}|(falsevalue){(3)%DocumentMenuCaption%(3)}%(1)}
Web part container title (nested)
Syntax: {%Something|(equals)Something|(truevalue){(1)%Something%(1)}%}
Returns: Nested macro
Example: {(1)%ContainerTitle|(equals)|(truevalue)|(falsevalue)<h2>{(2)%ContainerTitle%(2)}</h2>%(1)}