Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/crisu83/yii-formatter
A collection of formatters for the Yii PHP framework.
https://github.com/crisu83/yii-formatter
Last synced: 13 days ago
JSON representation
A collection of formatters for the Yii PHP framework.
- Host: GitHub
- URL: https://github.com/crisu83/yii-formatter
- Owner: crisu83
- Created: 2013-03-26T20:20:00.000Z (almost 12 years ago)
- Default Branch: master
- Last Pushed: 2013-05-22T13:05:52.000Z (over 11 years ago)
- Last Synced: 2025-01-01T21:32:54.150Z (21 days ago)
- Language: PHP
- Size: 161 KB
- Stars: 10
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
yii-formatter
=============A collection of formatters for the Yii PHP framework.
## Setup
In order to start using the formatters you need to configure it for your application.
```php
'components' => array(
'format' => array(
'class' => 'path.to.Formatter',
'formatters' => array(), // global formatter configurations (name=>config)
),
),
```## Usage
Once you have configured the formatted you can use it by calling the format component.
```php
Yii::app()->format->boolean($published);
Yii::app()->format->currency($price, array('currency' => 'EUR'));
Yii::app()->format->dateTime($updatedAt, array('dateWidth' => 'short', 'timeWidth' => 'short');
Yii::app()->format->runFormatter('path.to.MyFormatter', '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.
```php
function behaviors() {
return array(
'formatter' => array(
'class' => 'path.to.FormatterBehavior',
'formatters' => array(), // component formatter configurations (name=>config)
),
);
}
```When the behavior is attached you can call it to format any attribute value using a formatter.
```php
$model->formatAttribute('boolean', 'published');
$model->formatAttribute('currency', 'price', array('price' => 'EUR'));
$model->formatAttribute('dateTime', 'updatedAt', array('dateWidth' => 'short', 'timeWidth' => 'short'));
$model->formatAttribute('path.to.MyFormatter', 'foo');
$model->formatAttribute('myFormattingMethod', 'bar');
```You can also write your own formatters or use inline methods for formatting attribute values if necessary.