Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

Template Questions /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Auto paginate long articles


Go to End


3 Posts   1985 Views

Avatar
Xochi

Community Member, 7 Posts

13 October 2010 at 1:28pm

I am building a site for a journal which has a number of long articles. Is there a way for the view template to create a new page for every x number of words or characters and have auto generated <<prev 1 2 3 . . . next >> navigators at the bottom of each page if more than one page? Thanks.

Avatar
Xochi

Community Member, 7 Posts

13 October 2010 at 1:56pm

I found this code from this site http://www.sitepoint.com/forums/showthread.php?t=249193. It is not specific to Silverstripe but wondering if it will work. I am definitely a php newbie so any help on which scripts different parts of this code would go into would be greatly appreciated.

 
class Page
{
var $num;
var $text;
var $sentences;

var $i;
var $perpage;

    function Page($text, $pagenum=1, $perpage=40)
    {
        $this->num = $pagenum;
        $this->perpage = $perpage;
        $this->text = $text;

        $this->start = $this->perpage * ($this->num - 1); // 40*(1-1)=0, 40*(2-1)=40. This is the starting point
        $this->stop = $this->perpage * $this->num; // 40*1=40, 40*2=80, this is the ending point

        $this->sentences();
    }

    function sentences()
    {
        $this->sentences = explode('. ', $this->text); // Use ". " instead of just "." to avoid issues with elipses.
        $this->sentences = array_filter($this->sentences, array($this, 'filter'));
    }

    function filter($in)
    {
        if ($this->i >= $this->start && $this->i < $this->stop)
        {
            $r = true; // Leave it in the array.
        }
        else
        {
            $r = false;
        }
        $this->i++; // Increment for next time.
        return $r;
    }

    function output()
    {
        echo implode('. ', $this->sentences).'.';
    }
}

$str = 'Hello. World. I. Am. Testing.';

$p = new Page($str, 1, 2);
$p->output();
echo '<br/><br/>';

$p = new Page($str, 2, 2);
$p->output();
echo '<br/><br/>';

$p = new Page($str, 3, 2);
$p->output();
echo '<br/><br/>'; 

Avatar
Ryan M.

Community Member, 309 Posts

2 November 2010 at 11:22am

Here's a tutorial on pagination within the SS system: http://doc.silverstripe.org/private:recipes:pagination

You'll have to build your own function that combines pagination with PHP's str_word_count (http://php.net/manual/en/function.str-word-count.php), and use the URL params or $_REQUEST to determine which page to serve up, ie ?page=2 or /article/25/page2