[Solar-talk] Solar_View_Helper_Capture
Paul M Jones
pmjones at ciaweb.net
Tue Oct 17 09:15:45 PDT 2006
Hi all,
Quoting from <http://solarphp.com/trac/ticket/17> ...
> A good little helper to allow grabbing chunks of content without
> getting into using ob_start etc. For example, if we had a layout
> that had a side_bar, but wanted to set it in the current child
> template file. We would do:
>
> <?php $this->capture('side_bar') ?>
>
> Content for the side_bar! This will be available AFTER the view
> is rendered...
>
> <?php $this->capture() ?>
>
> And the code:
>
> Solar::loadClass('Solar_View_Helper');
>
> class Helper_Capture extends Solar_View_Helper {
>
> private $_captures = array();
>
> function capture($name=false){
>
> if( $name ){
>
> $this->_captures[] = $name; ob_start();
>
> }else{
>
> $name = array_pop($this->_captures); $this->_view->
> $name = ob_get_clean();
>
> } return $this;
>
> }
>
> }
I'm thinking this is well-served by the existing "partial" helper.
Instead of embedding the output to be captured in the same file, you
put that code in a separate file, and retrieve it using partial().
For example:
-- main.php --
<?php $side_bar = $this->partial('_side_bar.php') ?>
-- _side_bar.php --
Content for the side_bar! This will be available AFTER the view
is rendered...
Note also that variables can be passed into the partial scope as
well; see the docs here:
<http://solarphp.com/index.php/docs/read/Solar_View/partial()>
Does anyone here see a common and frequent case where capture() as
outlined above would make more sense than using partial()?
--
Paul M. Jones <http://paul-m-jones.com>
Solar: Simple Object Library and Application Repository
for PHP5. <http://solarphp.com>
Savant: The simple, elegant, and powerful solution for
templates in PHP. <http://phpsavant.com>
More information about the solar-talk
mailing list