• Home
  • Categories
  • Recent
  • Popular
  • Top
  • Tags
  • Users
  • Groups
  • Documentation
    • Home
    • Read API
    • Write API
    • Plugin Development
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
v3.5.2 Latest
Buy Hosting

Passing parameters into template helpers?

Scheduled Pinned Locked Moved NodeBB Plugins
6 Posts 3 Posters 2.2k Views
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • jongarrisonJ Offline
    jongarrisonJ Offline
    jongarrison Plugin & Theme Dev
    wrote on last edited by
    #1

    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

    PitaJP 1 Reply Last reply
    0
  • PitaJP Offline
    PitaJP Offline
    PitaJ Global Moderator Plugin & Theme Dev
    replied to jongarrison on last edited by PitaJ
    #2

    @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.

    jongarrisonJ 1 Reply Last reply
    1
  • jongarrisonJ Offline
    jongarrisonJ Offline
    jongarrison Plugin & Theme Dev
    replied to PitaJ on last edited by
    #3

    @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:

    GitHub - benchpressjs/benchpressjs: ultralight javascript templating framework

    ultralight javascript templating framework. Contribute to benchpressjs/benchpressjs development by creating an account on GitHub.

    favicon

    GitHub (github.com)

    1 Reply Last reply
    0
  • jongarrisonJ Offline
    jongarrisonJ Offline
    jongarrison Plugin & Theme Dev
    wrote on last edited by jongarrison
    #4

    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.

    Trying to make two things possible: 1. Allow template helpers to · jongarrison/templates.js@a6cda7f

    ultralight javascript templating framework. Contribute to jongarrison/templates.js development by creating an account on GitHub.

    favicon

    GitHub (github.com)

    Thanks for any feedback!

    1 Reply Last reply
    0
  • julianJ Offline
    julianJ Offline
    julian GNU/Linux
    wrote on last edited by
    #5

    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 😄

    jongarrisonJ 1 Reply Last reply
    1
  • jongarrisonJ Offline
    jongarrisonJ Offline
    jongarrison Plugin & Theme Dev
    replied to julian on last edited by
    #6

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

    1 Reply Last reply
    0

Copyright © 2023 NodeBB | Contributors
  • Login

  • Don't have an account? Register

  • Login or register to search.
Powered by NodeBB Contributors
  • First post
    Last post
0
  • Home
  • Categories
  • Recent
  • Popular
  • Top
  • Tags
  • Users
  • Groups
  • Documentation
    • Home
    • Read API
    • Write API
    • Plugin Development