[Templates] Passing $context to the providers, good or bad idea?

Rodney Broom rbroom+tt2 at rbroom.com
Tue Feb 19 23:19:16 GMT 2008


> From: "Perrin Harkins" <perrin at elem.com>
> On Feb 19, 2008 2:35 PM, Yann Kerhervé <yann.kerherve at gmail.com> wrote:
>
> > and less flexible (I probably wouldn't be able
> > to change the user in the template ([% user = my_other_user %]).
> 
> You lost me there.  Don't you pass that in when you call process()?

I think Yann means that he has situations which aren't known at the time of the process() call.


I've had need for TT components to be able to see each other, too. My original need was for the provider to be able to access the context object at fetch time so the template could influence the fetching. Roughly like so:

  # Provider
  sub fetch {
    ($self, $name) = @_;
    $run_time_change = $self->context->stash->get('some_parm');
    if ($run_time_change) {
      return $self->overloaded_content_mechanism($name);
    }

    # ... normal fetch process
  }


Like Yann, I also had concerns. I'm mostly running under Catalyst/mod_perl and have had wierd cleanup issues that required Scalar::Util::weaken(). I have a test project with several overloaded modules such that:
  $provider->can('context')
  $context->can('service')
  $service->can('tt')

Alas, I suddenly ran out of time and haven't been able to test the health of this approach.


---
Rodney Broom




More information about the templates mailing list