view Lib/IMPL/Web/View/TTView.pm @ 359:833e663796c4

TTView: added view variable to pass rendering context between controls TTView: display function renamed to display_for WebResource: resources now marked with roles for searching a desired resource by a role in the resource chain
author sergey
date Mon, 25 Nov 2013 02:19:31 +0400
parents 9330835535b9
children d5c8b955bf8d
line wrap: on
line source

package IMPL::Web::View::TTView;
use strict;

use JSON;
use IMPL::lang qw(hashMerge is);
use IMPL::Const qw(:prop);
use IMPL::declare {
	require => {
		Context => 'IMPL::Web::View::TTContext',
		Loader  => 'IMPL::Code::Loader'
	},
	base => [
		'IMPL::Object' => undef,
		'IMPL::Object::Autofill' => '@_',
		'IMPL::Object::Serializable' => undef
	],
	props => [
		options => PROP_RW,
		viewBase => PROP_RW,
		layoutBase => PROP_RW,
		layout => PROP_RW,
		tt_ext => PROP_RW,
		includes => PROP_RW | PROP_LIST,
		globals => PROP_RW
	]
};

sub CTOR {
	my ($this) = @_;
	
	$this->tt_ext('tt') unless defined $this->tt_ext;
}

sub display {
	my ($this,$model,$template,$args) = @_;
	
	my $context = Context->new($this->options);
	my $layout = delete $args->{layout} || $this->layout;
	
	return $context->invoke_environment(
		sub {
			my $ctx = shift;
			if ($layout) {
				return $ctx->invoke_environment(
					sub {
						return shift->render(
							$layout,
							hashMerge(
								{
									content => sub {
										$ctx->invoke_environment(
											sub {
												return shift->display_model($model,$template);
											},
											{
												base => $this->viewBase
											}
										)
									}
								}
							)
						); # render
					},
					{
						base => $this->layoutBase,
					}
				);
			} else {
				return $ctx->invoke_environment(
					sub {
						return shift->display_model($model,$template);
					},
					{
						base => $this->viewBase
					}
				);
			}
		},hashMerge(
			$this->globals,
			hashMerge(
				$args,
				{
					includes => scalar($this->includes),
					tt_ext => $this->tt_ext,
					debug => sub {
						warn @_;
					},
					is => sub {
						return is(shift,shift);
					},
					import => sub {
						return Loader->safe->Require(shift);
					},
					toJSON => sub {
						return JSON->new()->utf8->pretty->encode(shift);
					}
				}
			)
		)
	);
}

1;