-2

How can I render the values of two variables {{item_link}} and {{item_value}} inside the same div class?

I am using the #if helper to conditionally render the block. If I render them separately I am able to evaluate them, but if I try doing something like {{#if item_link && item_value}}, so I can then use them inside the div, it does not work. These are Handlebars layout templates. Is there a way to do it? Thanks!

pilchard
  • 12,414
  • 5
  • 11
  • 23
pebox11
  • 3,377
  • 5
  • 32
  • 57
  • I don't get it why you need that `#if` – Konrad May 03 '23 at 14:33
  • Does this answer your question? [Logical operator in a handlebars.js {{#if}} conditional](https://stackoverflow.com/questions/8853396/logical-operator-in-a-handlebars-js-if-conditional) – pilchard May 03 '23 at 14:34
  • also [Handlebars: multiple conditions IF statement?](https://stackoverflow.com/questions/41251635/handlebars-multiple-conditions-if-statement) – pilchard May 03 '23 at 14:38
  • I think nesting the `#if`s would be the way to go. – 76484 May 03 '23 at 15:40
  • Unfortunatelly not, at least in my case. Sorry ... – pebox11 May 03 '23 at 16:00
  • Yes, and applied it. But things you know do not work all the time, even if another accepted SO answer says so. I followed exactly the logic nesting the {{#if ..}} but for a reason it was not working on my case. Now trying to see how they can render by removing the conditional. But the links you suggested doesn't work. Sorry – pebox11 May 03 '23 at 16:15
  • 1
    Then you'll need to post more context, because with the given information simple nesting of `#if`s works fine – pilchard May 03 '23 at 16:20
  • Now, thats a good suggestion, whereas in the previous comment I sensed a little aggressivenes. Let me debug further and see why this is happening so I can add more feedback in the original post. Thanks! – pebox11 May 03 '23 at 16:26
  • 1
    Here is an example with nested `#if`s: https://jsfiddle.net/76484/kf60d8pz/ – 76484 May 03 '23 at 19:03
  • Thanks @76484 for your kindness to provide an example. Still does not work in my case. I solved it in another way. I will post the answer. Thanks – pebox11 May 03 '23 at 19:23

2 Answers2

0

Here is a solution I found. I moved the AND logic inside my js file and created a single object object = {item_link_name: item_link_value, item_value_name: item_value_value}. This way I can effectively html render with {{object}} or with {{#if object}} ... {{/if}} (if I need also to have some additional logic in my html, modulo the one included in my js) and refer to the individual values with {{item_link_value}} and {{item_value_value}}.

pebox11
  • 3,377
  • 5
  • 32
  • 57
-2

You can render the values inside the same div by concatenating them using the {{concat}} helper:

<div class="my-class">
  {{concat item_link item_value}}
</div>
Klone
  • 290
  • 1
  • 2
  • Hi @Klone, thank you for your suggestion but that doesn't seem to work in my case. – pebox11 May 03 '23 at 15:13
  • 1
    Hi, can you clarify what "doesn't seem to work" means ? How is it not working exactly ? – Klone May 03 '23 at 15:15
  • Nothing is rendered. – pebox11 May 03 '23 at 15:17
  • 2
    concat is not a built in helper (and doesn't help with the question which is about logical operators in an if block) – pilchard May 03 '23 at 15:26
  • The question is "How can I render the values of two variables {{item_link}} and {{item_value}} inside the same div class? " – Klone May 03 '23 at 15:29
  • 1
    Did you read the question @Klone ? or just the title? And any way you can just `
    {{item_link}} {{item_value}}
    `, but the OP wants to conditionally render the block based on the truthiness of the two variables (amply covered by the two flagged duplicates).
    – pilchard May 03 '23 at 15:29