Reputation:
I have a closure inside a function but when it is called the return value is not where i expected it to be.
public function test($name, $content)
{
$test = "\t<div id=\"{$name}\">{$content()}</div>\n";
return $test;
}
Instead of returning this...
<div id="name">content</div>
It instead returns...
content
<div id="name"></div>
If you have any idea how to fix this to display properly then i would be a very happy man, thanks in advance.
Upvotes: 0
Views: 31
Reputation: 33143
You haven't showed the $content()
function, but from the symptoms I assume it prints the content instead of returning it. What happens is that the test()
function calls the $content()
function which displays the content and returns nothing, then test()
returns and something else prints the return value.
To fix it simply have $content()
return the content instead of printing it.
Upvotes: 2
Reputation: 15374
Let the closure execute and catch what's returned. Then insert the result:
public function test($name, $content)
{
$insert = $content();
$test = "\t<div id=\"{$name}\">{$insert}</div>\n";
return $test;
}
Upvotes: -1