• Swedes

    What's the difference between templateValues (tV) and templateData (tD)?

    I know we get tD from a template hook and tV from a header hook, but we also get data from the header hook, which contains the dame data as in tD?

    Hook data object keys in the two cases:

    • [ 'req', 'res', 'templateValues', 'data' ] from header hook.
    • [ 'req', 'res', 'templateData' ] from template hook.

    So this might not be a technical question at all. I'm just wondering what the naming is meant to be. Maybe someone can shed some light on this, perhaps simple question?

    For instance, if data in header hook is the same as tD in template hook, why is it not just called templateData instead, to avoid confusion?

    I guess a header hook is only fired on initial render and not when the user navigates around, as is the case with a template hook.

    Also, when for filter:middleware.renderFooter is fired, only tV is present and not data?

  • NodeBB Admin

    For the header data variable is the data used to render the content of the page and it changes based on which page you are loading templateValues is passed to the the header template to render the header.

    Header and footer hooks are only fired once on initial page load.

    I think the name was just arbitrary when these hooks were added long time ago. And looks like the filter:middleware.renderFooter hook uses the entire page data to render the footer. So it is different than filter:middleware.renderHeader. I would prefer if they both had the same signature.

  • Swedes

    Yeah, I guess my line of thinking is; why not just have templateValues and templateData on initial render and then templateDataon page render? It just feels a bit confusing with these different signatures and names.

    So, what is data used for when header is rendered? Is it even used? And if not, why not just use templateData on all of them with things related to their function?

    templateData on header hook is to render header.
    templateData on page hook is to render page.
    templateData on footer hook is to render footer.

    I'm guessing there are cases where it's useful for data to be passed along the ride, but they're already treated as different parts, so maybe it's just easier to separate them entirely.

  • NodeBB Admin

    Right now the header hook is passed two variables templateValues and data. templateValues is used to render header.tpl and data is the data used to render that page. If you want to use any variables from data in header.tpl you need to assign them to templateValues in your plugin.

    Seems like footer hook is using the entire data object from the page so it can use anything from the page.

    Changing the variable name is fine but it would be a breaking change that would break plugins using templateValues if we remove it.

  • Swedes

    Yep, I see the issue. I'm not looking to push any changes. It was more out of curiosity on why there are several different names within essentially the same function.

Suggested Topics

| | | |