Question about injecting custom data into all NodeBB pages/templates (including header.tpl)


  • Plugin & Theme Dev

    Howdy,
    I'm trying to inject some custom data into all nodebb pages to make the data available for template rendering and javascript access.

    My current approach is not working, I'm listening to the hook: filter:middleware.renderHeader. My plugin function for the hook is getting called with data that includes data.templateValues and then calls a callback with the data that was passed in:

        hookRenderHeader: function(data, callback) {
          data.templateValues.someTestValue = "hello";
          return callback(null, data);
        }
    
    1. Should I be able to directly use {someTestValue} in header.tpl and other tpl files?
    2. In browser js, should I be able to directly access: ajaxify.data.someTestValue?
    3. Is there a recommended approach for injecting data into all nodebb templates and ajaxify.data?

    Thanks for any help or suggestions!


  • Community Rep

    If you want data available on all template renders, you should use the hook filter:config.get, example:

    Plugin.configGet = function (config, next) {
      config.someValue = "Some Data"
      next(null, config)
    }
    

    Now in any template, including header.tpl, using {config.someValue} will print Some Data

    In the browser, the config values will not be available in ajaxify.data.someValue but will be in config.someValue

    Note that with this method, the values never change, unless the user reloads. If you need dynamic data on every template, you will need a different method, although I can't think of any use case for this.


  • Plugin & Theme Dev

    Thank you @yariplus!!! That's exactly what I was hunting for.


  • Community Rep

    Glad I could help! 👍


Log in to reply
 


Looks like your connection to NodeBB was lost, please wait while we try to reconnect.