How can I print out a specific array entry in my template?



  • I'm trying to print out a specific custom variable value of mine in the account stats row. Specifically "customFields" is an array that contains a name/value pairs of custom fields.

    I'd like to print out the customField with the name "karma" (if it exists). Is it possible for the templating system's conditionals to handle that? Something like:

    <!-- BEGIN customFields -->
    <!-- IF customFields.name == 'karma' -->
        <div class="stat">
        <div class="human-readable-number" title="{customFields.value}">{customFields.value}</div>
        <span class="stat-label">{customFields.name}</span>
    </div>
    <!-- ENDIF -->
    <!-- END customFields -->

  • Global Moderator

    The way you're using customFields, it's an object, not an array.

    Anyways, the easiest way to fix this is to edit your data to have karma as one of the properties so you can do this instead:

    <!-- IF customFields.karma -->
    

    Otherwise, you can add a helper (keep in mind you need to do this server side and client side):

    Benchpress.registerHelper('equals', function (root, actual, expected) {
      return actual == expected;
    });
    

    and do this in your template:

    <!-- IF function.equals, customFields.name, "karma" -->
    

    The full BenchpressJS docs (that's our templating system) are located here: BenchpressJS docs



  • @pitaj Thanks, your advice coupled with this post helped me get the syntax right:

    https://community.nodebb.org/topic/3554/how-to-create-and-use-a-template-helper/9


  • Admin

    Good work as always @PitaJ 😄


 

| |