Load More with the new Starterkit

Hi there. I’m trying the to follow the ‘Load More’ tutorial and seem to be stuck. I pretty much don’t know what I’m doing :frowning:

I’m really sorry but if someone could help I would really appreciate it!!! I’m getting a undefined

http://localhost/mysite/undefined.json?limit=NaN&offset=NaN

I have as follows:

templates/projects.php

I’ve searched the docs for ‘compact’ can someone explain to me what that is as well?

<?php snippet('header') ?>   
   <div class="text">
            <h1><?php echo $page->title() ?></h1>
            <?php echo $page->text()->kirbytext(); ?>       
    </div>
 <div class="showcase"><?php snippet('showcase', compact('showcase')); ?></div>
 <button class="load-more">Load more</button>
  
<?php snippet ('footer')?>

snippets/showcase.php

 <div class="showcase">
 	<?php $projects = page('projects')->children()->visible();
          if(isset($limit)) $projects = $projects->limit($limit);?>
    <?php if($page->title() == 'Home'): ?>
         <h1>Recent Work</h1>
    <?php endif; ?>
  <ul class="showcase projects" data-page="<?= $page->url() ?>" data-limit="<?= $limit ?>">
     <?php foreach($projects as $project): ?>
        <li class="showcase-item">
         <figure>
          <a href="<?php echo $project->url(); ?>">
           <?php if($image = $project->images()->sortBy('sort', 'asc')->first()): $thumb = $image->crop(600, 500); ?>
              <img src="<?= $thumb->url() ?>" alt="Thumbnail for <?= $project->title()->html() ?>" class="showcase-image" />
            <?php endif ?>
            <div class="showcase-caption">
              <h3 class="showcase-title"><?php echo $project->title()->html() ?></h3>
            </div>
          </a>
          </figure>
        </li>
     <?php endforeach;?>
  </ul>
 </div>

controllers/projects.php

<?php

return function($site, $pages, $page) {

 $projects = $page->children()->visible();
 $count    = $projects->count();

 // check if the request is an Ajax request and if the limit and offset keys are set
if(r::ajax() && get('offset') && get('limit')) {

// convert limit and offset values to integer
$offset = intval(get('offset'));
$limit  = intval(get('limit'));

// limit projects using offset and limit values
$projects = $projects->offset($offset)->limit($limit);

// check if there are more projects left
$more = $count > $offset + 1;

// otherwise set the number of projects initially displayed
} else {

   $offset  = 0;
   $limit   = 2;
   $projects = $projects->limit($limit);

  }

 return compact('offset', 'limit', 'projects', 'more');

};

templates/projects.json.php

<?php

  $html = '';

  foreach($projects as $project) {

  // reuse the project snippet to create the HTML for each project
 // we need to set the third parameter to true, to return the
 // snippet content instead of echoing it
 $html .= snippet('showcase', compact('showcase'), true);

}

 // add $html and $more to the $data array
$data['html'] = $html;
$data['more'] = $more;

// JSON encode the $data array
echo json_encode($data);

scripts.js

var element = $('.projects');
var url     = element.data('page') + '.json';
var limit   = parseInt(element.data('limit'));
var offset  = limit;

$('.load-more').on('click', function(e) {

$.get(url, {limit: limit, offset: offset}, function(data) {

     if(data.more === false) {
       $('.load-more').hide();
    }

     element.children().last().after(data.html);

    offset += limit;

  });

});

I think its how i have the pages structured but not totally sure.

Thank you!!!

Why do you load the showcase.php snippet, where all the projects are defined again. That does not make sense.

What do you want to have different than in the tutorial?

compact is a more concise way of defining an array and its key/value pairs: http://php.net/manual/en/function.compact.php

Hi! @texnixe! I was just using the latest starterkit that has the showcase snippet with the project foreach loop. That definitely can be moved into the projects.php file.

I don’t really want to do anything different than the existing tutorial.

Thanks for elaborating on compact!

i’ll move the code into the projects page and give it a go. thank you!!

Ok that seemed to work. @texnixe Thank you for your valuable advice once again! To clarify, it wasn’t redundant. My templates/project.php file was slightly different than the snippets showcase.php file in terms of markup and styling. I guess I should of noted that :slight_smile:

Hey there, I got a similar problem and don’t want to open another thread, hope that’s okay.
I just imported everything from the cookbook into a fresh install of Kirby plainkit & started everything up with php -S localhost:8000, added some pages but upon clicking on the button, I get GET HXR http://localhost:8000/projects.json?limit=2&offset=2 / 404 No found.

Something with local development or … ?

Thanks for your reply,
daybugging

No worries. Hmmm…to honest, I’m not really sure without seeing your code. But, you said you just imported it. I dont have plainkit. I used the reg kirby starter. Depending on your set up, is there a way to use a different port - so you lose the :8000? I really cant say for sure without trying it. I’ll try tonight with plainkit and see. I’m using MAMP right now so my local dev is set to a dif port. I dont think that would be it though…

I’ll get back to you in awhile.

best,
Lance