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,
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);
}
}
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);
}
}
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
精彩评论