Skip to content
Tim Curzon edited this page Feb 28, 2022 · 3 revisions

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.


uc - String Casing

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 (ucfirst in php)
  • word - Uppercase the first letter of each word (ucwords in php)
  • all - Uppercase the whole string (strtoupper in php)
  • none - Lowercase the whole string (strtolower in php)

toHTML

Convert newlines to
and any special chars to html entities

<span tal:content="Ztal\Tales\Generic.toHTML:string" />

strReplace

String replacement

<span tal:content="Ztal\Tales\Generic.strReplace:string,original,replacement" />

This works the same way as str_replace


count

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

ellipsis

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.


fromJsonString

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.


phpType

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.


mod

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.


equal

Returns boolean true if both parameters are equal (php ==)

<span tal:condition="Ztal\Tales\Generic.equal:a,b" />

greaterThan

Returns boolean true when the first parameter is greater than the second (php >)

<span tal:condition="Ztal\Tales\Generic.greaterThan:a,b" />

allTrue

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" />

isTrue

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.


zendDate

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)


zendLocaleNumber

Format as number using a supplied Zend_Locale object

<span tal:content="Ztal\Tales\Generic.zendLocaleNumber:variable,localeObject" />

zendCurrency

Format a number using a supplied Zend_Currency object

<span tal:content="Ztal\Tales\Generic.zendCurrency:variable,currencyObject" />

numberFormatDecimal

Format a number to a specified number of decimal places

<span tal:content="Ztal\Tales\Generic.numberFormatDecimal:number,decimalPlaces" />

Clone this wiki locally