fill method
[catagits/HTML-Zoom.git] / lib / HTML / Zoom.pm
index 14401de..6420a0b 100644 (file)
@@ -163,6 +163,90 @@ sub AUTOLOAD {
   }
   return $self
 }
+
+## mst: I've been thinking
+## mst: $zoom->fill($selector => $text) -> $zoom->select($selector)->replace_content($text)
+## mst: same for \$html
+## mst: then if it's an object or an arrayref we assume repeat_content
+
+=head2 fill
+
+  @args = ($selector => $text||\$text) ## $zoom->select($selector)->replace_content($text)
+  @args =>($selector => \@arrayref||$coderef) ## $zoom->select($selector)->repeat_content(\@arrayref)
+
+=cut
+
+sub fill {
+  my $self = shift;
+  my %selection_args = @_;
+  while( my($selector, $args) = each %selection_args) {
+    my $type = ref($args) ? ref($args) : 'SCALAR';
+    if($type eq 'ARRAY' || $type eq 'CODE') {
+      $self = $self->select($selector)->repeat_content($args);
+    } elsif($type eq 'SCALAR') {
+        warn "$selector, $args";
+      $self = $self->select($selector)->replace_content($args);
+    } else {
+      die "I don't know what to do with args of type $type";
+    }
+  }
+  return $self;
+}
+
+=head2 wrap_with
+
+    my $layout = HTML::Zoom->from_html(<<HTML);
+    <html>
+      <head>
+        <title>Default Title</title>
+        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
+       <meta name="keywords" content="my special website" />
+        <link rel="shortcut icon" href="/favicon.ico" />
+           <link rel="stylesheet" href="/css/core.css" type="text/css" />
+      </head>
+      </body id="content-area">
+        <div>Default Content</div>
+      </body>
+    </html>
+    HTML
+
+    my $content = HTML::Zoom->from_html(<<HTML);
+    <html>
+      <head>
+        <title>My Awesome Page</title>
+      </head>
+      </body>
+        <h1>Special Page</h1>
+        <p>Here is some content</p>
+      </body>
+    </html>
+    HTML
+
+    $content
+      ->template($layout)  ## is a wrap object of some sort
+      ->match('title')
+      ->match('body')
+      ->...
+
+    ## ^^ would be similar to:
+
+    my (@title, @body);
+    $content
+      ->select('title')
+      ->collect_content({into => \@title})
+      ->select('body')
+      ->collect_content({into => \@body})
+      ->run;
+
+    $layout
+      ->select('title')
+      ->replace_content(\@title);
+      ->select('body')
+      ->replace_content(\@body);
+      ->...
+
+=cut
+
 1;
 
 =head1 NAME