diff Lib/IMPL/Web/View/TTContext.pm @ 345:72799d1211c5

sync
author cin
date Fri, 27 Sep 2013 16:28:27 +0400
parents 9bdccdf1f50b
children f05634287ac7
line wrap: on
line diff
--- a/Lib/IMPL/Web/View/TTContext.pm	Mon Sep 23 00:09:26 2013 +0400
+++ b/Lib/IMPL/Web/View/TTContext.pm	Fri Sep 27 16:28:27 2013 +0400
@@ -2,7 +2,11 @@
 use strict;
 use Template::Base;
 
+use IMPL::lang qw(is);
 use IMPL::declare {
+	require => [
+	   Document => '-Template::Document'
+	],
 	base => {
 		'Template::Context' => '@_'
 	}
@@ -24,4 +28,138 @@
     return $class->new($args);
 }
 
-1;
\ No newline at end of file
+sub base {
+	my $this = shift;
+	
+	return @_ ? $this->stash->set('base', @_) : $this->stash->get('base'); 
+}
+
+sub tt_ext {
+	my $this = shift;
+	
+	return @_ ? $this->stash->set('tt_ext', @_) : $this->stash->get('tt_ext');
+}
+
+sub find_template {
+	my ($this,$name,@inc) = @_;
+	
+	push @inc, "";
+	my $ext = $this->tt_ext || "";
+    
+	foreach my $dir (@inc) {
+		my $file = "$dir/$name$ext";
+		my $tt = eval { $this->template($file) };
+		
+		return {
+			# if we load a block then we should use the current base directory
+			base => is($tt,Document) ? $dir : $this->base,
+			isDocument => is($tt,Document),
+			name => $name,
+			file => $file,
+			template => $tt
+		} if $tt;
+	}
+	
+	$this->throw(Template::Constants::ERROR_FILE, "$name: not found");
+}
+
+sub require {
+	my ($this,$name) = @_;
+	
+	return $this->stash->get([ 'require', [$name] ]);
+}
+
+1;
+
+__END__
+
+=pod
+
+=head1 NAME
+
+C<IMPL::Web::View::TTContext> - доработанная версия контекста
+
+=head1 DESCRIPTION
+
+Расширяет функции C<Template::Context>
+
+=begin plantuml
+
+@startuml
+
+object FooContext {
+	document
+	this
+	model
+}
+
+object BarContext {
+    document
+    this
+    model
+}
+
+object FooFactoryContext {
+	require = function(){}
+	include = function(){}
+	labels = {...}
+	base = "my/app/view"
+	extends = "my/app/view/bar"
+}
+
+object BarFactoryContext {
+	require = function(){}
+	include = function(){}
+	base = "my/app/view"
+	labels = {...}
+	extends = undefined
+}
+
+object RegistryContext {
+	registry
+}
+
+object DocumentFactoryContext {
+	require = function() {}
+    include = function() {}
+    labels = {...}
+    base = ""
+    extends = undefined
+}
+
+object DocumentContext {
+	this
+}
+
+FooFactoryContext --o BarFactoryContext
+BarFactoryContext --o RegistryContext
+
+FooContext -right-o FooFactoryContext
+BarContext -right-o BarFactoryContext
+
+DocumentFactoryContext -up-o RegistryContext
+DocumentContext -left-o DocumentFactoryContext
+
+Document --> DocumentContext
+
+@enduml
+
+=end plantuml
+
+=head1 MEMBERS
+
+=head2 C<[get,set]base>
+
+Префикс пути для поиска шаблонов
+
+=head2 C<template($name)>
+
+Сначала пытается загрузить шаблон используя префикс C<base>, затем без префикса.
+
+=head2 C<clone()>
+
+Создает копию контекста, при этом C<stash> локализуется, таким образом
+клонированный контекст имеет собственное пространство имен, вложенное в
+пространство родительского контекста.
+
+=cut
\ No newline at end of file