如何将字段添加到特定页面模板

How to add fields to specific page templates

我知道如何使用 apostrophe-custom-pages 将字段添加到 pages,但不知道如何仅将字段 x 添加到页面模板 a 和字段y 仅在 beforeConstruct 方法中用于页面模板 b。我已经 console.logselfoptions 参数都传递给函数,但从未在日志中看到模板名称(因此允许我在beforeConstruct 方法。有办法实现吗?

要为不同的页面类型设置不同的架构,您需要为每个模板类型创建一个 apostrophe-custom-pages 子类。

app.js中或分配给每个模块的index.js

const apos = require('apostrophe')({
  shortName: 'my-site',
  modules: {

    'default-pages': {
      extend: 'apostrophe-custom-pages',
      label: 'Default Page',
      name: 'default',
      addFields: [
        {
          name: 'customField',
          label: 'Special Label',
          type: 'string'
        }
      ]
    },
    'wacky-pages': {
      extend: 'apostrophe-custom-pages',
      label: 'Wacky Page',
      name: 'wacky',
      addFields: [
        {
          name: 'partyMode',
          label: 'Turn on party mode',
          type: 'boolean',
          choices: [
            { label: 'Yes', value: true },
            { label: 'No', value: false }
          ]
        }
      ]
    },
    ...

然后在 apostrophe-pages 配置中,您要将这些新子类作为页面选项添加到 types 数组

lib/modules/apostrophe-pages/index.js

  // ... other pages configuration
  types: [
    {
      name: 'default',
      label: 'Default'
    },
    {
      name: 'wacky',
      label: 'Wacky'
    },
]

然后你想创建相应的模板作为 apostrophe-pages 模块中的视图。

lib/modules/apostrophe-pages /
-- views /
-- -- pages /
-- -- -- wacky.html
-- -- -- default.html