Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

Support

Page.find fails only on custom route

Solved by Nico Petri View solution

Started by Nico Petri 8 years ago · 11 replies · 891 views
8 years ago

Hello there,

i ve setup a route like /keyword -> /blog/keyword in the site.yaml. If the the page is invoked by this route the following find doesnt work as expected:

TWIG
  <div class="links">
  {% set _p_links = page.find('/_footer/links') %}
    {{_p_links.content}}
  </div>

The weired thing is that the _p_links object isn't empty. It points to the /blog/keyword/blog.md.

Does a custom route break such find instructions in general?

I use my own theme. All is up to date.

Thank u in advance.
np

8 years ago

@npetri Is '/_footer/links' located at the top level of folder '/user/pages'? Like '/user/pages/_footer/links'.

page.find('/...) Starts searching for a page from the root of the pages folder.

When I use your script in the 'blog-skeleton' in 'blog.html.twig' and use "page.find('/blog/hero-classes')", it correctly prints the content of page 'hero-classes', which is at 'user/pages/01.blog/hero-classes'

Uhh... no, “page.find(’./hero-classes’)” does not find it...

If the file cannot be found, it seems to return a reference to the current page. Not sure if that is logical...

8 years ago

Thank u for checking on that!

Yea the /_footer/links' is within the /usr/page folder. Otherwise it could never be found, but it works when in 'normal' cases. But it behave different if its used in a context of a custom a route. And like u also discovered, it returns an unexpected page, the current one. In my case, the page content of a blog item was pasted in the footer twice, bcs i have two of this find calls there.

My solution atm is not to use a custom route.

last edited 08/11/18 by Nico Petri
8 years ago

Wat is your folder structure?

I do use a custom route and 'page.find()' works properly when using the right folder path.

8 years ago

/pages:

_footer
_poi_nz
01.home
..
..
..
06.blog
..
..
error
imprint

The 01.page is a modular page, the home route is set to this page.

8 years ago

TL;DR: Again, could not reproduce the issue...

Did the following:

  • Fresh blog-skeleton + admin + devtools
  • Created following folder structure:
    TXT
    pages/
      _footer/
         links/
            default.md
      01.home/        // Copied modular with subfolders from skeleton-one-page
      02.blog/        // Renamed original folder from skeleton-blog
         classic-modern-architecture/
            item.md
         .../
         keywords/    // Added
            blogitem/
               item.md
            blog.md
         .../
         blog.md
      images/
    
  • Inserted the following code into 'blog.html.twig' and 'partials/blog-item.html.twig':
    TWIG
    <div class="links">
     {% set _p_links = page.find('/_footer/links') %}
     <p>Template = "{{page.template}}"<br>Url = {{ _p_links.url}}</p>
     {{_p_links.content}}
    </div>
    
  • Inserted into site.yaml'
    TXT
    routes:
    /keyword: /blog/keyword
    

Tested the following urls in the browser:

Note: All tested urls printed correct content of '_footer/links/default.md'

Am I missing some info?

8 years ago

Wow, thank u for ur effort. The code block that doesnt work is inside the footer partial that is used by the base.html.twig. The modular page that is called via / which points to /pages/01.home is extending this base.html.twig.

I can share my whole grav installation as a zip if u want to reproduce it.

Thank you very much.

8 years ago

Changed footer block of 'base.html.twig' to:

TWIG
{% block footer %}
    <div class="links">
       {% set _p_links = page.find('/_footer/links') %}
       <p>Template = "{{page.template}}"<br>page.find('/_footer/links') = {{ _p_links.url}}</p>
       {{_p_links.content}}
    </div>
{% endblock %}

In the footer, the same results are displayed as in above tests.

8 years ago

Preliminary result: I checked some options that maybe related to routes. I had disabled the default language in route. So i enabled this option and now the page.find() call works as expected. I will do some further test...

8 years ago Solution

Hello again,

its all related to the option routeable: false. However, in this case page.find() deliver the current page as a fallback. I don't know if this is suppost to be.

This option was set in the related files by accident. Probably, it was already set in the markdown file that i used as template for this files. In the admin panel, non routable pages have a red plus instead of grey or blue, but i didn't get it.

Thanks.

8 years ago

Does this mean your problem is solved?

Suggested topics

Topic Participants Replies Views Activity
Support · by Thomas, 1 week ago
2 57 15 hours ago
Support · by Anna, 3 days ago
2 65 18 hours ago
Support · by Justin Young, 19 hours ago
1 33 18 hours ago
Support · by Duc , 1 week ago
2 68 5 days ago
Support · by Colin Hume, 1 week ago
2 60 6 days ago