Document template losing variables

I’ve added a template to the Documents section through Config/templates. At the time of creating the template I have this line at the beginning:

{% set internet_service = loader.getServicesByTypeAndStatus(‘internet’, ‘active’) %}

After that, variable replacement works as expected when previewing as PDF. For example

add group=full name={{internet_service[0].login}}

becomes

add group=full name=joebloggs

So I have saved the template then gone into Customers and in the Documents section of a customer I selected Generate, then selected the template from the dropdown and selected Load.

The preview loads the template with populated variables but from a different customer (same customer as populates ‘Use last entity found for preview’ when making the template).

When the document is generated, the PDF is created and saved but all variable substitutions are gone. Everything in curly brackets e.g. “{{internet_service[0].login}}” is gone, including the brackets.

I don’t see how I can use the templates in this state, can anybody help me to get generated templates with variables please? (plain TXT would be very nice as an option over PDF but I can live with PDF if that is the only option).

Hello,

once you have generated a document - the system converts all variables for PDF format and yes, all your variables will gone.

I’m recommending you to use a dump function like {{dump(your_variable)}} to see all required data inside a variable or array.

For working with loops please check the link - for - Documentation - Twig - The flexible, fast, and secure PHP template engine (it’s useful when your customer will have two or more services)

Unfortunately, to check your code - you should use a template or write a code in the Document and then generate a Document. Only then you will see a real result.

Thank you

Hi,

Thanks.

Unfortunately, to check your code - you should use a template or write a code in the Document and then generate a Document. Only then you will see a real result.

I have the correct variable names and the variable substitution shows as working in preview, albeit with the wrong customer details.

When I use a template or write variable codes in the inline editor, the codes are removed completely,not substituted with variables. All that is in the generated document is an empty space where the instance of the variable should be.

I’ve been using the dump() function to properly choose the variable names and I know they are correct because the preview is working as expected.

I also understand loops and I will check Twig documentation as and when required, thanks for the link.

once you have generated a document - the system converts all variables for PDF format and yes, all your variables will gone.

I don’t understand this. How can variables be used at all if they are removed before the creation of the PDF?

Thanks.

Hi,

Just wanted to add this is now working as expected. When the service is not in an ‘Active’ state the variables are empty. When the service is changed to ‘Active’ the correct variable substitution occurs. Thanks again Nik for your assistance and sorry for the misunderstanding.

1 Like