Mercurial > pub > Impl
comparison Lib/IMPL/Web/View/TTContext.pm @ 345:72799d1211c5
sync
| author | cin |
|---|---|
| date | Fri, 27 Sep 2013 16:28:27 +0400 |
| parents | 9bdccdf1f50b |
| children | f05634287ac7 |
comparison
equal
deleted
inserted
replaced
| 344:f1d67615a5b1 | 345:72799d1211c5 |
|---|---|
| 1 package IMPL::Web::View::TTContext; | 1 package IMPL::Web::View::TTContext; |
| 2 use strict; | 2 use strict; |
| 3 use Template::Base; | 3 use Template::Base; |
| 4 | 4 |
| 5 use IMPL::lang qw(is); | |
| 5 use IMPL::declare { | 6 use IMPL::declare { |
| 7 require => [ | |
| 8 Document => '-Template::Document' | |
| 9 ], | |
| 6 base => { | 10 base => { |
| 7 'Template::Context' => '@_' | 11 'Template::Context' => '@_' |
| 8 } | 12 } |
| 9 }; | 13 }; |
| 10 | 14 |
| 22 delete $args->{CONFIG}; | 26 delete $args->{CONFIG}; |
| 23 | 27 |
| 24 return $class->new($args); | 28 return $class->new($args); |
| 25 } | 29 } |
| 26 | 30 |
| 31 sub base { | |
| 32 my $this = shift; | |
| 33 | |
| 34 return @_ ? $this->stash->set('base', @_) : $this->stash->get('base'); | |
| 35 } | |
| 36 | |
| 37 sub tt_ext { | |
| 38 my $this = shift; | |
| 39 | |
| 40 return @_ ? $this->stash->set('tt_ext', @_) : $this->stash->get('tt_ext'); | |
| 41 } | |
| 42 | |
| 43 sub find_template { | |
| 44 my ($this,$name,@inc) = @_; | |
| 45 | |
| 46 push @inc, ""; | |
| 47 my $ext = $this->tt_ext || ""; | |
| 48 | |
| 49 foreach my $dir (@inc) { | |
| 50 my $file = "$dir/$name$ext"; | |
| 51 my $tt = eval { $this->template($file) }; | |
| 52 | |
| 53 return { | |
| 54 # if we load a block then we should use the current base directory | |
| 55 base => is($tt,Document) ? $dir : $this->base, | |
| 56 isDocument => is($tt,Document), | |
| 57 name => $name, | |
| 58 file => $file, | |
| 59 template => $tt | |
| 60 } if $tt; | |
| 61 } | |
| 62 | |
| 63 $this->throw(Template::Constants::ERROR_FILE, "$name: not found"); | |
| 64 } | |
| 65 | |
| 66 sub require { | |
| 67 my ($this,$name) = @_; | |
| 68 | |
| 69 return $this->stash->get([ 'require', [$name] ]); | |
| 70 } | |
| 71 | |
| 27 1; | 72 1; |
| 73 | |
| 74 __END__ | |
| 75 | |
| 76 =pod | |
| 77 | |
| 78 =head1 NAME | |
| 79 | |
| 80 C<IMPL::Web::View::TTContext> - доработанная версия контекста | |
| 81 | |
| 82 =head1 DESCRIPTION | |
| 83 | |
| 84 Расширяет функции C<Template::Context> | |
| 85 | |
| 86 =begin plantuml | |
| 87 | |
| 88 @startuml | |
| 89 | |
| 90 object FooContext { | |
| 91 document | |
| 92 this | |
| 93 model | |
| 94 } | |
| 95 | |
| 96 object BarContext { | |
| 97 document | |
| 98 this | |
| 99 model | |
| 100 } | |
| 101 | |
| 102 object FooFactoryContext { | |
| 103 require = function(){} | |
| 104 include = function(){} | |
| 105 labels = {...} | |
| 106 base = "my/app/view" | |
| 107 extends = "my/app/view/bar" | |
| 108 } | |
| 109 | |
| 110 object BarFactoryContext { | |
| 111 require = function(){} | |
| 112 include = function(){} | |
| 113 base = "my/app/view" | |
| 114 labels = {...} | |
| 115 extends = undefined | |
| 116 } | |
| 117 | |
| 118 object RegistryContext { | |
| 119 registry | |
| 120 } | |
| 121 | |
| 122 object DocumentFactoryContext { | |
| 123 require = function() {} | |
| 124 include = function() {} | |
| 125 labels = {...} | |
| 126 base = "" | |
| 127 extends = undefined | |
| 128 } | |
| 129 | |
| 130 object DocumentContext { | |
| 131 this | |
| 132 } | |
| 133 | |
| 134 FooFactoryContext --o BarFactoryContext | |
| 135 BarFactoryContext --o RegistryContext | |
| 136 | |
| 137 FooContext -right-o FooFactoryContext | |
| 138 BarContext -right-o BarFactoryContext | |
| 139 | |
| 140 DocumentFactoryContext -up-o RegistryContext | |
| 141 DocumentContext -left-o DocumentFactoryContext | |
| 142 | |
| 143 Document --> DocumentContext | |
| 144 | |
| 145 @enduml | |
| 146 | |
| 147 =end plantuml | |
| 148 | |
| 149 =head1 MEMBERS | |
| 150 | |
| 151 =head2 C<[get,set]base> | |
| 152 | |
| 153 Префикс пути для поиска шаблонов | |
| 154 | |
| 155 =head2 C<template($name)> | |
| 156 | |
| 157 Сначала пытается загрузить шаблон используя префикс C<base>, затем без префикса. | |
| 158 | |
| 159 =head2 C<clone()> | |
| 160 | |
| 161 Создает копию контекста, при этом C<stash> локализуется, таким образом | |
| 162 клонированный контекст имеет собственное пространство имен, вложенное в | |
| 163 пространство родительского контекста. | |
| 164 | |
| 165 =cut |
