[英]How to access variables declared in an element from a view
Let's say I have aa view and an element: 假设我有一个视图和一个元素:
myElement{
$aVar = 'abc';
}
myView{
<?php echo $this->element('myElement'); ?>
<?php echo $aVar; ?> (outputs: abc)
}
After I render the element in the view, how can I access the variable declared in that element from my view? 在视图中渲染元素后,如何从视图访问该元素中声明的变量? When I try this, the variable is undefined.
当我尝试此操作时,变量未定义。
If you are using CakePHP 1.3 the API for this is: 如果您使用的是CakePHP 1.3,则此API为:
$this->set('dog', 'Sammy');
$this->getVar('dog'); // Sammy
Ref: https://book.cakephp.org/1.3/en/The-Manual/Developing-with-CakePHP/Views.html#getvar 参考: https : //book.cakephp.org/1.3/en/The-Manual/Developing-with-CakePHP/Views.html#getvar
You mean something like this? 你的意思是这样的吗?
// controller
$this->set('aVar', 'aValue');
// view or layout
echo $this->element('myElement', array('someVar'=>$aVar));
// myElement
echo $someVar; // outputs 'aValue' in 'myElement'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.