简体   繁体   中英

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:

$this->set('dog', 'Sammy');
$this->getVar('dog'); // Sammy

Ref: 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'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM