Pagination Helper for CakePHP

In one of my CMS projects, I ran across a case where the user created a post of very long content that scrolled endlessly down the page. In an effort to make the content more easily readable, I created a Pagination helper that breaks that content into discrete blocks of content with "next" and "prev" links.

The visibility of the content is controlled through script.aculo.us Effects.

The benefit of using this helper is that all of the content is still on the page, for SEO.

To use this helper, all you have to do is add "[page]" markers in your content, which indicate places where you want the content to break into a new "page".

Enable the helper in your controller:

<?php
var $helpers = array("Html""Pagination");
?>

In your view, render the paginated content:


<?php echo $pagination->paginateContent($content_for_layout); ?>

Helper

<?php
class PaginationHelper extends Helper
{
    
/**
     * Parses a string of content for [page] blocks, and replaces them with div tags for dynamic control over
     * content section visibility.
     * 
     * <a href="http://twitter.com/access">@access</a> public
     * <a href="http://twitter.com/since">@since</a> 1.0.0
     * <a href="http://twitter.com/param">@param</a> string $strContent The content to parse and paginate
     * <a href="http://twitter.com/return">@return</a> string The paginated content
     * 
     **/
    
function paginateContent($strContent)
    {
        
$strPaginated '';
        
$arrSections explode('[page]'$strContent);
        if(
count($arrSections) > 0)
        {
            for(
$i=0;$i<count($arrSections);$i++)
            {
                
$arrSections[$i] = preg_replace("/<br([^>]+)>/i"""$arrSections[$i], 1);
                
$curIndex $i 1;
                
$prevIndex $i == $curIndex 1;
                
$nextIndex $i >= count($arrSections) ? count($arrSections) : $curIndex 1;
                
                
$pageID 'page'.$curIndex;
                
$nextPage 'page'.$nextIndex;
                
$prevPage 'page'.$prevIndex;
                
                
// Handle first block
                
if($i == 0)
                {
                    
$strPaginated .= '<div id="'.$pageID.'" class="contentblock">'.$arrSections[$i];
                    if(
count($arrSections) > 1)
                    {
                                            
$strPaginated .= '<a class="pagelink" href=javascript:void(0); onclick="switchPage(\''.$nextPage.'\');">More ></a>';
                    }
                                        
$strPaginated .= '</div>';
                }
                else
                {
                    
// Hide the other blocks
                    
$strPaginated .= '<div id="'.$pageID.'" class="contentblock" style="display:none;">'.$arrSections[$i];
                    
                    if(
$curIndex count($arrSections))
                    {
                        
$strPaginated .= '<a class="pagelink" href=javascript:void(0); onclick="switchPage(\''.$nextPage.'\');">More ></a>';
                    }
                    
                    
$strPaginated .= '<a class="pagelink" href=javascript:void(0); onclick="switchPage(\''.$prevPage.'\');">< Prev</a>';
                    
                    
$strPaginated .= '</div>';
                }
            }
            
            return<<<PAGE_CODE
$strPaginated            
<script type="text/javascript">
var currentPage = 'page1';
</script>
PAGE_CODE;
            
        }
        return 
$strContent;
    
    } 
// End Function


// End Class
?>

You'll also need to add some javascript code to handle the switching of visible content blocks:

[js]
function switchPage(thePage)
{
if(window.currentPage)
{
if(thePage != window.currentPage)
{
new Effect.Fade(window.currentPage);
window.currentPage = thePage;
new Effect.Appear(thePage, {delay:0.5});
}
}
}
[/js]

[tags][/tags]

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

[...] Check it out here.

[...] Check it out here. [...]

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

About Erich

Erich is a web developer and a native New Englander who is passionate about life, the universe, and everything.

He is a Drupal consultant, previously employed as a senior developer at Harvard University, working on the IQSS OpenScholar project.  Prior to joining the team at Harvard, he was the engineering manager at CommonPlaces e-Solutions, in Hampstead, NH, contributing as the lead engineer on the Greenopolis.com and Twolia.com.

Erich is active in the Drupal community, having contributed modules and patches to the community. He presented at DrupalCon in Szeged Hungary, and co-presented at DrupalCon 2009 in Washington, DC.

Erich lives in New Hampshire with his wife, two sons, and three weimaraners.  When not writing code, Erich enjoys landscaping and woodworking.

Faceted search

Categories

Content type

Project types

Artwork Type

Artwork Tags

Recent comments

Activity Stream

August 29, 2011

August 25, 2011

August 24, 2011

August 23, 2011

August 15, 2011

August 11, 2011

August 10, 2011

August 9, 2011

August 4, 2011

August 3, 2011