Acme-CPANModulesBundle-Import-PerlDancerAdvent-2018

 view release on metacpan or  search on metacpan

devdata/http_advent.perldancer.org_2018_15  view on Meta::CPAN

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<title> Dancer2::Plugin::Paginator - Born Again | PerlDancer Advent Calendar</title>
<link rel="stylesheet" href="/css/style.css" />
<link rel="alternate" type="application/rss+xml" title="All Articles " href="/feed/2018" /> 


<!-- Grab Google CDN's jQuery. fall back to local if necessary -->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">/* <![CDATA[ */
    !window.jQuery && document.write('<script src="/javascripts/jquery.js"><\/script>')
/* ]]> */</script>

<!-- Prettyfy -->
<link href="/css/prettify.css" type="text/css" rel="stylesheet" />
<script type="text/javascript" src="/javascripts/prettify.js"></script>

</head>
<body onload="prettyPrint()">
<div id="page">

<div id="sidebar">
<a href="/" class="homelink">Dancer Advent Calendar</a><br />

<p>
The PerlDancer Advent Calendar is a community-driven project that aims 
to showcase the Dancer Perl web framework.
</p>

<p>
Each day of December until Christmas, one article about Dancer. Stay tuned for new moves!
</p>

<ul id="sidebar-items">
<li>
    <h3>About Dancer</h3>
    <ul class="links">
        <li><a href="http://www.perldancer.org/">Dancer homepage</a></li>
        <li><a href="http://twitter.com/PerlDancer">Official Twitter</a></li>
        <li><a href="http://github.com/PerlDancer/Dancer">Dancer on GitHub</a></li>
        <li><a href="http://github.com/PerlDancer/Dancer2">Dancer 2 on GitHub</a></li>
        <li><a class="feed" href="/feed/2018">RSS</a></li>
    </ul>
</li>
</ul>
</div>


<div id="content">
<div class="pod-document"><h1><a name="dancer2__plugin__paginator___born_again"></a>Dancer2::Plugin::Paginator - Born Again</h1>

<h2><a name="history"></a>HISTORY</h2>

<p>I would call it re-birth. It all started during end of July 2017, I was going through the documentation of <a href="https://metacpan.org/pod/distribution/Dancer2/lib/Dancer2/Plugins.pod">Dancer2::Plugins</a> and stumbled upon <a href="https://meta...
<h2><a name="rebirth"></a>REBIRTH</h2>

<p>Following the <a href="http://neilb.org/2014/01/31/adoption-request.html">advise</a> from Neil Bowers, I contacted the author <b>Blabos de Blebe</b> by email, if he was happy for me take it forward. He not only encouraged me but also gave me permi...
<h2><a name="challenge"></a>CHALLENGE</h2>

<p>After getting the push from the author, my first challenge was to make it compatible with <a href="https://metacpan.org/pod/distribution/Dancer2/lib/Dancer2/Plugins.pod">Dancer2::Plugins</a>. Having done this with other plugins e.g. <a href="https...
<h2><a name="example"></a>EXAMPLE</h2>

<p>Let me show you an example as described in the official document for <a href="https://metacpan.org/release/Dancer2-Plugin-Paginator">Dancer2::Plugin::Paginator</a>.</p>
<pre class="prettyprint">use Dancer2;
use Dancer2::Plugin::Paginator;

get '/list' =&gt; sub {
    my $paginator = paginator(
       'curr'     =&gt; $page,
       'items'    =&gt; rset('Post')-&gt;count,
       'base_url' =&gt; '/posts/page/',
    );

    template 'list', { paginator =&gt; $paginator };
};

true;</pre>

<h2><a name="configuration"></a>CONFIGURATION</h2>

<p>To bring the above example live, we need to tune the configuration slightly as below.</p>
<pre class="prettyprint">plugins:
   Paginator:
      frame_size: 3
      page_size: 7</pre>

