accessing index array from gotmpl - go

I have this list of an array and want to print something if index = 0, but print something else after that.
I've looked at this docs but seems not successful... all of them is printing something else instead.
{{ range $i, $v := .Lists }}
{{ if $i = 0 -}}
do something
{{- else -}}
do something else
{{- end -}}
{{- end }}

try == not =.
maybe like this
{{ if $i == 0 -}}
in documment:
And the following comparison operators are also supported: eq: Equal (==)...
https://docs.gomplate.ca/syntax/#functions

Related

Extract common commands into functions

I've got a file that consists of a number of configmaps.
Something like
{{- define "config1" -}}
kind: ConfigMap
metadata:
name: config-{{.Chart.Nn}}
apiVersion: v1
data:
script.sh: |-
#!/bin/bash
echo "Hello World"
echo "Hello Planet"
{{- end -}}
How do I extract echo "Hello World" and echo "Hello Planet" into a function so I can simply refer to the function within script.sh for the configmaps that need to run these particular commands?
I'm trying to avoid having to write the same code over and over.
Thanks.

Unescaped pug attribute not processed by laravel blade

I'm using Laravel Pug https://github.com/BKWLD/laravel-pug, and the file is named *.pug.blade.php. I'm trying to set an attribute value.
| <?php $x = "any"; ?>
div(class!="{{ $x }}")
| {{ $x }}
the output html is
<div class="{{ $x }}">any</div>
Interestingly, in attribute, the php variable is accessed with no < ? php ? > or {{ }} enclosures, just like javascript variables
| <?php $x = "any"; ?>
div(class=$x)
| {{ $x }}
the HTML output
<div class="any">any</div>
Also, the dollar sign can be omitted, and interpolation can be used
| <?php $x = "any"; ?>
div(class = x + "test")
| {{ $x }}
the HTML output
<div class="anytest">any</div>

Jekyll extracting filename from url with Ruby

I'm trying to extract the file name from a url on Jekyll using this ruby snippet:
{% assign filename = page.url.split('/')[-1] | replace: '.html', '.md' %}
If I just use:
{% assign filename = page.url | replace: '.html', '.md' %}
I get back the url with the replaced file type but my .split('/')[-1] doesn't seem to work.
I tried running the following in standalone ruby to ensure my syntax was right, and it returned bird as expected:
"cat/dog/b­ird".split­('/')[-1]
Why doesn't the same syntax work in my Jekyll instance? Is it that page.url isn't a string, or something else?
The problem is mixing ruby code with Liquid tags.
To extract the filename from a url in Jekyll you can use just pure Liquid template filters, using the equivalents of what you tried:
.split­('/') -> | split: '/'
[-1] -> | last
As an example with a custom URL:
{% assign url_example = "cat/dog/bird.html" %}
{% assign filename = url_example | split: '/' | last | replace: '.html', '.md' %}
{{filename}}
outputs:
bird.md

yaml multi line syntax without newline to space conversion

I have something like this dictionary:
env: qat
target_host: >
{%if env in ['prd'] %}one
{%elif env in ['qat','stg'] %}two
{%else%}three
{%endif%}
when I print it I get:
ok: [localhost] => {
"var": {
"target_host": "two "
} }
So it is converting the \n at the end of the line to a space. Which is exactly what it is supposed to do. However in this case I am just trying to spread out the lines to make the structure of the if/else more readable and I don't want the extra space. It works as expected if I put it all on one line without the > but I would like to be able to make it multiline just so its easier to read.
I found this question
Is there a way to represent a long string that doesnt have any whitespace on multiple lines in a YAML document?
So I could do:
env: qat
target_host: "{%if env in ['prd'] %}one\
{%elif env in ['qat','stg'] %}two\
{%else%}three\
{%endif%}"
And that gives the desired result.
Is there anyway to accomplish this without cluttering it up even more?
In Jinja* you can strip whitespaces/newlines by adding a minus sign to the start/end of a block. This should do the trick:
env: qat
target_host: >
{%if env in ['prd'] -%}one
{%- elif env in ['qat','stg'] -%}two
{%- else -%}three
{%- endif%}
* Jinja 2 is the templating engine used by Ansible.
Maybe what you need is the | literal?
env: qat
target_host: |
{%if env in ['prd'] %}one
{%elif env in ['qat','stg'] %}two
{%else%}three
{%endif%}
This will not 'fold' newlines into spaces, as oposed to >

How do I access variables under liquid filter argument?

module MyFilter
def all_caps(input)
input.upcase
end
end
Liquid::Template.register_filter(MyFilter)
template = Liquid::Template.parse(" {{ 'hi john' | all_caps }} ")
template.render
# => " HI JOHN "
template = Liquid::Template.parse(" {{ 'hi {{name}}' | all_caps }} ")
template.render('name' => 'john')
# => " ' | all_caps }} "
How do I fix this? No mentions in the official documentation.
Note: I have used all_caps example just for the simplicity. I want to achieve something complex.
I did figure out a way to do that. Would appreciate if there is any other solution.
template = Liquid::Template.parse("{% capture some_text %} Hi, {{name}} {% endcapture %} {{ some_text | all_caps }}")
template.render('name' => 'john')
Hope this helps.

Resources