FAQ
Hello! I need to implement pagination. Actually I have "pages" array,
"page" param and "per_page" variable.
In my code:

pages_count := math.Floor(float64(len(pages)) / float64(per_page))

then in template I need something like (pseudocode):

{{ if .page - 2 > 0 }}
     {{ $start_page := .page - 2 }}
{{ else }}
     {{ $start_page := 1 }}
{{ end }}

{{ if .page + 2 >= .pages_count }}
     {{ $finish_page := .page + 2 }}
{{ else }}
     {{ $finish_page := .pages_count }}
{{ end }}

<ul>
     {{ for $i := $start_page; $i <= $finish_page; ++$i }}
         <li {{ if $i == .page }} class="current_page" {{ end }}>
             <a href="{{ url "Pages.Show" .$i }}">$i</a>
         </li>
     {{ end }}
</ul>

How to implement this correctly?
Thx

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Krzysztof Kowalczyk at Jul 19, 2014 at 7:42 pm
    The short answer is: you have to move all the computation/logic to your Go code.

    Templates in Go don't support arithmetic (.page -2 or ++$i) or for loops.

    But since it's your own Go code that generates, instead of calculating
    $finish_page in the template, calculate it in your http handler and
    only send the data you need in the model and then use plain range .Foo
    in the template.

    For a specific example you can see how I did pagination for my simple forum:

    - https://github.com/kjk/fofou/blob/master/handle_forum.go#L59 - look
    at the use of "from" url argument

    - https://github.com/kjk/fofou/blob/master/tmpl/forum.html is the
    corresponding template

    - http://forums.fofou.org/sumatrapdf is how it looks in practice (see
    buttons at the bottom)

    -- kjk

    On Sat, Jul 19, 2014 at 1:00 AM, Nikita Loskutov wrote:
    Hello! I need to implement pagination. Actually I have "pages" array, "page"
    param and "per_page" variable.
    In my code:

    pages_count := math.Floor(float64(len(pages)) / float64(per_page))

    then in template I need something like (pseudocode):

    {{ if .page - 2 > 0 }}
    {{ $start_page := .page - 2 }}
    {{ else }}
    {{ $start_page := 1 }}
    {{ end }}

    {{ if .page + 2 >= .pages_count }}
    {{ $finish_page := .page + 2 }}
    {{ else }}
    {{ $finish_page := .pages_count }}
    {{ end }}

    <ul>
    {{ for $i := $start_page; $i <= $finish_page; ++$i }}
    <li {{ if $i == .page }} class="current_page" {{ end }}>
    <a href="{{ url "Pages.Show" .$i }}">$i</a>
    </li>
    {{ end }}
    </ul>

    How to implement this correctly?
    Thx

    --
    You received this message because you are subscribed to the Google Groups
    "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an
    email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Jason Hutchinson at Jul 21, 2014 at 7:19 pm
    I put together a really simple pager
    here: http://play.golang.org/p/rM2cw-g2u7

    It implements one of my favorite pagination formats, which always shows the
    first and last pages, current pages, and at least N pages to either side of
    current.

    I think this could be fairly easily adapted to output an HTML template.
    On Saturday, July 19, 2014 3:00:12 AM UTC-5, Nikita Loskutov wrote:

    Hello! I need to implement pagination. Actually I have "pages" array,
    "page" param and "per_page" variable.
    In my code:

    pages_count := math.Floor(float64(len(pages)) / float64(per_page))

    then in template I need something like (pseudocode):

    {{ if .page - 2 > 0 }}
    {{ $start_page := .page - 2 }}
    {{ else }}
    {{ $start_page := 1 }}
    {{ end }}

    {{ if .page + 2 >= .pages_count }}
    {{ $finish_page := .page + 2 }}
    {{ else }}
    {{ $finish_page := .pages_count }}
    {{ end }}

    <ul>
    {{ for $i := $start_page; $i <= $finish_page; ++$i }}
    <li {{ if $i == .page }} class="current_page" {{ end }}>
    <a href="{{ url "Pages.Show" .$i }}">$i</a>
    </li>
    {{ end }}
    </ul>

    How to implement this correctly?
    Thx
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedJul 19, '14 at 8:00a
activeJul 21, '14 at 7:19p
posts3
users3
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase