A collection of formatters for the Yii PHP framework.

Installs: 7 724

Dependents: 1

Suggesters: 0

Security: 0

Stars: 10

Watchers: 3

Forks: 1


dev-master 2013-05-22 13:05 UTC

This package is auto-updated.

Last update: 2024-05-29 02:50:20 UTC


A collection of formatters for the Yii PHP framework.


In order to start using the formatters you need to configure it for your application.

'components' => array(
    'format' => array(
        'class' => '',
        'formatters' => array(), // global formatter configurations (name=>config)


Once you have configured the formatted you can use it by calling the format component.

Yii::app()->format->currency($price, array('currency' => 'EUR'));
Yii::app()->format->dateTime($updatedAt, array('dateWidth' => 'short', 'timeWidth' => 'short');
Yii::app()->format->runFormatter('', 'foo');
Yii::app()->format->inline(array($this, 'myFormattingMethod'), 'bar');

Formatting model attributes

To allow for formatting model attributes easily you can attach the formatter behavior to your model.

function behaviors() {
  return array(
    'formatter' => array(
      'class' => '',
      'formatters' => array(), // component formatter configurations (name=>config)

When the behavior is attached you can call it to format any attribute value using a formatter.

$model->formatAttribute('boolean', 'published');
$model->formatAttribute('currency', 'price', array('price' => 'EUR'));
$model->formatAttribute('dateTime', 'updatedAt', array('dateWidth' => 'short', 'timeWidth' => 'short'));
$model->formatAttribute('', 'foo');
$model->formatAttribute('myFormattingMethod', 'bar');

You can also write your own formatters or use inline methods for formatting attribute values if necessary.