crisu83/yii-formatter

Yii PHP 框架的格式化工具集合。

安装次数: 7,724

依赖项: 1

建议者: 0

安全: 0

星标: 10

关注者: 3

分支: 1

类型:yii-extension

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

This package is auto-updated.

Last update: 2024-08-29 03:26:07 UTC


README

Yii PHP 框架的格式化工具集合。

配置

为了开始使用格式化工具,您需要为您的应用程序配置它。

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

使用方法

配置格式化后,您可以通过调用格式组件来使用它。

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');

格式化模型属性

为了方便地格式化模型属性,您可以给模型附加格式化行为。

function behaviors() {
  return array(
    'formatter' => array(
      'class' => 'path.to.FormatterBehavior',
      'formatters' => array(), // component formatter configurations (name=>config)
    ),
  );
}

当行为被附加时,您可以使用格式化器调用它来格式化任何属性值。

$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');

如果需要,您也可以编写自己的格式化器或使用内联方法来格式化属性值。