Page.find fails only on custom route


#1

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:

  <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


#2

@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…


#3

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.


#4

Wat is your folder structure?

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


#5

/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.


#6

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

Did the following:

  • Fresh blog-skeleton + admin + devtools
  • Created following folder structure:
    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’:
    <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’
    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?


#7

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

Changed footer block of ‘base.html.twig’ to:

{% 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.


#9

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…


#10

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.


#11

Does this mean your problem is solved?


#12

Yes, i just tagged it as solved.