picocms / Pico

Pico is a stupidly simple, blazing fast, flat file CMS.
http://picocms.org/
MIT License
3.81k stars 616 forks source link

Going back to parent page #597

Closed notakoder closed 2 years ago

notakoder commented 2 years ago

I have a directory structure as follows and I am trying to set up a link on each page to the previous index page. ie, a link on file5.md and file6.md pointing to topic.md and a link on file1.md, file2.md, file3.md and topic.md pointing to blog.md.

content
-index.md
-blog.md
-blog
  -file1.md
  -file2.md
  -file3.md
  -topic.md
  -topic
    -file5.md
    -file6.md

I am currently using conditional statements in the main template for to insert the links, like:

{% if (current_page.id starts with "blog/") and not (current_page.id starts with "blog/topic/") %}
           Insert link to blog.md
{% endif %}

This is a tiresome process as I have to insert it for every directory inside content and every sub directory inside blog. Is there a better way (like a twig variable) to go to previous directory's index page that I can apply to all?

PhrozenByte commented 2 years ago

That's not the previous, but parent page. You can use Pico's pages Twig function (see https://picocms.org/in-depth/features/pages-function/):

At some other place you might have heard that Pico’s pages() function also accepts the offset parameter. […] There’s only one common use case: Accessing a page’s parent page. Remember the example above: You’re shop and you want to learn more about your parent. Just pass offset=-1 (i.e. pages("shop", offset=-1)), it will return index. Keep in mind that the pages() function returns pages only, so pages("shop/flowers/daisies", offset=-1) will return nothing, because there’s no shop/flowers/index.md page.

Try this:

{% set parent_page = pages(current_page.id, offset=-1)|first %}
notakoder commented 2 years ago

Yes, parent page it is. Sorry about mislabelling it. :)

I did try using pages() function with offset value of -1 and it almost worked. The url of the links correctly links to the respective parent page. However, a /Array string attached to the end of each url, thus resulting in 404 error. Not sure why. I use folder.md naming convention instead of folder/index.md. To verify that the error is not due to that, I created an index.md file within a folder only to see the same error. Here is my code.

{% set parent_page = pages(current_page.id, offset=-1)|first %}
<a href="{{ parent_page }}">Back</a> 

By the way, the in-depth page is written well. I think there must be a index page for in-depth articles. :)

PhrozenByte commented 2 years ago

parent_page holds the parent page's data. You'll have to use Pico's link Twig filter, the same you'd use it elsewhere.

{% set parent_page = pages(current_page.id, offset=-1)|first %}
<a href="{{ parent_page.id|link }}">Back</a> 

By the way, the in-depth page is written well. I think there must be a index page for in-depth articles. :)

Yes. Even though there are way more issues about our docs :see_no_evil:

notakoder commented 2 years ago

Thank worked. Thanks. On re-reading the doc,

pages("shop") returns the data of page shop/shipping.md

Subtle things you miss. :)

Yes. Even though there are way more issues about our docs see_no_evil

I'll try to make some time for improving them. :)

Thanks for the help by the way.