Saturday, November 27, 2010

Capture view output for preprocessing

I was in need of a way to wrap some HTML around view outputs, if
needed, thus stopping me from adding the wrapper code to the layout.
This was for a templating issue I was having.

So after spending some time looking through Cakes View class, I cam up
with the code below, hope it helps someone else.

<?php
class WrapperHelper extends AppHelper {

public function afterRender() {
$post = ob_get_flush();
while(ob_get_level() > 0) {
ob_end_clean();
}
$post = "
<div class=\"post\">
<div class=\"post-body\">
<div class=\"post-inner article\">
$post
</div>
<div class=\"cleared\"></div>
</div>
</div>
";
ob_start();
echo $post;
}
}
?>

What it does is this: capture the output buffer, clean all buffers,
modify the rendered view, start output buffering again and echo out
the modified view.

Puk!

Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions.

You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en

No comments: