Passing parameters into template helpers?


  • Plugin & Theme Dev

    Hi,
    I've created a client and a server template helper that has been correctly registered for both server and client side use. I'm able to see logging messages showing the the methods are being called both on client and server template renders. However, I don't seem to be able to pass any parameters into the functions.

    Am I using the right syntax to embed my template helper?

    In the template, I've tried a few ways to call the template helper without luck:

           <img src="{function.someHelperFunction, someAvailableObject.subObject.value}">
            
           <img src="{function.someHelperFunction, someAvailableObject}">
    

    Server Side template helper:

      templates.registerHelper('someHelperFunction', function(value) {
        winston.verbose("value: ", value);
        return "http://someurl.com/?zipper=" + value;
      });
    

    Client Side template helper:

    templates.registerHelper('someHelperFunction', function(value) {
        console.log("value: ", value);
        return "http://someurl.com/?zipper=" + value;
    });
    

    In both cases "value" is undefined. Any ideas? What am I doing wrong?

    FYI:
    I'm running NodeBB 1.1.2, and I've used both of these threads as examples on creating template helpers:
    https://community.nodebb.org/topic/3554/how-to-create-and-use-a-template-helper
    https://community.nodebb.org/topic/6020/theme-conditional-target-xth-post/3


  • Global Moderator

    @jongarrison Any errors? Are you sure you're passing in a defined value?

    Edit:
    Actually, it looks like you can only pass in arguments that are keys of the current scope. So with this data:

    {
      derp: [
        {
          "name": "tom",
          "amazing": "nope"
        },
        {
          "name": "bob",
          "amazing": "maybe"
        },
        {
          "name": "carl",
          "amazing": "yes"
        }
      ]
    }
    

    And this helper:

    templates.registerHelper('capitalize', function (value) {
      return value[0].toUpperCase() + value.slice(1);
    });
    

    You could do something like this:

    <ul>
      <!-- BEGIN derp -->
        <li>{function.capitalize, name} - {amazing}</li>
      <!-- END derp -->
    </ul>
    

    I think.


  • Plugin & Theme Dev

    @PitaJ Thank you! That was a very helpful tip.

    That's a curious limitation of the template helpers. It makes sense that we would want to be able to run a template function on a singular value, right?

    For other people that are running into questions about templates.js, here is the link to the project. I was looking around inside NodeBB for a while not realizing it was an external dependency:

    https://github.com/psychobunny/templates.js


  • Plugin & Theme Dev

    I'm wrapping my head around the templates.js project and I've made some small changes which I think meets my two goals:

    1. Allow template helpers to receive parameter values outside of a loop.
    2. Allow template helpers to receive string literals as parameters.

    Could somebody (@psychobunny @PitaJ) else review my changes? It seems like changes to templates.js can have some far ranging unintended consequences.

    https://github.com/jongarrison/templates.js/commit/a6cda7f003d58e5de6f270aba42b00c8b924fe80

    Thanks for any feedback!


  • Admin

    The main concern is whether it will break existing implementations and invocations of templates.js... depending on how we use helpers right now, this might be an issue, or it might not 😄


  • Plugin & Theme Dev

    @julian Agreed. But the goal is good and worthwhile, right? 😄


Log in to reply
 


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