Take the 2-minute tour ×
Craft CMS Stack Exchange is a question and answer site for administrators, end users, developers and designers for Craft CMS. It's 100% free, no registration required.

While I know the back button does this perfectly well, I was wondering if there was a good way to "return to" a paginated page using a previous request or something? Wanting to do this server-side rather than relying on JS. Thanks.

share|improve this question
    
Can you elaborate a bit? E.g. are you linking from a paginated entry listing to an entry's actual URL? –  Brandon Kelly Jun 30 at 20:28
1  
Precisely. A blog list view that is paginated. Say you're on pg 5 and choose an entry I want to go back to pg 5 with a text link on entry view versus just the blog landing page. Wasn't sure if it was in the request or session arrays or not. If not, I will make a small plugin. –  Ryan Shrum Jun 30 at 20:47
add comment

3 Answers 3

up vote 4 down vote accepted

The easiest way would be to check craft.request.urlReferrer and see if they just came from the entry index page. If so, provide a link to send them back.

{% set referrer = craft.request.urlReferrer %}
{% set indexBaseUrl = url('articles') %}

{% if referrer and referrer|slice(0, indexBaseUrl|length) == indexBaseUrl %}
    <a href="{{ referrer }}">Go back</a>
{% endif %}

Another, more explicit way to do it would be to actually pass the current page in the query string of the entry URLs, from the entry index page.

{% for entry in entries %}
    {% set url = url(entry.uri, { page: craft.request.pageNum }) %}
    <a href="{{ url }}">{{ entry.title }}</a>
{% endfor %}

Then you could check for that “page” query string param in the single entry template:

{% set indexPage = craft.request.getQuery('page') %}
{% if indexPage %}
    {% set indexUrl = url('articles/' ~ craft.config.pageTrigger ~ indexPage) %}
    <a href="{{ indexUrl }}">Go back</a>
{% endif %}
share|improve this answer
add comment

This is not something built into Craft, but you could accomplish this with a plugin. You could have it run during your plugin's init function. On each page load, it would take the current page and load it into a currentUrl session variable. Then when you went to another page, the previous page url would move from currentUrl to a prevUrl session variable, and the current one would go into currentUrl.

You would write a template variable to check and output them in your templates. You could possibly work with Craft's session flashes, but you would have to remember to tell it not to delete them when you grabbed them.

share|improve this answer
add comment

I'd guess if this existed it would be available through craft.session. Unfortunately, I don't see any reference to a variable for the previous page's URL in the documentation for craft.session:

http://buildwithcraft.com/docs/templating/craft.session

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.