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.

Below is the plugin if you want to use it. I modified it to include the 
English.pm exports since I was unsure if TT would let me do something like:

[% ENV.0 %]

to get $0. I figured it would think I was trying to access ENV[0].


Shay



---------------------------------------------------------------------------------------------------------------------------

package Template::Plugin::ENV;

require 5.004;

use strict;
use Template::Plugin;

use vars qw( $VERSION );
use base qw( Template::Plugin );

$VERSION = sprintf("%d.%02d", q$Revision: 1.00 $ =~ /(\d+)\.(\d+)/);

#==============================================================================
#                      -----  CLASS METHODS -----
#==============================================================================

sub new(){
    my ($class, $context, $params) = @_;

    my $self = bless {
        _CONTEXT => $context,
        _keys => [keys %ENV],
    }, $class;

    $self->{$_} = $ENV{$_} for keys %ENV;
    $self->_importEnglish() if $params->{english};

    return $self;
}


###################################################
# Give same type of iterator usage as normal hash #
###################################################

sub keys(){
    return @{$_[0]->{_keys}};
}


#################################################
# Imports all constants from the English module #
# This is not the default behaviour             #
#################################################

sub _importEnglish(){
    my $self = shift;
    use English;

    for (@English::EXPORT){
        $_ =~ tr/*/$/;
        $self->{substr($_, 1)} = eval $_;
    }
}


1;
__END__



Reply via email to