Browse files

Applied patch from Bill Moseley for HTML plugin

  • Loading branch information...
1 parent c7430c5 commit 2259a9579a42d095e9ed94e4f555b8879e5b33ef @abw committed Sep 19, 2012
Showing with 89 additions and 1 deletion.
  1. +89 −1 lib/Template/Plugin/HTML.pm
View
90 lib/Template/Plugin/HTML.pm
@@ -29,7 +29,8 @@ sub new {
my ($class, $context, @args) = @_;
my $hash = ref $args[-1] eq 'HASH' ? pop @args : { };
bless {
- _SORTED => $hash->{ sorted } || 0,
+ _SORTED => $hash->{ sorted } || 0,
+ attributes => $hash->{ attributes } || $hash->{ attrs } || { },
}, $class;
}
@@ -42,8 +43,18 @@ sub element {
return "<$name$attr>";
}
+sub closed_element {
+ my ($self, $name, $attr) = @_;
+ ($name, $attr) = %$name if ref $name eq 'HASH';
+ return '' unless defined $name and length $name;
+ $attr = $self->attributes( $attr );
+ $attr = " $attr" if $attr;
+ return "<$name$attr />";
+}
+
sub attributes {
my ($self, $hash) = @_;
+ $hash ||= $self->{ attributes };
return '' unless ref $hash eq 'HASH';
my @keys = keys %$hash;
@@ -54,6 +65,49 @@ sub attributes {
} @keys);
}
+sub add_attributes {
+ my ($self, $attr) = @_;
+ return unless ref $attr eq 'HASH';
+
+ my $cur = $self->{ attributes };
+
+ for (keys %{$attr}) {
+ $cur->{$_} = exists $cur->{$_}
+ ? $cur->{$_} . " $attr->{$_}"
+ : $attr->{$_};
+
+ }
+
+ return;
+}
+
+*add_attribute = \&add_attributes;
+*add = \&add_attributes;
+
+
+sub replace_attributes {
+ my ($self, $attr) = @_;
+ return unless ref $attr eq 'HASH';
+
+ my $cur = $self->{ attributes };
+
+ for (keys %{$attr}) {
+ $cur->{$_} = $attr->{$_};
+ }
+
+ return;
+}
+
+*replace_attribute = \&replace_attributes;
+*replace = \&replace_attributes;
+
+sub clear_attributes {
+ my $self = shift;
+ $self->{ attributes } = { };
+ return;
+}
+
+
sub escape {
my ($self, $text) = @_;
for ($text) {
@@ -109,6 +163,40 @@ Returns the elements of the hash array passed by reference correctly
formatted (e.g. values quoted and correctly escaped) as attributes for
an HTML element.
+=head2 add_attribute(attributes)
+
+This provides a way to incrementally add attributes to the object.
+The values passed in are stored in the object. Calling
+L<element> with just a tag or L<attributes> without an parameters
+will used the saved attributes.
+
+ USE tag = HTML;
+ tag.add_attributes( { class => 'navbar' } );
+ tag.add_attributes( { id => 'foo' } );
+ tag.add_attributes( { class => 'active' } );
+
+ tag.element( 'li' ); # <li class="navbar active" id="foo">
+
+This method has two aliases: add_attribute() and add().
+
+=head2 replace_attribute(attributes)
+
+This will replace an attribute value instead of add to existing.
+
+
+ USE tag = HTML;
+ tag.add_attributes( { class => 'navbar' } );
+ tag.add_attributes( { id => 'foo' } );
+ tag.replace_attributes( { class => 'active' } );
+
+ tag.element( 'li' ); # <li class="active" id="foo">
+
+This method has two aliases: replace_attribute() and replace().
+
+=head2 clear_attributes
+
+Clears any saved attributes
+
=head2 element(type, attributes)
Generates an HTML element of the specified type and with the attributes

0 comments on commit 2259a95

Please sign in to comment.