Search code examples
apostrophe-cmsnunjucks

How to access data saved in apostrophe widget?


I have the following widget:

module.exports = {
    extend: 'apostrophe-widgets',
    name: 'navigation',
    label:'Navigation',
    addFields: [
        {
            name: 'logo',
            label: 'Logo',
            type: 'singleton',
            widgetType: 'apostrophe-images',
            options: {
                limit: 1,
            }
        },
        {
            name: 'menuPoints',
            label: 'Menu Points',
            type: 'area',
            options: {
                widgets: {
                    'menu': {}
                }
            }
        },
    ],
    construct: function (self, options) {
        const superLoad = self.load;
        self.load = (req, widgets, callback) => superLoad(req, widgets, (err) => {
            if (err) {
                return callback(err);
            }

            for (const widget of widgets) {
                widget.url = req.url.substr(3);
            }

            return callback(null);
        });
    }
}

This is a navigation widget that allows users to populate the header with menu points using another widget, the menu-widget.

How do I display the data saved in multiple 'menuPoints' on my widgets template?

I tried to iterate using {% for menuPoint in data.widget.menuPoints %} but failed.


Solution

  • if menuPoints is an area it is going to be a more complex object than just an array of data. I would recommend logging the object out and stepping through it to find what you want.

    in your nunjucks template you can use apos.log to start logging things on the server

    apos.log(data.widget.menuPoints)

    You'll probably have to log out multiple times as you dot through the object.