Forum
Paginate results rendered using perch_template or...
I'm displaying a set of search results limited to PerchShop and paginating them. This works great.
I'm now trying to access a custom flag on the category for each product so that i can render a different URL for these search results.
On search.php I have the following which renders the search results and pagination correctly but the call to each
does not seem to do anything. Is this implemented/supported. Getting this to work would be the ideal solution.
perch_content_search(perch_get('q'),[
'apps' => ['PerchShop'],
'count' => 10,
'paginate' => true,
'page-links' => true,
'page-link-style' => 'all',
'each' => function($item) {
$cats = perch_categories([
'set' => 'products',
'skip-template' => true,
'filter' => 'catID',
'match' => 'eq',
'value' => $item['category'][0]
], true);
$item['webshop'] = $cats[0]['webshop'];
return $item;
}
]);
Secondly I tried the following to get the results and loop through myself getting the category for each. This works but I lose the pagination.
$results = perch_content_search(perch_get('q'),[
'apps' => ['PerchShop'],
'count' => 10,
'paginate' => true,
'page-links' => true,
'page-link-style' => 'all',
'skip-template' => true
],true);
foreach($results as &$result) {
$cats = perch_categories([
'set' => 'products',
'skip-template' => true,
'filter' => 'catID',
'match' => 'eq',
'value' => $result['category'][0]
], true);
$result['webshop'] = $cats[0]['webshop'];
}
perch_template('search/search-result-content.html', $results);
The templates of the above two calls are the same aside from the namespace the first uses search
and the other content
.
<perch:before>
<h1 class="lined">Search results for “<perch:search id="search_key" />”</h1>
<ul>
</perch:before>
<li class="<perch:search id="perch_item_odd" />">
<a href="<perch:search id="result_url" replace="/shop|" />">
<h4><perch:search id="result_title" /> (<perch:search id="webshop" />)</h4>
<perch:if exists="result_excerpt"><p class="excerpt">…<perch:search id="result_excerpt" encode="false" />…</p></perch:if>
</a>
</li>
<perch:after>
</ul>
<perch:if exists="paging">
<ul class="pagination">
<perch:if exists="not_first_page">
<li class="prev-page"><a href="<perch:search id="prev_url" type="hidden" encode="false"/>" class="icon-arrow-left"></a></li>
</perch:if>
<!--*<li class="active">Page <perch:search id="current_page" type="hidden" /> of <perch:search id="number_of_pages" type="hidden" /></li>*-->
<perch:search id="page_links" encode="false" />
<perch:if exists="not_last_page">
<li class="next-page"><a href="<perch:search id="next_url" type="hidden" encode="false" />" class="icon-arrow-right"></a></li>
</perch:if>
</ul>
</perch:if>
<perch:showall/>
</perch:after>
<perch:noresults>
<perch:if exists="search_key">
<h1>Search results for “<perch:search id="search_key" />”</h1>
<perch:else />
<h1>Search</h1>
</perch:if>
<perch:if exists="search_key">
<p>Sorry, there are no results for “<perch:search id="search_key" />”.</p>
</perch:if>
</perch:noresults>
Is it possible to get either of the above scenarios to work? Have I done something silly to prevent them from working as expected? Finally, Is there an alternative/better way to achieve this?
Thanks
I would use the
each
callback. What error are you getting?Drew, it doesn't look like PerchContent->search_content() checks for the 'each' option.
No error at all. It all runs just fine, but the each callback does not appear to do anything. Should it work on
perch_content_search()
?Ah right, I missed you were using
perch_content_search()
. Pagination is done by the data provider, not by the template. If you want to paginate you'll need to manage it yourself within the PHP before passing the result to the template.Apologies for the delayed reply and thanks Drew.
It would be great if the
each
callback was available onperch_content_search()
. Something for the future perhaps?Yes, quite likely.