Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add setCurrent() method to Page Collection #3398

Merged

Conversation

Karmalakas
Copy link
Contributor

Lets us have a workaround for #531
Also was a discussion on Discourse

This new method would allow to have expected (not inversed) prev and next items of the collection in Twig
Eg.:

    {% set collection = page.parent.collection({items: '@self.children', filter: {published: true, routable: true}}) %}
    {% set isFirst = collection.isFirst(page.path) %}
    {% set isLast = collection.isLast(page.path) %}

    <section>
        {% if not isFirst %}
            {% do collection.setCurrent(page.path) or collection.prev %}
            <a href="{{ collection.current.url }}">Previous page</a>
        {% endif %}

        {% if not isLast %}
            {% do collection.setCurrent(page.path) or collection.next %}
            <a href="{{ collection.current.url }}">Next page</a>
        {% endif %}
    </section>

Copy link
Member

@mahagr mahagr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not a bad idea as it's not limited to siblings but next/previous items in the whole collection, which can contain any page on the site.

However, there's minor room for improvement.

{
reset($this->items);

while (key($this->items) !== $path && key($this->items) !== null) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would get the key into a variable to speed up the loop.

Copy link
Contributor Author

@Karmalakas Karmalakas Jul 8, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But key setting will have to be inside the loop anyway 🤔 Will see what I can come up with. Also will check how expensive this is (I know it might be 2x faster maybe, but really doubt there will be a significant difference)

Copy link
Contributor Author

@Karmalakas Karmalakas Jul 8, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it worth introducing a variable to gain 250ms in a 1M items collection? 🙂 Ran a few times and one time result was opposite even 🙂
image

Copy link
Member

@mahagr mahagr Jul 8, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's 9% faster. :) Generally I prefer well-written code over repeating the same thing over and over again.

while (($key = key($this->items)) !== null && $key !== $path) {
    next($this->items);
}

@Karmalakas Karmalakas requested a review from mahagr July 8, 2021 21:30
@Karmalakas
Copy link
Contributor Author

Ping @mahagr 🙂
Not sure if re-request of review is enough or should I ping next time right after re-request 😄

@mahagr mahagr merged commit 6dd5e0f into getgrav:develop Jul 22, 2021
mahagr added a commit that referenced this pull request Jul 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants