NAME
HTML::Native::List - List of HTML::Native objects
SYNOPSIS
use HTML::Native::List;
# Create some HTML
my $list = HTML::Native::List->new (
[ img => { src => "logo.png" } ],
[ h1 => "Hello!" ],
"This is some text",
);
print $list;
# prints "<img src="logo.png" /><h1>Hello!</h1>This is some text"
DESCRIPTION
An HTML::Native::List object represents a list containing a mixture of HTML::Native objects (themselves representing HTML elements) and plain-text content. HTML::Native uses an HTML::Native::List object to represent the children of an HTML element.
An HTML::Native::List object is a tied array (see perltie). You can treat it as a normal Perl array:
my $list = HTML::Native::List->new (
[ img => { src => "logo.png" } ],
[ h1 => "Hello!" ],
"This is some text",
);
print $list->[1];
# prints "<h1>Hello!</h1>"
Any anonymous arrays within the contents will be automatically converted into new HTML::Native objects. For example:
my $list = HTML::Native::List->new();
push @$list, [ p => "Hello world" ];
print $list;
# prints "<p>Hello world</p>"
See HTML::Native for more documentation and examples.
SUBCLASSING
When subclassing HTML::Native::List, you may wish to override the class that is used by default to hold new elements. You can do this by overriding the new_element()
method:
new_element()
$elem = $self->new_element ( ... )
The default implementation of this method simply calls HTML::Native->new()
:
return HTML::Native->new ( @_ );
ADVANCED
DYNAMIC GENERATION
You can use anonymous subroutines (closures) to dynamically generate portions of an HTML::Native::List array. For example:
my $message;
my $list = HTML::Native::List->new (
[ h1 => "Dynamic content" ],
sub { return $message; },
);
$message = "Hello world!";
print $list;
# prints "<h1>Dynamic content</h1>Hello world!"
The subroutine can return either a single fully-constructed HTML::Native::List object, or a list of arguments ready to be passed to HTML::Native::List->new()
. For example:
sub {
return HTML::Native::List->new (
[ img => { src => $image } ],
[ p => $message ],
);
}
or
sub {
return ( [ img => { src => $image } ],
[ p => $message ] );
}