Using variables from controller in layout

42 views
Skip to first unread message

Adri Kodde

unread,
May 9, 2012, 1:49:37 PM5/9/12
to Alloy
Is it possible to get variables from a layout that are assigned in the controller?

Controller:
$view->set('foo', 'apple');


Layout:
echo $view->block('test')->content(function() use($view) {
         $view->get('foo');
}

This shows nothing in the block.

Am I doing something wrong?

Vance Lucas

unread,
May 9, 2012, 2:10:03 PM5/9/12
to allo...@googlegroups.com
Try this:

Controller:

$view->set('foo', 'apple');


Layout:
echo $view->block('test')->content(function() use($view) {
         return $view->get('foo'); // have to 'return' string or just 'echo' it out, or end PHP tags and just output text/html
}

Adri Kodde

unread,
May 9, 2012, 2:23:53 PM5/9/12
to allo...@googlegroups.com
Nope, that doesn't work in my case. (in my previous post I forgot the echo, but that was a typo).
You're saying it should work? Then I'll try to find what's going wrong. (for a moment I thought it was just not possible.)

9 mei 2012 20:10
Try this:

Controller:

$view->set('foo', 'apple');


Layout:
echo $view->block('test')->content(function() use($view) {
         return $view->get('foo'); // have to 'return' string or just 'echo' it out, or end PHP tags and just output text/html
}

--
Vance Lucas
http://vancelucas.com




9 mei 2012 19:49

Vance Lucas

unread,
May 9, 2012, 2:32:55 PM5/9/12
to allo...@googlegroups.com
Okay, sorry - my bad. I didn't catch what was happening. The block's "content" method is intended to SET content where the local variables and data is available. Example:

Controller:
$view->set('foo', 'apple');

View (or I guess you *could* do it in the controller):
// This SETS the block content using local data
$view->block('test')->content(function() use($view, $foo) {
         echo $view->set('foo'); // or just "echo $foo;"
}

Layout:
// This echos out the block content that was set in the view above via __toString
echo $view->block('test'); // echo set content


--
Vance Lucas
http://vancelucas.com



postbox-contact.jpg
postbox-contact.jpg

Adri Kodde

unread,
May 9, 2012, 2:38:47 PM5/9/12
to allo...@googlegroups.com
aaahhh! this works, haha.

I'm used to the Twig template system, so this is just all a bit different!

Thanks again for helping me out.

9 mei 2012 20:32
Okay, sorry - my bad. I didn't catch what was happening. The block's "content" method is intended to SET content where the local variables and data is available. Example:

Controller:
$view->set('foo', 'apple');

View (or I guess you *could* do it in the controller):
// This SETS the block content using local data
$view->block('test')->content(function() use($view, $foo) {
         echo $view->set('foo'); // or just "echo $foo;"
}

Layout:
// This echos out the block content that was set in the view above via __toString
echo $view->block('test'); // echo set content


--
Vance Lucas
http://vancelucas.com




9 mei 2012 20:23
Reply all
Reply to author
Forward
0 new messages