clearing Zend_View_Abstract's _vars?

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

clearing Zend_View_Abstract's _vars?

Nathan Bell-3
Perhaps I'm not using Zend_View as intended:

I use the registry in my index.php to create a singleton instance of
Zend_View.  I then set the view's variables in my controllers and
render.   However, sometimes I want to render two views in a single page
request, like so:

someAction() {
        $view = Zend::registry("view");
        $view->firstName = "Roger";
        $view->lastName = "Moore";
        echo $view->render("showPerson.php");

        $view->header = "Data table";
        $view->tableData = array(...);
        echo $view->render("showDataTable.php");
}


So now the view "showDataTable.php" will have all the variables set from
the previous render (firstName, lastName).  The parameters intended for
the first call to render() don't get reset when the view is rendered,
and there is no "clearVars()" method to clear out the view data before
the second call to render().

Perhaps I shouldn't be using a singleton instance of Zend_View.  Or
would it make sense to clear the vars in Zend_View_Abstract? or provide
a clearVars() method?  I'd be happy to submit a patch, but I'm not sure
it's how you intended Zend_View to be used.

The issue with constructing a new Zend_View for every render is that
there are many settings (setViewPath(), addHelperPath(), etc.) that
would have to be set several times in the same *Action() method.

I know several work-arounds (constructing a new Zend_View each time,
extending Zend_View, etc.), so I'm not asking for a workaround, more a
philosophy: should Zend_View be resetting parameters at render() and/or
providing a clearVars() method?

-Nathan
Reply | Threaded
Open this post in threaded view
|

Re: clearing Zend_View_Abstract's _vars?

Art Hundiak
I use Zend_View to render nested templates and would not want the data to
be cleared between renders.  I think using two views would be you best
bet.

It might be nice to have a reset variables.  Trivial to implement.  I'd
suggest posting an enhancement request.

It's sad that most of Zend_View_Abstract methods and data are private.
Otherwise you could simply extend your own view class.  I guess they did
that because rendering occurs inside of a view method so they didn't want
tamplates to be able to get at protected data?  Actually I'm not really
sure why Zend uses so many private methods/variables in their base
classes.  The front controllers have a similiar issue.  I guess I should
start a new thread and not hijack this one.

>
> Perhaps I'm not using Zend_View as intended:
>
> I use the registry in my index.php to create a singleton instance of
> Zend_View.  I then set the view's variables in my controllers and
> render.   However, sometimes I want to render two views in a single page
> request, like so:
>
> someAction() {
> $view = Zend::registry("view");
> $view->firstName = "Roger";
> $view->lastName = "Moore";
> echo $view->render("showPerson.php");
>
> $view->header = "Data table";
> $view->tableData = array(...);
> echo $view->render("showDataTable.php");
> }
>
>
> So now the view "showDataTable.php" will have all the variables set from
> the previous render (firstName, lastName).  The parameters intended for
> the first call to render() don't get reset when the view is rendered,
> and there is no "clearVars()" method to clear out the view data before
> the second call to render().
>
> Perhaps I shouldn't be using a singleton instance of Zend_View.  Or
> would it make sense to clear the vars in Zend_View_Abstract? or provide
> a clearVars() method?  I'd be happy to submit a patch, but I'm not sure
> it's how you intended Zend_View to be used.
>
> The issue with constructing a new Zend_View for every render is that
> there are many settings (setViewPath(), addHelperPath(), etc.) that
> would have to be set several times in the same *Action() method.
>
> I know several work-arounds (constructing a new Zend_View each time,
> extending Zend_View, etc.), so I'm not asking for a workaround, more a
> philosophy: should Zend_View be resetting parameters at render() and/or
> providing a clearVars() method?
>
> -Nathan
>
>