Append templateData to sidebar

Plugin Development
  • Swedes

    So I've been working with NodeBB for a while now and am familiar with several concepts, but one thing I'm not sure about.

    If I want to add data to the "global scope" before render. How would I do that?

    I know how to get data to the page templates, but that doesn't carry over into the global templates, like the sidebar menu.

    How can I, for instance, fetch my categories and place them in the sidebar?

  • NodeBB

    Hi @magnusvhendin,

    The hook you are looking for is 'filter:middleware.render' this is triggered on every page render so if you add additional data in it it will be available in widgets as well.

    Here is a sample:

    myPlugin.filterMiddlewareRender = function (hookData, callback) {
        hookData.templateData.someArray = [{name: 'foo'}, {name: 'baz'}];
        callback(null, hookData);

    Now in a html widget you can access someArray like so

    <!-- BEGIN someArray -->
    <!-- END -->

    Also keep in mind there is a categories widget to place on the sidebar already, you might be able to modify that as well.

  • Swedes

    Thank you @baris for your reply. I know I can append to the templates data. My issue is that I can't access that data in the mobile menu (on the left).

    I'm making a custom menu with a hierarchy of categories and also grouped in open and private. So I create that hierarchy in backend (with the hook you suggested) and it works great in every template, but it doesn't seem to be accessible when I do for instance:

    <!-- BEGIN privateCategories -->
      <!-- IMPORT partials/menuItem.tpl -->
    <!-- END privatecategories -->

    The only thing accessible in the config object, which as I understand is added in the frontend load. Again, it works in the "main" templates, not in the menu (which replaces partials/slideout-menu.tpl).

    Using Persona by the way.

  • NodeBB

    Ahh my suggestion only works for the content area, if you want to add additional data to the header you can use filter:middleware.renderHeader

  • Swedes

    @baris Just what I needed. Thanks a lot!!

Suggested Topics

| | | |