【问题标题】:How to add fields to specific page templates如何将字段添加到特定页面模板
【发布时间】:2019-11-16 01:20:12
【问题描述】:

我知道如何使用apostrophe-custom-pages 将字段添加到pages,但不知道如何将字段x 仅添加到页面模板a 和字段y 仅添加到页面模板bbeforeConstruct 方法中。我将console.logged 两个selfoptions 参数都传递给了函数,但从未在日志中看到模板名称(因此允许我在beforeConstruct 方法中运行逻辑。有没有办法完成这个?

【问题讨论】:

  • 是否有两个不同的 apostrophe-custom-pages 子类是您想要避免的事情?
  • 不,只是不确定如何构造多个apostrophe-custom-pages 子类。目前,我只是通过将我自己的实例添加到lib/modules 来扩展apostrophe-custom-pages,这显然适用于我在lib/modules/apostrophe-pages 中的工作,其中存储了我的其他模板。我是否会在lib/modules 中创建一个新模块,并使用module.exports = { extend: 'apostrophe-custom-pages',... 进行初始化?如果是这样,我该如何包含对这个新模板的引用?进行快速测试,并将其添加到 apostrophe-pages/index 会引发未找到错误。

标签: apostrophe-cms


【解决方案1】:

要为不同的页面类型提供不同的架构,您需要为每个模板类型创建一个 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-29
    • 2013-05-26
    • 1970-01-01
    • 2013-12-30
    • 2015-05-08
    • 1970-01-01
    • 2016-05-02
    相关资源
    最近更新 更多