I have a requirement to return items from a list (using a 'for loop' in jinja) that presents the 1st item first as a single quoted value, then the same item as a non-quoted value before moving to the 2nd item in the list to repeat the same process.
For example, if I had the following list
items = [v1,v2,v3]
I would like my for loop to output the following (in this exact order)
desired_output = 'v1',v1,'v2',v2,'v3',v3
Here is my workings so far... I can only produce one output of the 'item' so I'm looking for ideas to produce both
items = [v1,v2,v3]
{%- for i in items %}
'{{i}}'
{%- if not loop.last %} , {% endif -%}
{%- endfor %}
CodePudding user response:
It's easy to confuse strings and string literals, especially when using jinja to template SQL, since you need to consider the parsing context in both languages.
A string is a sequence of characters in memory -- in other words, a string is data.
A string literal is a sequence of characters in code -- a string literal is not data until a parser reads it into memory as a string.
If you want a string with the value v1
in memory, you would usually write that as a string literal "v1"
or 'v1'
in your code, depending on the language. Jinja is basically Python, so either single or double quotes can define string literals. In most SQL dialects, only single quotes can define string literals.
If you want a string with the value 'v1'
in memory, you could write that as in your code as the Python/jinja string literal "'v1'"
. In this case, the outer double quotes denote the string literal, and the inner quotes become part of the string, in memory.
When you use jinja to template SQL code that contains SQL string literals, you need the values of the strings in Jinja to equal a string literal in SQL. These are both equivalent, and will template to the SQL string literal 'v1'
:
-- include the single quotes in the template
{% set s = "v1" %}
'{{ s }}'
-- include the single quotes in the data
{% set t = "'v1'" %}
{{ t }}
So with that in mind:
items = ["v1", "v2", "v3"]
{%- for i in items %}
'{{ i }}', {{ i }}
{%- if not loop.last %}, {% endif -%}
{%- endfor %}
Will template to:
'v1', v1, 'v2', v2, 'v3', v3