Jump to:

3445 Posts in 1031 Topics by 872 members

Template Questions

SilverStripe Forums » Template Questions » Auto paginate long articles

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

Page: 1
Go to End
Author Topic: 1369 Views
  • Xochi
    Avatar
    Community Member
    6 Posts

    Auto paginate long articles Link to this post

    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.

  • Xochi
    Avatar
    Community Member
    6 Posts

    Re: Auto paginate long articles Link to this post

    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/>';

  • Ryan M.
    Avatar
    Community Member
    309 Posts

    Re: Auto paginate long articles Link to this post

    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

    1369 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.