* e.sammer <[EMAIL PROTECTED]> [2002-06-27 06:46]:
> I'm a recent convert from another template module mostly for the
> include behavior of TT.
Welcome.
> What I'd like to do is have generic "components" similar to slashdot's
> login, poll, etc. included in a large number of pages. I'll use a
> login component as an example from here on, but substitute as needed.
OK...
[-- snip --]
> What I wish I had (as hack-ish as it would be) was a TT directive such as:
>
> [% URI /components/login %]
>
> ...that would accept *template* output from a uri AND RECURSIVELY
> include it as the INCLUDE directive does. Of course, I could hack this
> together with lwp and other nasty-such-bits but that is U-G-L-Y not to
> mention expensive.
Since you are using mod_perl, you can take advantage of Apache's
subrequest architecture and a plugin:
package My::Plugin::SubRequest;
use base qw(Template::Plugin);
use Apache;
sub new {
my ($class, $context, $uri) = @_;
my $r = Apache->request;
my $subreq = $r->lookup_uri($uri); # creates a new request_rec
# using $uri as the path,
# but...
my $filename = $subreq->filename; # ...we only want the filename
return bless {
"_r" => $r,
"_FILENAME" => $filename,
"_CONTEXT" => $context,
} => $class;
}
sub output {
my $self = shift;
my $context = $self->{ _CONTEXT };
my $filename = $self->{ _FILENAME };
# Note: this requires ABSOLUTE => 1
return $context->include($filename);
}
1;
Use it as:
[% USE subreq = SubRequest("/components/login") %]
[% subreq.output %]
This is completely untested, of course. I'll test this out as soon as I
get a chance and report on the results...
> For the record, I originally was doing this with apache sub requests
> and re-tie'ing Apache->PRINT to my own package, buffering the
> subrequest's output and including it that way, but that was also
> extemely nasty to work with (as taking the tied print from apache just
> doesn't seem right and actually seemed to upset MP). Generally
> speaking, it was like "hiway robbery output chain" for MP - a yucky
> thought, all told.
Ah. My method still uses subrequests, but I think it's a little
cleaner than retying.
Out of curiosity, though, why aren't you just using INCLUDE directly,
with an INCLUDE_PATH that includes your /components URI (using
File::Spec->catfile($r->document_root, "components") or similar)?
(darren)
--
Occam's Razor:
The explanation requiring the fewest assumptions is probably the
correct one.