annotate lib/IMPL/Web/View/TTView.pm @ 413:af8d359ee4cc ref20150831

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