Squatting

 view release on metacpan or  search on metacpan

lib/Squatting/View.pm  view on Meta::CPAN

package Squatting::View;

#use strict;
#use warnings;
#no  warnings 'redefine';

#our $AUTOLOAD;

# constructor
sub new {
  my $class = shift;
  my $name  = shift;
  bless { name => $name, @_ } => $class;
}

# name of view
sub name : lvalue {
  $_[0]->{name};
}

# name of view
sub headers : lvalue {
  $_[0]->{headers};
}

# $content = $view->_render($template, $vars)       # render $template
# $content = $view->_render($template, $vars, '_')  # render generic template
sub _render {
  my ($self, $template, $vars, $alt) = @_;
  $self->{template} = $template;
  if (exists $self->{layout} && ($template !~ /^_/)) {
    $template = $alt if defined $alt;
    $self->{layout}($self, $vars, $self->{$template}($self, $vars));
  } else {
    $template = $alt if defined $alt;
    $self->{$template}($self, $vars);
  }
}

# forward to _render()
sub AUTOLOAD {
  my ($self, $vars) = @_;
  my $template = $AUTOLOAD;
  $template =~ s/.*://;
  if (exists $self->{$template} && ref($self->{$template}) eq 'CODE') {
    $self->_render($template, $vars);
  } elsif (exists $self->{_}) {
    $self->_render($template, $vars, '_');
  } else {
    die("$template cannot be rendered.");
  }
}

sub DESTROY { }

1;

=head1 NAME

Squatting::View - default view class for Squatting

=head1 SYNOPSIS

  package App::Views;
  use Squatting ':views';
  our @V = (
    V(
      'example',
      layout => sub {
        my ($self, $v, $content) = @_;
        "(header $content footer)";
      },
      home => sub {

 view all matches for this distribution
 view release on metacpan -  search on metacpan

( run in 0.504 second using v1.00-cache-2.02-grep-82fe00e-cpan-2c419f77a38b )