Nile
view release on metacpan or search on metacpan
lib/Nile/View.pm view on Meta::CPAN
my ($self, %vars) = @_;
map {$self->{vars}->{$_} = $vars{$_}} keys %vars;
$self;
}
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
=head2 set()
$view->set(email=>'nile@cpan.org');
$view->set(%vars);
Same as method var() above.
=cut
sub set {
my ($self, %vars) = @_;
map {$self->{vars}->{$_} = $vars{$_}} keys %vars;
$self;
}
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
=head2 get()
$email = $view->get("email");
@user = $view->get(qw(fname lname email website));
Returns one or more template variables values.
=cut
sub get {
my ($self, @name) = @_;
#@{ $h{'a'} }{ @keys }
@{ $self->{vars} }{@name};
}
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
=head2 content()
# get current template content
$content = $view->content;
# set current template content direct
$view->content($content);
Get or set current template content.
=cut
sub content {
my ($self) = shift;
if (@_) {
$self->{content} = $_[0];
return $self;
}
$self->{content};
}
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
sub parse_vars {
my ($self) = @_;
my ($match, $attrs, $content, $cdata, $cdata_content, $closing, %attr, $type, $k, $v);
my $tag = "vars";
$self->{tag} = +{};
my $counter = 0;
#(<$tag(\s+[^\!\?\s<>](?:"[^"]*"|'[^']*'|[^"'<>])*)/>([^<]*)(<\!\[CDATA\[(.*?)\]\]>)?(</$tag>)?)
while ( $self->{content} =~ m{
(<$tag(\s+[^\!\?\s<>](?:"[^"]*"|'[^']*'|[^"'<>])*)/>)|(<$tag(\s+[^\!\?\s<>](?:"[^"]*"|'[^']*'|[^"'<>\/])*)>(.*?)<\/$tag>)
}sxgi ) {
if ($1) {
($match, $attrs, $content) = ($1, $2, undef);
}
else {
($match, $attrs, $content) = ( $3, $4, $5);
if ($content =~ /<\!\[CDATA\[(.*?)\]\]>/is) {
$content = $1;
}
}
#print "match:\n$match \nattrs: $attrs\nvalue: $value\n";
# parse attributes to key and value pairs
%attr = ();
#while ( $attrs =~ /\G(?:\s+([^=]+)=(?:"([^"]*)"|'([^']*)'|(\S+))|(.+))/sg ) {
while ( $attrs =~ m{([^\s\=\"\']+)\s*=\s*(?:(")(.*?)"|'(.*?)')}sxg ) {
$attr{$1} = ( $2 ? $3 : $4 );
}
if ($attr{name}) {
$type = (exists $attr{type} and $attr{type} ne "")? $attr{type} : "var";
$self->{tag}->{$type}->{$attr{name}} = {attr=>{%attr}, match=>$match, content=>$content};
}
elsif (exists $attr{type} and $attr{type} ne "") {
# handle <vars type="perl">print "Hello world";</vars>
# <vars type="plugin" method="Date->date" />
$counter++;
$attr{name} = $tag."_".$counter;
$type = $attr{type};
#say "$attr{name}: $attr{method}, $attr{type}";
$self->{tag}->{$type}->{$attr{name}} = {attr=>{%attr}, match=>$match, content=>$content};
}
#print "\n";
}
#$self->app->dump($self->{tag});
$self;
}
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
sub parse_blocks {
my ($self) = @_;
$self->{block} = +{};
$self->parse_nest_blocks($self->{block}, $self->{content});
return $self;
}
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
sub parse_nest_blocks {
( run in 1.635 second using v1.01-cache-2.11-cpan-39bf76dae61 )