Optimize page selectors on search page

Hey guys,

I’m trying to implement Algolia search into a Kirby project here at the moment.

While most things work very fine by now, I have a couple of very long selectors in the PHP code. Is there any way to optimize them? They seem unnecessarily long but it was the only way I got it working now:

    <?php foreach($results['hits'] as $result): ?>
    <li>
        <a href="<?php echo $site->find('parentResourceFolder')->grandChildren()->findBy('uid', $result['uid'])->url() ?>">
            <?php echo $site->find('parentResourceFolder')->grandChildren()->findBy('uid', $result['uid'])->title() ?>
        </a>
        <p>
            <?php echo $site->find('parentResourceFolder')->grandChildren()->findBy('uid', $result['uid'])->text()->kirbytext() ?>
        </p>
        <p><a href="<?php echo $site->find('parentResourceFolder')->grandChildren()->findBy('uid', $result['uid'])->linkurl() ?>">External Link URL</a></p>
        <p>
            Category: <?php echo $site->find('parentResourceFolder')->grandChildren()->findBy('uid', $result['uid'])->category() ?>
        </p>
    </li>
    <?php endforeach ?>

Also, does it matter if I do this once and store information in a variable or does Kirby optimise this itself?

Thanks in advance,
Anselm

Instead of $site->find() you can use the page() helper:

<?= page('parentResourceFolder')->grandChildren()->findBy('uid', $result['uid'])->url() ?>

Thanks, that’s a bit better indeed. However, I initially hoped that as I have a UID of the page I could directly call it in such function, like <?php echo page($result['uid'])->title() ?> but this results in an error Uncaught Error: Call to a member function title() on boolean.

You would have to pass the complete path to the page() helper, not just the UID. Anyway, the UID may not be unique across folders.

Errr. :frowning: I feared that already by its design but why should a UID not be a unique ID? So, is there any way to uniquely identify a page without specifying any parent?

Well, nothing hinders you to use the same UID in different parent folders, either accidentally or by design. So, you could of course make your UIDs unique, but it may happen that they are in fact not unique.

You could use $site->index()->findBy() but that might slow things down in a big website.

I have worked with Algolia for a Kirby site as well. The trick I used was to use $page->id() as the objectID instead of $page->uid(). You can then use the id to get the page you wanted:

    <?php foreach($results['hits'] as $result): ?>
    <?php $p = page($result['objectID']) ?>
    <li>
        <a href="<?php echo $p->url() ?>">
            <?php echo $p->title() ?>
        </a>
        <?php echo $p->text()->kirbytext() ?>
        <p><a href="<?php echo $p->linkurl() ?>">External Link URL</a></p>
        <p>
            Category: <?php echo $p->category() ?>
        </p>
    </li>
    <?php endforeach ?>

Kirby will cache collections, but it will not cache collection filters and finds, so it generally makes sense to store the result yourself, also to keep your code DRY. I have done that in the code I posted above.

Thanks, the trick with the $page->id() as objectID is pretty good, thanks!