twig to tpl conversion of condition operator - opencart2.3

{% set i = 0 %}
{% set i = i + 1 %}
{% if i % 2 == 0 %}
i am trying to change this set and i operator to change this code .
this is the twig file code . how can we write this code in tpl file .I am facing problem to solve condition operators like this specially f i % in above three codes . mean how can i convert this line in tpl exactly. i am converting opencart 3.0.2.0 theme into opencart 2.3.0.2 . i tired my best to understand how to change this code in tpl but could't manage to solve it . thanks

You can write above code in .tpl like this
<?php
$i = 0;
$i++;
if($i%2==0){}
?>

Related

helm escape ` in template yaml file

Does anyone know how to espace ` in helm template?
I try to add \ in front doesn't seem to work
original:
{{- if not .Values.istio_cni.chained }}
k8s.v1.cni.cncf.io/networks: '{{ appendMultusNetwork (index .ObjectMeta.Annotations `k8s.v1.cni.cncf.io/networks`) `istio-cni` }}',
{{- end }}
Tried:
{{- if not .Values.istio_cni.chained }}
k8s.v1.cni.cncf.io/networks: '{{`{{ appendMultusNetwork (index .ObjectMeta.Annotations \`k8s.v1.cni.cncf.io/networks\`) `istio-cni` }}`}}',
{{- end }}
Error:
Error: parse error at (test/templates/istio-sidecar-injector-istio-system-ConfigMap.yaml:29): unexpected "k8s" in operand
In the Go text/template language backticks delimit "raw string constants"; in standard Go, there is no escaping at all inside raw string literals and they cannot contain backticks.
In your example, it looks like you're trying to emit a Go template block into the output. You can make this work if you use double quotes around the variable names inside the template, and then backticks around the whole thing:
# double quotes around string literal
# v v
k8s.v1.cni.cncf.io/networks: '{{`{{ appendMultusNetwork ... "istio-cni" }}`}}'
# ^ ^
# backticks around whole expression

How to use printf in this Helm template function?

I created this Helm template function in my templates/_helpers.yaml file. It simply gets the gets the value of an array element (the index .Values... part), based on the passed-in environment. It works fine.
{{/*
Function to get min CPU units
*/}}
{{- define "microserviceChart.minCpuUnits" -}}
{{ index .Values.valuesPerEnvironment.cpuUnits ((pluck .Values.environment .Values.environments | first | default .Values.environments.sandbox) | int) | quote }}
{{- end }}
For example, in my values.yaml file
environments:
sandbox: 0
staging: 1
production: 2
valuesPerEnvironment:
cpuUnits: [512, 512, 1024]
so my template function returns "512", "512", "1024" based on my passed-in environment. However, can I use printf to it adds m to these values? In other words, I want it to return "1024m" for production. I tried the following but I get a syntax error
{{/*
Function to get min CPU units
*/}}
{{- define "microserviceChart.minCpuUnits" -}}
{{- printf "%dm" index .Values.valuesPerEnvironment.cpuUnits ((pluck .Values.environment .Values.environments | first | default .Values.environments.sandbox) | int) | quote }}
{{- end }}
Instead of trying to get the arguments in the right order for printf, you could include the m in the template body. You'll also need to make the "..." explicit, instead of using the quote function, to get the m inside the quotes.
Expanded out (note that {{- and -}} will delete whitespace before and after, including newlines):
{{- define "microserviceChart.minCpuUnits" -}}
"
{{- index .Values.valuesPerEnvironment.cpuUnits ((pluck .Values.environment .Values.environments | first | default .Values.environments.sandbox) | int) -}}
m"
{{- end }}
The heck with it. I just made my values.yaml like this, and I get the same result.
environments:
sandbox: 0
staging: 1
production: 2
valuesPerEnvironment:
cpuUnits: [512m, 512m, 1024m]
It'd still be cool to know if I can do this in the function, so if someone answers the actual question, I'll accept that as the answer.

Caculating the global index for two ranges in Kubernetes Sprig/helm templates?

