-
Notifications
You must be signed in to change notification settings - Fork 3
Generic Tales
The generic tales cover a host of useful little utilities you will use throughout your templates. Some of them may seem a little odd at first but as you become accustomed to the way TAL works they will hopefully make sense.
All the generic tales are used in the form Ztal\Tales\Generic. followed by the name of the tale - see the examples for more info.
Change the case of a string
<span tal:content="Ztal\Tales\Generic.uc:option,string" />option may be one of:
- first - Uppercase the first letter of the string (
ucfirstin php) - word - Uppercase the first letter of each word (
ucwordsin php) - all - Uppercase the whole string (
strtoupperin php) - none - Lowercase the whole string (
strtolowerin php)
Convert newlines to
and any special chars to html entities
<span tal:content="Ztal\Tales\Generic.toHTML:string" />String replacement
<span tal:content="Ztal\Tales\Generic.strReplace:string,original,replacement" />This works the same way as str_replace
Count string length or array size
<span class="item" tal:content="Ztal\Tales\Generic.count:keyword,variable">keyword is NOT a normal variable but an absolute string that can be one of:
- string - perform a
strlen - array - perform a
count
Truncate overlong strings and add an ellipsis (...)
<td tal:content="Ztal\Tales\Generic.ellipsis:variable,length" />length is the length at which truncation is required. Note that the elipsis is added in addition to the truncated string so a string truncated to 20 chars will actually be 23 chars long.
Build an array from a string of JSON
<span tal:define=" myVar Ztal\Tales\Generic.fromJsonString:{'name':'bob','gender':'male'}" />Note that:
- An absolute JSON string is expected, not a variable
- Single quotes are required for quoting elements ('' can be used to insert a single quote)
json_decode is used to perform the decoding so it must be supported on your PHP install.
Report the type of a variable
<span tal:content="Ztal\Tales\Generic.phpType:variable" />Currently uses gettype but as gettype is not guaranteed to stay consistent we use a wrapper and will endeavour to keep the output of this tale as consistent as possible.
Return the result of a mod b (a%b in php)
<span tal:content="Ztal\Tales\Generic.mod:a,b" />Note that PHPTAL's built-in support for repeats negates many of the uses of this tale.
Returns boolean true if both parameters are equal (php ==)
<span tal:condition="Ztal\Tales\Generic.equal:a,b" />Returns boolean true when the first parameter is greater than the second (php >)
<span tal:condition="Ztal\Tales\Generic.greaterThan:a,b" />Returns boolean true when all parameters are true.
Note that this tale will accept unlimited parameters.
<span tal:condition="Ztal\Tales\Generic.allTrue:a,b" />Returns the second parameter if the first parameter equates to true
<span tal:content="Ztal\Tales\Generic.isTrue:variable,result" />This tale may seem odd at first but combining it with the TAL chaining expression (|) you can easily return different values based on the state of a variable without needing to use tal:condition blocks.
Format as Zend_Date instance as a string
<span tal:content="Ztal\Tales\Generic.zendDate:variable,format" />format is the format you wish for the string output (as you would pass to Zend_Date's getString method)
Format as number using a supplied Zend_Locale object
<span tal:content="Ztal\Tales\Generic.zendLocaleNumber:variable,localeObject" />Format a number using a supplied Zend_Currency object
<span tal:content="Ztal\Tales\Generic.zendCurrency:variable,currencyObject" />Format a number to a specified number of decimal places
<span tal:content="Ztal\Tales\Generic.numberFormatDecimal:number,decimalPlaces" />