comparison lib/IMPL/Web/View/TTView.pm @ 407:c6e90e02dd17 ref20150831

renamed Lib->lib
author cin
date Fri, 04 Sep 2015 19:40:23 +0300
parents
children
comparison
equal deleted inserted replaced
406:f23fcb19d3c1 407:c6e90e02dd17
1 package IMPL::Web::View::TTView;
2 use strict;
3
4 use JSON;
5 use IMPL::lang qw(hashMerge is);
6 use IMPL::Const qw(:prop);
7 use IMPL::declare {
8 require => {
9 Context => 'IMPL::Web::View::TTContext',
10 Loader => 'IMPL::Code::Loader',
11 Factory => 'IMPL::Web::View::ObjectFactory'
12 },
13 base => [
14 'IMPL::Object' => undef,
15 'IMPL::Object::Autofill' => '@_',
16 'IMPL::Object::Serializable' => undef
17 ],
18 props => [
19 options => PROP_RW,
20 viewBase => PROP_RW,
21 layoutBase => PROP_RW,
22 layout => PROP_RW,
23 tt_ext => PROP_RW,
24 includes => PROP_RW | PROP_LIST,
25 globals => PROP_RW
26 ]
27 };
28
29 sub CTOR {
30 my ($this) = @_;
31
32 $this->tt_ext('tt') unless defined $this->tt_ext;
33 }
34
35 sub display {
36 my ($this,$model,$template,$args) = @_;
37
38 my $context = Context->new($this->options);
39 eval {
40 $context->process('globals' . '.' . $this->tt_ext, $args);
41 };
42 my $layout = delete $args->{layout} || $this->layout;
43
44 return $context->invoke_environment(
45 sub {
46 my $ctx = shift;
47 if ($layout) {
48 return $ctx->invoke_environment(
49 sub {
50 return shift->render(
51 $layout,
52 hashMerge(
53 {
54 content => sub {
55 $ctx->invoke_environment(
56 sub {
57 return shift->display_model($model,$template);
58 },
59 {
60 base => $this->viewBase
61 }
62 )
63 },
64 model => $model
65 }
66 )
67 ); # render
68 },
69 {
70 base => $this->layoutBase,
71 }
72 );
73 } else {
74 return $ctx->invoke_environment(
75 sub {
76 return shift->display_model($model,$template);
77 },
78 {
79 base => $this->viewBase
80 }
81 );
82 }
83 },hashMerge(
84 $this->globals,
85 hashMerge(
86 $args,
87 {
88 includes => scalar($this->includes),
89 tt_ext => $this->tt_ext,
90 debug => sub {
91 warn @_;
92 },
93 is => sub {
94 my ($obj,$class) = @_;
95 if (is($class,Factory)) {
96 return is($obj,$class->factory);
97 } else {
98 return is($obj,$class);
99 }
100 },
101 import => sub {
102 return Factory->new(Loader->safe->Require(shift));
103 },
104 toJSON => sub {
105 return JSON->new()->utf8->pretty->encode(shift);
106 }
107 }
108 )
109 )
110 );
111 }
112
113 1;