In a Helm Chart I have to following values
dataCenters:
- name: a
replicas: 3
- name: b
replicas: 2
When generating the template I would like my output to be like the following
server.1 = a-1
server.2 = a-2
server.3 = a-3
server.4 = b-1
server.5 = b-2
I tried this code
{{- $index := 0 -}}
{{ range $dc := .Values.cluster.dataCenters -}}
{{ range $seq := (int $dc.replicas | until) -}}
{{- $index := (add $index 1) -}}
server.{{ $index }}={{ $dc.name }}-{{ $seq }}
{{ end -}}
{{ end -}}
however in helm templates I don't thing you can reassign the value of the index as my 4th line is attempting and because of that I get out
server.1 = a-1
...
server.1 = b-2
How does one calculates the global index 0 to 4 (1 to 5 in my situation) using the Sprig/Helm templating language?
I have a way to do it that involves some trickery, heavily inspired by functional programming experience.
A Go/Helm template takes a single parameter, but the sprig library gives you the ability to create lists, and the text/template index function lets you pick things out of a list. That lets you write a "function" template that takes multiple parameters, packed into a list.
Say we want to write out a single line of this output. We need to keep track of which server number we're at (globally), which replica number we're at (within the current data center), the current data center record, and the records we haven't emitted yet. If we're past the end of the current list, then print the records for the rest of the data centers; otherwise print a single line for the current replica and repeat for the next server/replica index.
{{ define "emit-dc" -}}
{{ $server := index . 0 -}}
{{ $n := index . 1 -}}
{{ $dc := index . 2 -}}
{{ $dcs := index . 3 -}}
{{ if gt $n (int64 $dc.replicas) -}}
{{ template "emit-dcs" (list $server $dcs) -}}
{{ else -}}
server.{{ $server }}: {{ $dc.name }}-{{ $n }}
{{ template "emit-dc" (list (add1 $server) (add1 $n) $dc $dcs) -}}
{{ end -}}
{{ end -}}
At the top level, we know the index of the next server number, plus the list of data centers. If that list is empty, we're done. Otherwise we can start emitting rows from the first data center in the list.
{{ define "emit-dcs" -}}
{{ $server := index . 0 -}}
{{ $dcs := index . 1 -}}
{{ if ne 0 (len $dcs) -}}
{{ template "emit-dc" (list $server 1 (first $dcs) (rest $dcs)) -}}
{{ end -}}
{{ end -}}
Then in your actual resource definition (say, your ConfigMap definition) you can invoke this template with the first server number:
{{ template "emit-dcs" (list 1 .Values.dataCenters) -}}
Copy this all into a dummy Helm chart and you can verify the output:
% helm template .
---
# Source: x/templates/test.yaml
server.1: a-1
server.2: a-2
server.3: a-3
server.4: b-1
server.5: b-2
I suspect this trick won't work well if the number of servers goes much above the hundreds (the Go templating engine almost certainly isn't tail recursive), and this is somewhat trying to impose standard programming language methods on a templating language that isn't quite designed for it. But...it works.

For Loop not working in Jinja/Flask

In jinja template my code is something like this, I m trying to get values from my MongoDB database
{% for a in output %}
{{ a.product_name }}
{% else %}
<p> No product found </p>
{% endfor %}
Some HTML CODE
{% for b in output %}
{{ b.product_name }}
{% endfor %}
The problem is first loop is working fine, but second loop not at all working. But when I write the second loop before first loop, then second loop work but then not first loop ( it going inside else and printing "No Product Found").
I am not able to understand this problem.
You want to iterate over the mongodb cursor twice. So after first iteration, you need to call the rewind method on the output (cursor) somewhere between the two loops.
output.rewind()
I am not sure if you would be able to do this in the Jinja template itself.
So the better option would be to convert the pymongo cursor object into a list itself, so you can iterate multiple times.
output_as_list = list(output)
Now you should be able to use output_as_list in your code the way you expected.
Looks like the output is an iterator. Try to convert it to a list (or dict) inside a view function.
You can reproduce such behaviour by the next code:
output = (x for x in range(3))
# output = list(output) # if uncomment this line, the problem will be fixed
for x in output: # this loop will print values
print(x)
for x in output: # this loop won't
print(x)
UPD: Since the output is a mongodb cursor, you can rewind it by calling output.rewind() directly in the template.
{% for a in output %}
{{ a.product_name }}
{% else %}
<p> No product found </p>
{% endfor %}
Some HTML CODE
{% set _stub = output.rewind() %} {# use a stub to suppress undesired output #}
{% for b in output %}
{{ b.product_name }}
{% endfor %}

Convert string to integer in Shopify Liquid?

I just read this related answer:
How can I convert a number to a string? - Shopify Design — Ecommerce University
To convert a string to a number just add 0 to the variable:
{% assign variablename = variablename | plus:0 %}
Not super elegant but it works!
Inelegant or not, the answer given there isn't working for me. What's the right way to do this?
Are the Liquid docs really missing such basic answers or am I just not finding the right place to look?
Using assign with a math filter is correct. See this thread on GitHub, and this blog post.
Variables created through {% capture %} are strings. When using assign, either of these options should give you a number:
{% assign var1 = var1 | plus: 0 %}
{% assign var2 = var2 | times: 1 %}
If this doesn't work for you, can you post the relevant code?
In Shopify Liquid programming language, you can convert a string to an integer using the to_i filter.
For example, if you have a variable string_number that contains a string representation of a number, you can convert it to an integer using the following code:
{{ string_number | to_i }}
It will convert the string to integer and you can use it in mathematical calculations, comparisons and other operations that work with integers.
For example, you could use the to_i filter to add two numbers together:
{% assign number_1 = "5" %}
{% assign number_2 = "2" %}
{% assign result = number_1 | to_i + number_2 | to_i %}
{{ result }}
This will output: 7
Please note that this filter will return 0 if the string is not a valid number.
Source