Twig variables are empty or do not exist

Version: v5.10.62

Environment: Docker

Checklist

  • Can you replicate the issue on our v5 demo site? Yes.
  • Have you searched existing issues? Yes.

Describe the bug

All variables are empty or do not exist when used with twig.

Steps To Reproduce

  1. Create a new invoice template.
  2. Select an invoice.
  3. Paste this twig code into the header:
<ninja>
{% if client.name != "" %}
 exists
{% else %}
 does not exist
 Test: $client.name
{% endif %}
</ninja>

Expected Behavior

“exists” should come out on the invoice preview.
But instead, “does not exist” and the client name comes out.

Thank you for your help.

Edit:

My fault. You need to get the first invoice object first like here:

<ninja>
{% set var = invoices | first %}
{% if var.client.name != "" %}
 exists
{% else %}
 does not exist
 Test: $client.name
{% endif %}
</ninja>

Is it possible to access the variable client.name without setting the var variable first?
The documentation confused me, because it says that you can access invoiceninja variables, too:

Hi,

I suggest asking in a discussion in GitHub.

Hi!

No, it is not possible, I guess.

If I understand the “architecture” of this variable-system correctly it’s part of a big mapping. This is something like arrays of variables but the single array-values have a further value or even array or mapping. You can compare this with a file-system-tree:

Invoices {
  -> Invoice #1 {
    -> (invoice.)amount = X €
    -> (invoice.)client = "Client Name"
    -> ...
    }
  -> Invoice #2 {
    ...
  }
}

So it’s mandatory to tell twig which entity it has to use. Otherwise it’s not a unique input which cannot give an output.

Thank you!
I have found out that you can call the first invoice with invoices[0]. So you can call invoices[0].call.name to make it a bit shorter.

How would one access the first name of total_tax_map?
invoices[0].total_tax_map[0].name did not work for me.

Hm, good question.

I use a for-loop to get access to the mappings.

                              {% for tax in entity.total_tax_map %}
                                   <tr>
                                             <td>
                                             {% if tax.name == 'MwSt. 19%' %}
                                                  19 % USt.
                                             {% elseif tax.name == 'MwSt. 7%' %}
                                                  7 % USt.
                                             {% else %}
                                                  {{ tax.name }}
                                             {% endif %}
                                             </td>
                                             <td>{{
                                                  tax.total | format_currency(
                                                  entity.client.currency, locale=entity.client.locale) }}
                                             </td>
                                   </tr>
                              {% endfor %}

Works fine.

Interesting, this works for me well:

{{ invoices[0].total_tax_map[0].name }}

EDIT:
Nope, must be something else – even if my taxes are line-based, so this should be the correct mapping:
{{ invoices[0].line_tax_map[0].name }}

Mhhh, I’m no pro in programming. So I can only guess, that it must be an issue about the syntax for using mappings but not arrays.