<h2><a name="real_hands_on"></a>REAL HANDS-ON</h2>

<p>Having seen the details, now let us try real example. I have got GitHub repository that I use to try new things related to Dancer2 i.e. <a href="https://github.com/manwar/Dancer2-Cookbook">Dancer2 Cookbook</a>. You should find a link "Books" showi...
<h3><a name="example_configuration"></a>Example Configuration</h3>

<pre class="prettyprint">plugins:
  Paginator:
    frame_size: 1
    page_size: 3</pre>

<h3><a name="action"></a>Action</h3>

<pre class="prettyprint">get '/books/:page' =&gt; sub {
    my $books = _books();
    my $curr  = params-&gt;{page} || 1;

    template 'books' =&gt; _paginator($books, $curr);
};

sub _paginator {
    my ($books, $curr) = @_;

    my $items = scalar(@$books);
    my $paginator = paginator(
       curr     =&gt; $curr,
       items    =&gt; $items,
       base_url =&gt; '/books',
       mode     =&gt; 'path'
    );

    my $page_size = $paginator-&gt;page_size;
    my $i = ($curr - 1) * $page_size;
    my $j = ($i + $page_size) - 1;
    my $results = [ @$books[$i..$j] ];

    return {
       results   =&gt; $results,
       paginator =&gt; $paginator,
       prev_l    =&gt; $paginator-&gt;prev,
       next_l    =&gt; $paginator-&gt;next,
     };
}</pre>

<h3><a name="template"></a>Template</h3>

<pre class="prettyprint">&lt;% IF results.size %&gt;
   &lt;ul&gt;
      &lt;% FOREACH book IN results %&gt;
         &lt;% IF book.name %&gt;
            &lt;li&gt;&lt;b&gt;&lt;% book.name %&gt;&lt;/b&gt; &amp;nbsp; &lt;a href="/edit/book/&lt;% book.id %&gt;"&gt;Edit&lt;/a&gt;&lt;/li&gt;
         &lt;% END %&gt;
      &lt;% END %&gt;
   &lt;/ul&gt;
   &lt;br/&gt;
   &lt;hr/&gt;
   &lt;a href="/books/&lt;% prev_l %&gt;"&gt;[prev]&lt;/a&gt;...&lt;a href="/books/&lt;% next_l %&gt;"&gt;[next]&lt;/a&gt;
   &lt;hr/&gt;
&lt;% ELSE %&gt;
   &lt;ul&gt;&lt;li&gt;&lt;b&gt;No book found.&lt;/b&gt;&lt;/li&gt;&lt;/ul&gt;
&lt;% END %&gt;</pre>

<h2><a name="conclusion"></a>CONCLUSION</h2>

<p>This is all you need to get started. If you feel lucky, you can take a look at <a href="https://metacpan.org/pod/Paginator::Lite">Paginator::Lite</a> for more insights. Any questions/suggestions, feel free to get in touch mohammad.anwar@yahoo.com....
<h2><a name="author"></a>AUTHOR</h2>

<p>This article has been written by Mohammad S Anwar for the Perl Dancer Advent Calendar 2018.</p>
<h2><a name="copyright"></a>COPYRIGHT</h2>

<p>No copyright retained. Enjoy.</p>
<p>Mohammad S Anwar</p>
</div>

 <div id="disqus_thread"></div>
    <script type="text/javascript">
        /* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */
        var disqus_shortname = 'danceradvent'; // required: replace example with your forum shortname

        /* * * DON'T EDIT BELOW THIS LINE * * */
        (function() {
            var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
            dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
            (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
        })();
    </script>
    <noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>




</div>



<div id="footer">
Powered by the  
<a href="http://perldancer.org/" title="Perl Dancer - Perl web framework">
Dancer Perl web framework</a>
</div>
</div>


<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-25174467-2']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

</script>
</body>
</html>



( run in 0.646 second using v1.01-cache-2.11-cpan-39bf76dae61 )