Insert an if statement in go tmpl range - go-templates

In my project, in the index.tmpl file is the range function defined: {{ range $index, $jb := .Jailbreaks }} which iterates through the Jailbreaks array.
I was wondering if there is a way to check if the $index is on a defined position. So for this I tried {{ if $index == 0 }} but on compiling I get the error
Error rendering index template: template: index.tmpl:63: unexpected "=" in operand
Do I have to define a function in the main.go file to complete this task?
I am working with this project for everyone who is wondering.

You are looking for {{ if eq $index 0 }}. See https://golang.org/pkg/text/template/#hdr-Actions and https://golang.org/pkg/text/template/#hdr-Functions.

Related

Why this odd warning from Vue / Vuetify / Vite?

I am constructing an array of Vuetify 'chips' that can have data dragged from one to the other:
<v-container id="endgrid" style="max-width: 300px; position: relative;">
<v-row v-for="(row,r) in endGrid">
<v-chip size="x-large"
v-for="(chip,c) in row"
:key="chip.name"
draggable="true"
#drop="drop($event)"
#dragover="allowDrop($event)"
#dragstart="drag($event)"
:id=idString(1,r,c)
> {{ chip.name }} </v-chip>
</v-row>
</v-container>
and it works as expected. But during the document creation I am getting this warning (in the debug console) for every one (of 25) chip creations:
[Vue warn]: Invalid prop: type check failed for prop "draggable". Expected Boolean, got String with value "true".
at <VChip size="x-large" key=43 draggable="true" ... >
I'm sure the correct syntax for draggable is with a string, not a Boolean. Although if I remove the quotes, the warnings still appear - but the code still works.
I'm concerned that
this may be hiding something else wrong in my code
even if not, those warnings appearing in a browser's debug console don't look good!
Since it may be relevant, the data used to construct the grid looks like this:
onBeforeMount(() => {
var index = 1;
for (var i = 0; i < 5; i++)
{
endGrid[i] = [];
for (var j = 0; j < 5; j++)
{
endGrid[i][j] = {
"name" : i*10+j,
"id" : index,
"row" : i,
"col" : j,
"list": 'end'
};
++index;
}
}
});
You need to bind draggable first in order to pass boolean:
:draggable="true"

Go template merging dictionary with a possibly empty source dictionary

In a go template I'm merging labels from project level and application level with
{{ range $k, $v := (merge $project.labels $app.labels) }}
# Do something with $k and $v.
{{end}}
Both $project.labels and $app.labels are dictionaries generated from a yaml file.
Now I want to make app.labels as an optional field, this can be done with some extra with statement but I wonder if there is an elegant way to do this.
Currently if $app.label is not defined in the yaml file I'll get:
wrong type for value; expected map[string]interface {}; got interface {}
Figured it out by adding empty dict as a default:
{{ range $k, $v := (merge $project.labels ($app.labels | default dict)) }}

return template interpolation within ternary operator in angular 5

How do achieve following in angular 5 template:
<h6>{{userWalletData.BTCWalletBalance ? {{userWalletData.BTCWalletBalance}} BTC = {{userWalletData.BTCWalletBalanceInFiat}} : 'Fetching balance...'}}</h6>
You can use Angular's *ngIf in the template to achieve it.
<h6 *ngIf="userWalletData.BTCWalletBalance != undefined && userWalletData.BTCWalletBalanceInFiat != undefined; else fetching_balance">{{userWalletData.BTCWalletBalance}} BTC = {{userWalletData.BTCWalletBalanceInFiat}}</h6>
<ng-template #fetching_balance><span>Fetching balance...</span></ng-template>
hope it helps!

How to use continue and break keywords in golang templates?

For example:
{{range .Users}}
{{if .IsAdmin}}
{{/* How to use "break" or "continue"? */}}
{{end}}
{{end}}
The documentation for "break" or "continue" in templates is not available in golang.org
They are not documented because they do not exist.
To make sure - check the tests for the text/template lexer: https://github.com/golang/go/blob/master/src/text/template/parse/lex_test.go
Go101 does mention (May 2021, 4+ years later):
Since Go 1.18, "break" and "continue" might be supported in range loops of Go templates.
Note: Go 1.18 should be released in Q1 2022.
That would solve issue 20531 text/template: add break and continue support.
And is currently implemented in CL 321491: html/template, text/template: implement break and continue for range loops.
This is still a work in progress for now (Q1 2021)
Update Sept. 2021: confirmed, with commit d0dd26a
html/template, text/template: implement break and continue for range loops
break and continue for range loops was accepted as a proposal in June 2017.
It was implemented in CL 66410 (Oct 2017)
but then rolled back in CL 92155 (Feb 2018) because html/template changes had not been implemented.
This CL reimplements break and continue in text/template and then adds support for them in html/template as well.
break and continue statements are part of text/template and html/template in Go 1.10 (in Beta at time of writing). From the release notes:
The new actions {{break}} and {{continue}} break out of the innermost
{{range ...}} loop, like the corresponding Go statements.
Prior releases of Go (Before 1.10) do not support break or continue statements.
Looking at the beta documentation you can see the new itemContinue and itemBreak items in the lexer, the new nodes like ContinueNode in Parser to follow the code.
You can use a variable to add a in processing check and skip any existing checks for the remainder of the loop.
// method 1
{{ $dothing := false }}
{{ range $i, $A := .List }}
{{ if $A.Check }}
{{ $dothing = true }}
{{end}}
{{ end }}
{{ if $dothing }}
// do thing
{{ end }}
// method 2
{{ $already := false }}
{{ range $i, $A := .List }}
{{ if $already }}
{{ else }}
{{ if $A.Check }}
{{ $already = true }}
// do thing
{{ end }}
{{ end }}
{{ end }}

Typo3 GP variables in TSSETUP

I use typo3 7.6.10
I learn how get a url variable and store it in other variable for fluid in TSSETUP:
lib.pippomio = TEXT
lib.pippomio.data = GP:cat
example url: index.php?id=10&cat=pino
I print the variable in my template:
<f:cObject typoscriptObjectPath="lib.pippomio" />
Ok it works.
Now i need to print the variable in input by Search Indexed Engine:
<input class="tx-indexedsearch-searchbox-sword" id="tx-indexedsearch-searchbox-sword" type="text" name="tx_indexedsearch_pi2[search][sword]" value="progetto">
How can i store in lib.pippomio the POST Variable "tx_indexedsearch_pi2[search][sword]" ???
I tried
lib.pippomio.data = GP:tx_indexedsearch_pi2[search][sword]
But it doesn't works.
I want to print the word searched in an other place.
You can use a pipe "|" to get it
lib.pippomio.data = GP:tx_indexedsearch_pi2|search|sword
TYPOSCRIPT Reference => getText