Hi,
in the notes of a product I have different lines, each line begins with a dash (‘-’). In my template for the invoice I would like to generate in the product table different list items from the notes by using the split function in twig as follows:
<td>
<p>{{ item.product_key }}</p>
<ul>
{% set temp_notes = item.notes|split('-') %}
{% for note in temp_notes %}
<li> {{ note }} </li>
{% endfor %}
</ul>
</td>
But for some reason the split function does not work. It seems like
{% set temp_notes = item.notes|split('-') %}
returns nothing. The dash ist not the problem. I also tried different characters to seperat.I also tried
{% set temp_notes = item.notes|split('') %}
which should result in a separation after every character. But again the split returns nothing.
Any ideas or hints?
UPDATE:
I learnt that the split function is not active. I wonder if this is true only for the self-hosted version or for the hosted version too.
I am surprised that the split function is not active. If the twig module is used anyway, why is it disabled.
Another problem
I built a custom design with some statement in the <ninja> environment. In the preview everything works just fine and looks as I wanted it. The preview is based on a real quote. But when I look at the pdf of my quote in “normal operation” it looks like everything inside the ninja section is skipped.
I do not understand that. I would expect that both the preview and the real generation use the same pdf engine.
Since my code works fine in the preview I think I have no syntax errors. This is the coede which is completely skipped:
As mentioned in the other post I think your best option is to send an email asking the split function to be enabled. For security reasons all methods are disabled, we can enable them on a case by case basis.
Note: if you’d prefer you can request this change on GitHub by creating an issue.
I asked about twig-features, too. @david gave me some answers about it. If I remember correctly, it’s a matter of performance and security that some filters/features are not enabled by “default”.
About your twig-coding-problem: Well, your code is about invoices but not for quotes. If you want twig to append to quotes, you must write such code as:
{% if quotes %}
{% set quote = quotes | first %} {# not invoices, because this only applies to invoices #}
[here your further code, but be beware that sometimes $variabes may not work]
{% endif %}
If you want a design and a ninja-code within apply to more than just one entity, do something like this:
<ninja>
{% if quotes %}
{% set entity = quotes | first %}
{% elseif invoices %}
{% set entity = invoices | first %}
{% elseif credits %}
{% set entity = credits | first %}
(…)
{% endif %}
{{ entity.name }} {# to display the entity-name of the document for example #}
</ninja>
Hi David,
thanks a lot for the Info. Also thanks to Schmitti!
One more question for my understanding:
Is there a principle difference between “designs” sand “templates” regarding to what is can I use there? To make my question more precise:
Does ninja work only in templates or can I use it in designs too?
For example I made two very complex designs by using Twig which allows me to show different tables for quotes and invoices and to have free-text-lines or just comment-lines with italic font-format and so on. But it was damn a hard way to have this working as it should be.
I understand it that way that all line_items attributes can be found in invoices. It seems to me that there is no such thing as quote.lime_items? That also complies with the fact that I do get nothing from quotes.line_items in the statement above.
Anyway, the statements with quote(s)instead of invoice(es)do not work.
Despite all the technical issue it is still not comprehensible to me that the code works in the design tool, but not in real life. I guess that I am not the only one who uses quotes the way I try to do.
I think quotes only work within real quotes, not in the editor.
You must be aware of using the right keywords/variables:
<ninja>
{% set invoice = invoices | first %}
{% if invoice %}
...
{% for item in invoice.line_items|filter(item => item.type_id == 1) %}
..
{% endfor %}
{% andif %}
</ninja>
Here’s a mistake in line 3 for example. It must be “if invoices”, not “invoice”.
Ahm, I don’t know, but does “andif” is a correct and implemented twig-code?