简体   繁体   English

如何从视图访问元素中声明的变量

[英]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.

相关问题 PHP:如何访问已在其外部声明的函数内的变量? - PHP: how to access variables inside a function that have been declared outside of it? 无法从视图中的 function 访问变量 - Can not access variables from a function in the view 如何在需要/包含声明的PHP文件中使用变量 - How to use variables from php file where require/include is declared 如何从前一个范围获取所有声明的变量? - How do you get all declared variables from the previous scope? Laravel - 如何在视图中使用/访问会话变量? - Laravel - How to use/access session variables in a view? 为什么在视图中没有$ this的情况下可以访问变量? - How come it is possible to access variables without $this in the view? 如何从 Codeigniter 中的库访问 Controller 中声明的静态变量? - How to access static variable declared in Controller from Library in Codeigniter? Php Yii2 如何访问布局中声明的变量到站点? - Php Yii2 How can i access the variables declared in layouts to site? 使用php,如何访问XML模式声明所声明的XML元素的属性? - using php, how do I access XML element's attributes declared by XML Schema declaration? 如何检查所有声明的变量是否都已设置? - How to check if all declared variables are set?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM