NAME

HTML::Tree::Create::Callback::ChildrenPerLevel - Create HTML document by using a callback (and number of elements per level)

VERSION

This document describes version 0.03 of HTML::Tree::Create::Callback::ChildrenPerLevel (from Perl distribution HTML-Tree-Create-Callback), released on 2016-04-01.

SYNOPSIS

use HTML::Tree::Create::Callback::ElemsPerLevel qw(create_html_tree_using_callback);
$tree = create_html_tree_using_callback(
    sub {
        my ($level, $seniority) = @_;
        $id++;
        if ($level == 0) {
            return (
                'body',
                {}, # attributes
                "text before children",
                "text after children",
            );
        } elsif ($level == 1) {
            return ('p', {id=>$id}, "", "");
        } elsif ($level == 2) {
            return (
                'span', {id=>$id, class=>"foo".$seniority},
                'text3.'.$seniority,
                'text4',
            );
        }
    },
    [3, 2],
);
print $tree;

Sample result:

<body>
  text before children
  <p id="2">
    <span class="foo0" id="3">
      text3.0
      text4
    </span>
  </p>
  <p id="4">
  </p>
  <p id="5">
    <span class="foo0" id="6">
      text3.0
      text4
    </span>
  </p>
  text after children
</body>

DESCRIPTION

FUNCTIONS

create_html_tree_using_callback($cb, \@num_children_per_level) => str

This is like HTML::Tree::Create::Callback's create_html_tree_using_callback (in fact, it's implemented as a thin wrapper over it), except that the callback does not need to return:

($element, \%attrs, $text_before, $text_after, $num_children)

but only:

($element, \%attrs, $text_before, $text_after)

The $num_children will be calculated by this function to satisfy total number of children per level specified in \@num_children_per_level. So suppose \@num_children_per_level is [10, 50, 25], then the root element will have 10 child elements, and each child element will have 50/10 = 5 children of their own, but only one out of two of these children will have a child because the number of children at the third level is only 25 (half of 50).

Specifying total number of children per level is sometimes more convenient than specifying number of children per node.

HOMEPAGE

Please visit the project's homepage at https://metacpan.org/release/HTML-Tree-Create-Callback.

SOURCE

Source repository is at https://github.com/perlancar/perl-HTML-Tree-Create-Callback.

BUGS

Please report any bugs or feature requests on the bugtracker website https://rt.cpan.org/Public/Dist/Display.html?Name=HTML-Tree-Create-Callback

When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature.

SEE ALSO

The interface of this module is modeled after Tree::Create::Callback::ChildrenPerLevel.

AUTHOR

perlancar <perlancar@cpan.org>

COPYRIGHT AND LICENSE

This software is copyright (c) 2016 by perlancar@cpan.org.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.