Skip to content

Instantly share code, notes, and snippets.

@ianshea
Last active January 3, 2016 13:09
Show Gist options
  • Save ianshea/8467363 to your computer and use it in GitHub Desktop.
Save ianshea/8467363 to your computer and use it in GitHub Desktop.
Paginated JSON for Craft Example
// requires underscore.js, jquery
$('.load-more-btn').on('click', function( e ){
{
var $this = $(e.currentTarget);
// Precompile Template for loaded items
var tmpl = _.template( $('#tmpl-workitem').html() );
// Run Ajax Request for next page of items
$.get( $this.attr('href') ).done(function( response )
{
// check if next url exists and update load more buton with the new paginated url.
if( response.nexturl )
{
$this.attr('href', response.nexturl );
}
else
{
// there isn't any more items, so we don't have to load more and can remove it.
$this.hide();
}
// Loop through the returned projects and append them to the list
//
$.each( response.projects, function( num, obj )
{
$('.work-item-list').append( tmpl(obj) );
});
});
return false;
});
{
{% paginate craft.entries.section('project').type(['work']).limit( paginationExtends.basicNumber ) as entries %}
{% if paginate.nextUrl %}
"nexturl" : "{{paginate.nextUrl}}",
{% endif %}
"projects" :
[
{% for entry in entries %}
{% set workPoster = craft.assets.first({ childOf: entry, childField: 'workPoster' }) %}
{
"title" : "{{entry.title}}",
"url" : "{{ entry.url }}",
"client" : "{{ entry.parent.title }}",
"thumb" : "{{ workPoster.getURL('listThumb') }}",
"worktype" : "{% for option in entry.workType %}{{ option }}{% if not loop.last%},{% endif %}{% endfor %}"
}{% if not loop.last %},{% endif %}
{% endfor %}
]
{% endpaginate %}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment