tags:

views:

15

answers:

1

Hello all,

There could be a vary simple way to achieve what I am trying to do in cakephp but I am unable. here is what I am trying to do : I have a settings file which I read through configuration object and the setting as

$config['pageMeta']=array(
'1'=>array('desc'=>'<?php echo $param1 ?> some data, <?php echo $param2 ?> some content in <?php echo $param3 ?>')
);

What I would like to do is that read the above string as

Configure::read('pageMeta.1.desc'); and somehow evaluate 'param1','param2' and 'param3' replaced with actual values. I am doing this in view layout. I could probably write a function to do string replace but not sure if that is the right way to do.

Any help is appreciated.

thanks aboxy

A: 

"<?php echo $param1 ?>" for the purpose of string replacement within a string is a bad idea. There are better ways to do it:

  1. Cake's own String::insert function:

    $str = 'Hello :place, the :noun is :adjective.';
    echo String::replace($str, array('place' => 'World', 'noun' => 'weather', 'adjective' => 'hot'));
    // Hello World, the weather is hot.
    
  2. PHP's sprintf:

    $str = 'Hello %s, the %s is %s.';
    echo sprintf($str, 'World', 'weather', 'hot');
    // Hello World, the weather is hot.
    

Maybe you're also just looking for standard localization.

deceze
deceze,thanks. was hoping to get some automagic with cakephp but your solutions are completely acceptable. I would stick with first solution. will report back if any issues
aboxy