# -------------------------------------------------------------------------------------
# lib::Template::Var
# -------------------------------------------------------------------------------------
#
#    Author   : Jean-Michel Hiver                         (c) Jean-Michel Hiver, 2000.
#    Synopsis : Provides the implementation for the
#    <% var ... %> tag
# -------------------------------------------------------------------------------------
package lib::Template::Var;
use lib::util::Text2Html;
use lib::Template::AbstractTag;
use lib::Exception;
use lib::Template;
use strict;
use vars qw /$DECIMAL_TO_HTML @ISA/;
@ISA = qw /lib::Template::AbstractTag/;


##
# $obj->main;
# -----------
#   Parses and displays the value of a variable.
#   <% var name="foo" %>
#   <% var name="foo" encode_html="1" %>
##
sub main
{
    my $self   = shift;
    my $result = $self->result;
    my $tokens = $self->tokens;    
    my ($token_name, $token_hash) = $self->parse_tag (shift @{$tokens});
    
    # retrieve the variable that needs to replace the tag
    $token_hash->{name} or throw (new lib::Exception ( code => 'VARIABLE_NOT_SET',
						       info => "$token_name should have a 'name' attribute" ) );
    my $varname = $token_hash->{name};
    my $value   = $self->get ($varname);
    $value = @{$value} if (ref $value eq "ARRAY");
    
    $token_hash->{encode_html} or $token_hash->{encode_html} = 0;
    $token_hash->{encode_xml}  or $token_hash->{encode_xml}  = 0;
    $value = text2html ($value) if ($token_hash->{encode_html});
    $value = text2xml ($value)  if ($token_hash->{encode_xml});
    
    push @{$result}, $value;
}


1;







