如何在 data.home._children 循环中访问自定义字段

How to access custom fields in a data.home._children loop

我在 lib/modules/apostrophe-custom-pages/index.js

中添加了一些自定义字段

具体来说,我为摘录添加了一个字段,为封面图像添加了图像单例,以及另一个允许用户为页面定义图标的单例。

这些图像是这样定义的:

 {
    name: 'icon',
    label: 'Icon',
    type: 'singleton',
    widgetType: 'apostrophe-images',
    options:{
        limit: 1,
        minSize: [200,200],
        aspectRatio: [1,1],
    },
    controls:{
      movable:false
    },
    image: true
  },
 {
    name: 'coverimg',
    label: 'Header Image',
    type: 'singleton',
    widgetType: 'apostrophe-images',
    options:{
        limit: 1,
        minSize: [400,400],
        aspectRatio: [1,1],
    },
    controls:{
      movable:false
    },
    image: true
  }

我可以在页面上使用以下方法检索封面图片和图标:{% set coverimage = apos.images.first(data.page.coverimg) or null %} ,

但是,我无法在 data.home._children 下的导航中找到图标,如下所示:

    {%- for tab in data.home._children -%}
      {%- set iconImg = apos.images.all(tab.icon) %}
      {{ apos.log(tab) }}
    <li>
      <a class="sidebar-main-link 
      {% if data.page and (apos.pages.isAncestorOf(tab, data.page) or tab._id == data.page._id) %}
        active
      {% endif %}
      " href="{{ tab.slug }}">

        <div class="icon" style="
        backgroung-image: url('{{ apos.attachments.url(image.attachment, { size: 'full'}) }}')  "></div>
        {{ tab.title }}

      </a>
    </li>
  {% endfor %}

这张returns标准missing.svg图片

There is a new tutorial on the apostrophe documentation site which covers this issue in detail.

简短的回答是,您必须配置 apostrophe-pages 模块的 filters 选项来加载相关页面,而不仅仅是非常基本的信息。出于性能原因,此默认行为并不是一件坏事,但要求 Apostrophe 仅加载一个小部件不会减慢太多速度。

// in lib/modules/apostrophe-pages/index.js
module.exports = {
  // other configuration, then...
  filters: {
    ancestors: {
      children: {
        areas: [ 'thumbnail' ]
      }
    }
  }
};