Template-Lace

 view release on metacpan or  search on metacpan

README.mkdn  view on Meta::CPAN

    details on the match specifications supported).  However we have added two minor bits to
    how [Mojo::DOM58](https://metacpan.org/pod/Mojo::DOM58) works to make some types matching easier.  First, if a match specification
    ends in ':content' that means 'match the content, not the full node'.  In the example case
    "title=\\'title:content'" that would get the text value of the title tag.  Second, in the case
    where you want the match to return a collection of nodes all matching the specification, you
    would prepend a '@' to the front of it (think in Perl @variable means an array variable). In
    the given example "css=\\'@link'" we want the attribute 'css' to be a collection of all the
    linked stylesheets in the current DOM.

    You will use this type of value when you are making components that do complex layout
    and overlays of the current DOM (such as when you are creating a master layout page for
    your website).

In addition to any attributes you pass to a component via a declaration as described above
all components could get some of the following automatic atttributes:

- content

    If the component has content as in the following example

        <prefix-name

README.mkdn  view on Meta::CPAN


    <view-master title=\'title:content'
        css=\'@link'
        meta=\'@meta'
        body=\'body:content'>

So four attributes, all coming from the DOM associated with the 'content' area of this component.  We
grab the content of the title tag and the content of the HTML body tag, as well as the collection (if
any) of the link takes (for css style sheets) and any template specific meta tags.

If you are looking carefully you have noticed instead of a 'process\_dom' method we have a 'on\_component\_add' method.  We could do this with 'process\_dom' but that method runs for every request and since this overlay contains no dynamic request bo...

Here's a sample of the actual result, rendering all the components (you can peek at the repository which has all the code for these examples to see how it all works)

    <html>
      <head>
        <meta startup="Fri Mar 31 08:43:24 2017">
        <meta charset="utf-8">
        <meta content="width=device-width, initial-scale=1" name="viewport">
        <title>
          Things To Do

lib/Template/Lace.pm  view on Meta::CPAN

like "body=\'body:content'.  If you want the contents of a match in the form of a collection of
nodes instead using "body=\'body:nodes'.  You might wish to do this if the contents are large
and you want to avoid unnessary stringification and parsing.

Lastly, in the case where you want the match to return a collection of nodes all matching the 
specification, you would prepend a '@' to the front of it (think in Perl @variable means an
array variable). In the given example "css=\'@link'" we want the attribute 'css' to be a
collection of all the linked stylesheets in the current DOM.

You will use this type of value when you are making components that do complex layout
and overlays of the current DOM (such as when you are creating a master layout page for
your website).

=back

In addition to any attributes you pass to a component via a declaration as described above
all components could get some of the following automatic atttributes:

=over 4

=item content

lib/Template/Lace.pm  view on Meta::CPAN


    <view-master title=\'title:content'
        css=\'@link'
        meta=\'@meta'
        body=\'body:content'>

So four attributes, all coming from the DOM associated with the 'content' area of this component.  We
grab the content of the title tag and the content of the HTML body tag, as well as the collection (if
any) of the link takes (for css style sheets) and any template specific meta tags.

If you are looking carefully you have noticed instead of a 'process_dom' method we have a 'on_component_add' method.  We could do this with 'process_dom' but that method runs for every request and since this overlay contains no dynamic request bound ...

Here's a sample of the actual result, rendering all the components (you can peek at the repository which has all the code for these examples to see how it all works)

    <html>
      <head>
        <meta startup="Fri Mar 31 08:43:24 2017">
        <meta charset="utf-8">
        <meta content="width=device-width, initial-scale=1" name="viewport">
        <title>
          Things To Do

lib/Template/Lace/DOM.pm  view on Meta::CPAN

  } else {
    $_[0]->($self, @_[1..$#_]);
  }
  return $self;
}

sub clone {
  return Storable::dclone(shift);
}

sub overlay {
  my ($self, $cb, @args) = @_;
  local $_ = $self;
  my $overlay_dom = $cb->($self, @args);
  $self->replace($overlay_dom);
}

sub wrap_with {
  my ($self, $new, $target) = @_;
  $target ||= '#content';
  $self->overlay(sub {
    $new->at($target)
      ->content($self);
    return $new;
  });
}

sub repeat {
  my ($self, $cb, @items) = @_;
  my $index = 0;
  my @nodes = map {

lib/Template/Lace/DOM.pm  view on Meta::CPAN


Returns:

      <section>
        <p id='story'>Don&#39;t look down'</p>
      </section>

Isolate running transformaions on a DOM to explicit data.  Makes it easier to create
reusable snips of transformations.

=head2 overlay

Overlay the current DOM with a new one.  Examples a coderef that should return the
new DOM and any additional arguments you want to pass to the coderef.  Example;

    my $dom = Template::Lace::DOM->new(qq[
      <h1 id="title">HW</h1>
      <section id="body">Hello World</section>
      </html>
    ]);

    $dom->overlay(sub {
      my ($dom, $now) = @_; # $dom is also localized to $_
      my $new_dom = Template::Lace::DOM->new(qq[
        <html>
          <head>
            <title>PAGE_TITLE</title>
          </head>
          <body>
            STUFF
          </body>
        </html>

t/dom.t  view on Meta::CPAN

{
  # clone
  {
    ok my $dom = Template::Lace::DOM->new('<h1>Hello</h1>');
    ok my $clone = $dom->clone;
    is "$dom", '<h1>Hello</h1>';
    is "$clone", '<h1>Hello</h1>';
    isnt refaddr($dom), refaddr($clone);
  }
  
  #overlay
  {
    my $dom = Template::Lace::DOM->new(qq[
      <h1 id="title">HW</h1>
      <section id="body">Hello World</section>
      </html>
    ]);

    $dom->overlay(sub {
      my ($dom, $now) = @_; # $dom is also localized to $_
      my $new_dom = Template::Lace::DOM->new(qq[
        <html>
          <head>
            <title>PAGE_TITLE</title>
          </head>
          <body>
            STUFF
          </body>
        </html>



( run in 1.707 second using v1.01-cache-2.11-cpan-49f99fa48dc )