tags:

views:

155

answers:

3

Hi,

I am using cakephp for my site. I have stored multiple blocks in database and trying to access the code with following syntax.

foreach($blocks as $block){
    if($block['Block']['position'] == 'left'){
        $str = $block['Block']['value'];
        eval("\"echo $str\";");
     }
}

And i m getting this error;

: Undefined property: View::$requestAction [APP\views\layouts\home.ctp(60) : eval()'d code

Your Help will be highly appreciated.

Thanks,

A: 

Why are you using eval at all? Try this:

foreach($blocks as $block){ 
    if($block['Block']['position'] == 'left'){ 
        $str = $block['Block']['value']; 
        echo htmlentities($str);
     } 
} 
LeguRi
A: 

I have using this code for dynamic blocks on different position in my theme. I have resloved my problem using this mentioned chunk of code.

foreach($blocks as $block){
    if($block['Block']['position'] == 'left'){
        $str = $block['Block']['value'];
        echo $this->requestAction($str);
     }
}
Naveed
A: 

Agreed - though you could get more fancy/optimized and use set::extract like:

<?php
echo implode("",set::extract($blocks,"/Block[position=left]/value"));
// or
echo current(set::extract($blocks,"/Block[position=left]/value"));
?>

http://book.cakephp.org/view/1501/extract

set::extract is often faster than a foreach loop, and the syntax becomes a clean one-liner

zeroasterisk