Jump to content

Sorting by template label in selectors


ZGD
 Share

Recommended Posts

I usually stick to a convention for my template names so each is a single word. However the template label may be more descriptive, for example I have a template called post with the label Blog Post. I am implementing a search function and want to group results into types, so all Blog Posts appear together. Using the selector sort=template works for grouping in order by template name, but I would like the groups to appear alphabetically by label.

TL;DR: Is there a way I can use selectors to sort by template label, something like sort=template.label?

Link to comment
Share on other sites

This one seems to work fine for me

$sorted = $templates->find('id>0')
    ->sort('label') // sort by label
    ->explode(['label', 'name']);

which gives

array (51)
    44 => array (2)
        label => "Blog" (4)
        name => "blog" (4)
    76 => array (2)
        label => "Blog Section" (12)
        name => "section-blog" (12)
    49 => array (2)
        label => "Categories" (10)
        name => "categories" (10)
    57 => array (2)
        label => "Contact" (7)
        name => "contact" (7)
    73 => array (2)
        label => "Hero Section" (12)
        name => "section-hero" (12)

 

Link to comment
Share on other sites

Hmm. I thought you had a <select> that visitors used to filter pages by templates.

For pages,

$pages->find('id>0')->sort(['template.label', 'template.name'])

this one sorts by label, and sorts unlabelled templates by name, but unlabelled templates come after the labelled ones.

image.png.aed026eac59bb9addc1e82de0ac7add3.png

  • Like 1
Link to comment
Share on other sites

7 hours ago, abdus said:

For pages,


$pages->find('id>0')->sort(['template.label', 'template.name'])

@ZGD, this works well if you have a smallish number of pages that you will load into a memory without any limit/pagination. It is applying the sort to the PageArray produced by $pages->find() rather than applying the sort in the database query. It won't work if you need to apply a limit/pagination because each set of pages will be sorted separately.

If you need to sort a larger number of pages by template then I recommend renaming your templates to align with the template label, as you cannot sort by template properties such as label within the $pages->find() - the template sort is handled as a special case within PageFinder.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...