Delegate to manipulate html before output
This is an open discussion with 3 replies, filed under Extensions.
Search
From the Delegates list, I’d say FrontendOutputPostGenerate
:
( 'FrontendOutputPostGenerate', '/frontend/', array( 'output' => &$output ) )
The callback function always receives a single argument that most people tend to name $context
, which is an array of variables, arrays, objects etc. The best way to inspect this $context
variable is to dump its contents. So in your callback function:
public function myDelegateCallbackFunction($context) { var_dump($context); die; }
In the above example you’d expect to see $context
as an array with a single key output
that contains the rendered source of the page.
And to change its value, simply:
$context['output'] = 'This is the new output.';
thanks for the help nick. I’ve seen this syntax: &$var
, all throughout symphony, and now i see it in that delegate. why is it written with the ampersand before the dollar sign?
Without the ampersand is known as “by value”, so the function receives the argument value as-is. To change the value the function must return
the new value. So for example, a function that adds one to a number working “by value”:
function increment_number($number) { return $number++; } $my_var = 1; $my_var = increment_number($my_var); echo $my_var; // 2
When using dynamically generated callback functions it’s easier to pass arguments by reference. This allows them to be modified directly without the need to return
them afterwards. The function receives a reference to the variable rather than the value of the variable itself.
function increment_number(&$number) { $number++; } $my_var = 1; increment_number($my_var); echo $my_var; // 2
Create an account or sign in to comment.
which delegate(s) would I use to grab the html, modify the html, then spit out the resulting html out onto the page?
also, what’s the best/easiest way to figure out all the arguments that are passed to a callback?