Template-Plex

 view release on metacpan or  search on metacpan

lib/Template/Plex.pm  view on Meta::CPAN

use feature qw<say refaliasing>;
no warnings "experimental";


use Log::ger;
use Log::OK;	#Allow control of logging from the command line

use Symbol qw<delete_package>;

use constant::more KEY_OFFSET=>0;
use constant::more DEBUG=>0;

use constant::more {plex_=>0, meta_=>1, args_=>2, sub_=>3,
  package_=>4, init_done_flag_=>5, skip_=>6,
	cache_=>7, slots_=>8, parent_=>9, default_result_=>10, id_=>11, ref_cache_=>12
};

use constant::more KEY_COUNT=>ref_cache_ - plex_ +1;

#Template::Plex::Internal uses the field name constants so import it AFTER
#we define them
use Template::Plex::Internal;

our %top_level_cache;
sub new {
	my ($package, $plex)=@_;
	my $self=[];
  #$self->[plex_]=$plex;
	$self->[cache_]={};
  $self->[slots_]={};
	bless $self, $package;
}

sub get_cache {
	$_[0][cache_];
}

#Returns a template loaded and intialised
sub load {
    my ($self, $path, $vars, %opts)=@_;
		my $template;
		if(ref($self)){
			DEBUG and Log::OK::TRACE and log_trace __PACKAGE__." instance load called for $path";
			\my %fields=$self->args;

			my %options=$self->meta->%{qw<root use base>}; #copy
      if(%opts){
        $opts{caller}=$self; 
      }
      else {
        $options{caller}=$self;
      }
      
			$template=Template::Plex::Internal->new(\&Template::Plex::Internal::_prepare_template, $path, $vars?$vars:\%fields, %opts?%opts:%options);

		}
		else{
			DEBUG and Log::OK::TRACE and log_trace __PACKAGE__." class load called for $path";
			#called on package
      my $dummy=[];
      $dummy->[Template::Plex::meta_]={file=>(caller)[1]}; 
      bless $dummy, "Template::Plex";
      $opts{caller}=$dummy;

			$template=Template::Plex::Internal->new(\&Template::Plex::Internal::_prepare_template, $path, $vars, %opts);

		}
			$template->setup;
			$template;
}

#Returns a template which was already loaded can called from the callers position
#
#TODO: special case where the second argument is a hash ref or undef
# This indicates no id was specified so use implicit cache entry
# path must always be defined.
# eg 
#   cache undef, "path", .... ; #will use explicit cache key
#   cache "path", {var};        #Use implicit cache key
#   cache "path";               #Use implicit cache key
#
# This tidies up the common use case for cached templates
sub cache {
    my $self=shift;
    my @args=@_;

    if(@args ==1){
        # Recalling implicit cache key with path only
        unshift @args, undef;
    }
    elsif(defined($args[1]) and ref($args[1]) eq "HASH"){
      # variables hash ref given, with implicit cache id
      unshift @args, undef;
    }
    else{
      # Expect explicit cache Id
    }

		my ($id, $path, $vars, %opts)=@args;

    #my ($self, $id, $path, $vars, %opts)=@_;
		DEBUG and Log::OK::TRACE and log_trace __PACKAGE__." cache: $path";
		$id//=$path.join "", caller;	#Set if undefined
    #say STDERR "-=----IN CACHE id=$id, path=$path, vars=", %$vars, %opts;
    #use Data::Dumper;
    #say STDERR Dumper [$id, $path, $vars, \%opts];
    #sleep 1;
		if(ref($self)){
      my $c=$self->[cache_]{$id};
      if($c){
          return $c;
      }
      else {
        my $template=$self->load($path, $vars, %opts);
        $template->[id_]=$id;
        $template->[ref_cache_]=$self->[cache_];
        
        $self->[cache_]{$id}//=$template;
      }
		}
		else{



( run in 3.013 seconds using v1.01-cache-2.11-cpan-b16cb0d3907 )