How-to access widget data in helper

泄露秘密 提交于 2019-12-01 01:35:54

Passing a piece/widget/page to a helper at the template level is pretty common practice, just like your illustrating.

If your issue is that you always need some special data/decision/format to happen based on a widget, you can override the widget's load method and attach that special something to your widget and then access it in your template.

widget's index.js

module.exports = {
    extend: 'apostrophe-widgets',
    ...
    construct: function(self, options) {
        const superLoad = self.load;
        self.load = function (req, widgets, callback) {
            return superLoad(req, widgets, function (err) {
                if (err) {
                    return callback(err);
                }
                // `widgets` is each widget of this type being loaded on a page
                widgets.forEach(function (widget) {
                    // do something cool, attach it to widget
                    widget.somethingCool = 'hello world';
                });
                return callback(null);
            });
        };
    }
}

You would then be able to access data.widget.somethingCool in your template.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!