Search code examples
javascriptnode.jsapostrophe-cms

Apostrophe-pieces-pages can't register page type


I try to create a new pieces page, kind of a blog style in apostrophe cms. I have trainings, trainings-widgets and trainings-pages folders. I registered them all in app.js; pieces by themselves and widgets work beautifully. The problem is when I try to add page of Trainings type. I get Error: template not found: pages/trainings-page.html in console.

In apostrophe-pages/index.js I have it configured:

module.exports = {
    types: [{
            name: 'home',
            label: 'Home'
        },
        {
            name: 'trainings-page',
            label: 'Trainings'
        }
    ]
}

And in trainings-pages/index.js:

module.exports = {
    name: 'trainings-page',
    label: 'Training Page',
    extend: 'apostrophe-pieces-pages',
    piecesFilters: []
}

I can't figure out what might be wrong here.

Edit: A showcase git: https://github.com/msdsk/apostrophe-error


Solution

  • In addition to the module directory name being incorrect, as noted:

    lib/modules/trainigs-pages should be:

    lib/modules/trainings-pages

    When we fix this we graduate to a new error:

    Error: template not found: trainings-pages:layout.html

    You are extending layout.html. But that file is in a different module, apostrophe-pages, that trainings-pages does not extend, and so it is never found. This is our fault for not recommending a more universal location for layout.html in the tutorial.

    You can solve it two ways:

    OPTION ONE

    1. Move layout.html to lib/modules/apostrophe-templates/views/layout.html.

    2. Always extend it like this:

    {% extends 'apostrophe-templates:layout.html' %}

    This "cross-module" path syntax will find it in the apostrophe-templates module no matter what module you're in.

    OPTION TWO

    Another option is to set the viewsFolderFallback option of apostrophe-templates in app.js, like this:

    'apostrophe-templates': { 
      viewsFolderFallback: __dirname + '/views'
    },
    

    And create a views/ folder at the top level of the project and move layout.html there. Then you don't have to use the special syntax, you can just write layout.html and if the module doesn't have its own, it'll look in the fallback folder. This is what we usually do in-house.

    I'll discuss with the rest of the Apostrophe core team whether the tutorial should recommend option one or option two, and adjust the apostrophe CLI to set up option two automatically if that is preferred.

    (There is a third option: you can put things in lib/modules/apostrophe-module/views and it works as a fallback without any configuration. That's because all modules eventually inherit from apostrophe-module. But our feeling in the past has been that this approach is not very intuitive.)