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

NodeBB Plugins
  • Plugin & Theme Dev
    #1

    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
    #2

    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
    #3

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

  • Community Rep
    #4

    Glad I could help! 👍


Suggested Topics


  • 0 Votes
    1 Posts
    198 Views

  • 0 Votes
    5 Posts
    1696 Views

  • 0 Votes
    4 Posts
    2102 Views

  • 0 Votes
    3 Posts
    1468 Views

  • 1 Votes
    23 Posts
    11916 Views

| | | |