Catalyst-View-EmbeddedPerl-PerRequest

 view release on metacpan or  search on metacpan

lib/Catalyst/View/EmbeddedPerl/PerRequest.pm  view on Meta::CPAN

}

sub required {
  my ($self, $required) = @_;
  return $self->attr('required', 'required') if $required;
  return '';
}

sub href {
  my ($self, @href_parts) = @_;
  my $href = $self->safe_concat(@href_parts);
  return $self->attr('href', $href);
}

sub src {
  my ($self, @src_parts) = @_;
  my $src = $self->safe_concat(@src_parts);
  return $self->attr('src', $src);
}

sub data {
  my ($self, $data) = @_;
  return $self->attr('data', $data);
}


sub default_helpers {
  my $class = shift;
  return (
    view  => sub { my ($self, $c, @args) = @_; return $self->view(@args); },
    content => sub { my ($self, $c, @args) = @_; return $self->content(@args); },
    content_for => sub { my ($self, $c, @args) = @_; return $self->content_for(@args); },
    content_append => sub { my ($self, $c, @args) = @_; return $self->content_append(@args); },
    content_prepend => sub { my ($self, $c, @args) = @_; return $self->content_prepend(@args); },
    content_replace => sub { my ($self, $c, @args) = @_; return $self->content_replace(@args); },
    content_around => sub { my ($self, $c, @args) = @_; return $self->content_around(@args); },
    push_style => sub { my ($self, $c, @args) = @_; return $self->push_style(@args); },
    get_styles => sub { my ($self, $c, @args) = @_; return $self->get_styles(@args); },
    push_script => sub { my ($self, $c, @args) = @_; return $self->push_script(@args); },
    get_scripts => sub { my ($self, $c, @args) = @_; return $self->get_scripts(@args); },
    over => sub { my ($self, $c, @args) = @_; return $self->over(@args); },
    attr => sub { my ($self, $c, @args) = @_; return $self->attr(@args); },
    style => sub { my ($self, $c, @args) = @_; return $self->style(@args); },
    class => sub { my ($self, $c, @args) = @_; return $self->class(@args); },
    checked => sub { my ($self, $c, @args) = @_; return $self->checked(@args); },
    selected => sub { my ($self, $c, @args) = @_; return $self->selected(@args); },
    disabled => sub { my ($self, $c, @args) = @_; return $self->disabled(@args); },
    readonly => sub { my ($self, $c, @args) = @_; return $self->readonly(@args); },
    required => sub { my ($self, $c, @args) = @_; return $self->required(@args); },
    href => sub { my ($self, $c, @args) = @_; return $self->href(@args); },
    src => sub { my ($self, $c, @args) = @_; return $self->src(@args); },
    data => sub { my ($self, $c, @args) = @_; return $self->data(@args); },
  );
}

sub inject_helpers {
  my ($class, $sandbox_ns, %helpers) = @_;
  foreach my $helper(keys %helpers) {
    if($sandbox_ns->can($helper)) {
      warn "Skipping injection of helper '$helper'; already exists in namespace $sandbox_ns" 
        if $ENV{DEBUG_TEMPLATE_EMBEDDED_PERL};
      next;
    }
    eval qq[
      package $sandbox_ns;
      sub $helper { \$helpers{'$helper'}->(__SELF, __SELF->ctx, \@_) }
    ]; die "Can't inject helpers: $@" if $@;
  }
}

sub render {
  my ($self, $c, @args) = @_;
  $c->log->debug("Rendering Template: @{[ ref $self ]}") if $c->debug;

  my $rendered = eval {
    $self->render_template($c, @args);
  } || do {
    my $error = $@;
    $c->log->error("Error processing template: $error");
    $c->log->debug($error) if $c->debug;
    return $error;
  };

  $c->log->debug("Template @{[ ref $self ]} successfully rendered") if $c->debug;

  return $rendered;
}

sub render_template {
  my ($self, $c, @args) = @_;

  my $rendered_template = '';
  my @docs = @{$self->_doc};
  foreach my $doc (@docs) {
    my $ns = $doc->{yat}->{sandbox_ns}; 
    no strict 'refs';
    no warnings 'redefine';
    local *{"${ns}::__SELF"} = sub { $self };

    $rendered_template = $doc->render($self, $c, @args);
    $rendered_template = $self->_temple->{auto_escape}
      ? $self->raw($rendered_template)
      : $rendered_template;

    @args = ($rendered_template);
  }

  return $rendered_template;
}

sub read_attribute_for_html {
  my ($self, $attribute) = @_;
  return unless defined $attribute;
  return my $value = $self->$attribute if $self->can($attribute);
  die "No such attribute '$attribute' for view"; 
}

sub attribute_exists_for_html {
  my ($self, $attribute) = @_;
  return unless defined $attribute;
  return 1 if $self->can($attribute);



( run in 2.492 seconds using v1.01-cache-2.11-cpan-71847e10f99 )