Shay Harding <[EMAIL PROTECTED]> said something to this effect on 11/20/2001:
> On Tuesday 20 November 2001 02:10 pm, Perrin Harkins wrote:
> > > On Tuesday 20 November 2001 01:53 pm, Scott McWhirter wrote:
> > > Is there a way of using/gaining the perl global variables for use in the
> > > template?
> >
> > Just pass their values in like any other variables if you only want to read
> > them, or write a plugin to access them if you're trying to set them.
> > - Perrin
> 
> I came across this same question yesterday and went through all the docs to 
> see if there was a way that TT would let me do this. I couldn't find one and 
> since I needed the REMOTE_USER value from %ENV on every access of every CGI 
> it wasn't really best (read I was too lazy to modify 20 files) to have to 
> pass %ENV in from each CGI. I only needed the value inside 1 template so I 
> opted to write the plugin and modify the 1 template file.

It seems to me that the simplest plugin looks something like:

  package Template::Plugin::Env;

  use strict;
  use base qw(Template::Plugin);
  use vars qw($AUTOLOAD);

  sub new {
      my ($class, $context) = @_;
      bless { _CONTEXT => $context, _ENV => \%ENV } => $class
  }

  sub AUTOLOAD {
      my $self = shift;
      (my $var = uc $AUTOLOAD) =~ s/.*:://;

      $self->{ _ENV }->{ $var } = shift if @_;

      return $self->{ _ENV }->{ $var } || "";
  }
  1;

I added the uc in AUTOLAOD to make the lookup case-insensitive.

Use it like:

  [% USE env = Env %]
  [% env.remote_addr %]

Finally, you can modify %ENV:

  [% env.squibble("Faz!") %]

(darren)

-- 
People who make peaceful change impossible make violent change
inevitable.


Reply via email to