You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Page.find()在局部模板中无法使用的技术问题求助

Troubleshooting Your Video Listing Partial

Hey there! Let's figure out why you can't pull in those video child pages in your partial. Here are the most likely fixes based on common static site generator behavior (like Grav, which uses Twig templating similar to your code):

1. Fix the Page Lookup Context

The biggest issue here is probably using page.find('/videos') instead of a site-wide lookup. When you're in a partial, the page variable refers to the current page that's including the partial—not the root videos page. So it's trying to find /videos relative to the current page's path, which won't work if the current page is in a different section.

Replace your lookup with the site-level finder, which always checks from the root of your site:

<a href="{{ url('videos') }}">
  <h6 class="borbottom text-uppercase"><i class="fab fa-youtube"></i> Serke TV</h6>
</a>
{% set videos_parent = site.find('/videos') %}
{% for p in videos_parent.children if p != page %}
  <h3>{{ p.title }}</h3>
{% endfor %}

2. Verify Collection Configuration (If Using page.collection())

If you tried page.collection('videos') earlier, that method relies on a collection defined in the videos page's frontmatter. Make sure your /videos page has a YAML header that defines its child collection, like this:

content:
  items: '@self.children'
  order:
    by: title
    dir: asc

Then you can call the collection from the site-wide page object:

{% set videos_collection = site.find('/videos').collection() %}
{% for p in videos_collection if p != page %}
  <h3>{{ p.title }}</h3>
{% endfor %}

3. Check Route vs. Actual Page Path

You mentioned setting up a videos route—double-check that this route maps directly to your /videos page's actual file path. If the route is pointing to a custom controller instead of the static page, site.find('/videos') might not resolve correctly. Ensure your route configuration doesn't override the page's natural file-based path.

4. Clear Your Site Cache

Static site generators often cache page data to speed up builds. If you've made changes to the videos page or its children, clear your site's cache (most generators have a CLI command like bin/grav clear-cache or an admin panel option) to make sure the partial pulls in fresh, up-to-date data.


内容的提问来源于stack exchange,提问作者Alexander Kim

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 04:30:15