customize kaminari pagination template

拟墨画扇 提交于 2019-12-24 11:20:43

问题


I'm having difficulty customizing the default kaminari template.

I've used the generator to create kaminari template files. In kaminari/paginator.html.haml:

= paginator.render do
  %nav.pagination

All I want to do is access the actionview helpers within this, like link_to, or render. I've searched the source code and I can't find any incline of the helper being passed to this render method.

Essentially, being able to do this:

= paginator.render do
  %nav.pagination
    = render :partial => 'custom links'
    = link_to "custom link", custom_path

..would solve my problems.


回答1:


I didn't get a satisfactory answer, and so I'll submit my own solution.

Helpers cannot be used in the paginator.render block.

So, first I generate kaminari custom template files:

rails g kaminari:views default -e haml

Create a new file kaminari/custom.html.haml with the contents:

#pagination
  = paginate custom
  = render :partial => "kaminari/custom_view_file"

Replace kaminari paginator helper (paginate @results) in your view file with:

= render :partial => "kaminari/custom", :object => @results



回答2:


You should be able to do something like this:

# /app/views/kaminari/_paginator.html.erb

= paginator.render do
  %nav.pagination
    = render :partial => 'shared/custom'
    = link_to "custom link", custom_path

Basically, you need to provide the full path of the partial as it probably does not reside in the same directory.

EDIT:

I think Kaminari does not import rails view helpers in the paginate.render block in _paginator.html.erb. To customize the output generated by paginator, you should instead
1. customize the _next_page.erb.html and so on where you can use rails helpers. I don't know why it is so though.
2. If you want to display some common html as a part of paginator, I suggest you put it in layout instead. Paginator is about navigation of pages.

For example, this is the _prev_page.html.erb:

<span class="prev">
  <%= link_to "google", "www.google.com" %>
  <%# link_to_unless current_page.first?, raw(t 'views.pagination.previous'), url, :rel => 'prev', :remote => remote %>
</span>

I just commented out the old code and used a custom link.




回答3:


Just met the same problem. My solution:

= paginator.render do
  %nav.pagination
    = render :partial => 'custom links'
    = @template.link_to "custom link", custom_path

As partial rendering occurs in scope of Paginator, there is possibility to use its instance variable, pointing to template (see https://github.com/amatsuda/kaminari/blob/master/lib/kaminari/helpers/paginator.rb)

Little hacky, but works.



来源:https://stackoverflow.com/questions/10821262/customize-kaminari-pagination-template

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!