I need to extend the number helper to include other currencies. Using the "addFormat" function in the number helper, I have created a new "Currenci开发者_开发知识库esHelper" to add these currencies.
<?php
class CurrenciesHelper extends NumberHelper {
I know I need to make it possible for this helper to run this function immediately. What am I missing to contain this so it runs and adds this format?
$this->Number->addFormat('CAD', array(
'before'=>'$',
'after' => false,
'zero' => 0,
'places' => 2,
'thousands' => '.',
'decimals' => ',',
'negative' => '()',
}
Starting CakePHP 2.1, you don't need to extend the helper to get this done. NumberHelper
has been re-factored into CakeNumber
class. If you go through the code, you can see that the formats are now stored as a static array.
That helps us configure currency formats within app/Config/core.php
, like the following:
App::uses( 'CakeNumber', 'Utility' );
CakeNumber::addFormat(
'CAD',
array(
'before' => '$ ', 'after' => false,
'zero' => 0, 'places' => 2, 'thousands' => '.',
'decimals' => ',', 'negative' => '()', 'escape' => true
)
);
// ... and any more definitions to follow.
Once your currency definitions are part of the core configuration, you can use them in any view using NumberHelper
like $this->Number->currency( $c, 'CAD' )
.
PREVIOUS ANSWER CHANGED: Due to position of functions, after the fact. Had to change to:
function __beforeRender(){
}
Was causing overwriting of other custom currencies.
By encapsulating it in a construct function, it run on helper construction. I was sure to include the parent constructor just in case.
function __construct() { parent::__construct(); // code goes here }
精彩评论