I have a subchart in the chart dependencies defined as:
dependencies:
- condition: myTestSubchart.enabled
name: my-test-subchart
repository: oci://harbor.test.com/helm
version: 0.1.0
alias: myTestSubchart
The alias is defined as we cannot reference on the names with dashes in the yaml.
However, using alias, we do not get the correct name of the subchart.
For example {{ template "my-test-subchart.name" .Subcharts.myTestSubchart }} will produce myTestSubchart instead of my-test-subchart, I understand this is because of the alias of the dependent subchart.
The template is default one:
{{/*
Expand the name of the chart.
*/}}
{{- define "my-test-subchart.name" -}}
{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }}
{{- end }}
But I need to have an alias, because of the yaml convention.
One of the solution I do not like very much is to override the name back in the values.yaml of the umbrella chart like this:
myTestSubchart:
nameOverride: my-test-subchart
This will produce expected my-test-subchart from {{ template "my-test-subchart.name" .Subcharts.myTestSubchart }}.
However, it is hardcoded in the values of the umbrella chart.
Is there any solution for that without overriding values?
I was looking for the same. The following works for me:
The alias is defined as we cannot reference on the names with dashes in the yaml.
One can use the sprig template functions for this, which are part of helm templates.
Remove the alias. Instead of:
{{ template "my-test-subchart.name" .Subcharts.myTestSubchart }}
use:
{{ include "my-test-subchart.name" (get .Subcharts "my-test-subchart") }}
This should give you the name from the chart definition.
Related
I have this code I use inside a partial in Hugo to pass context to it.
{{- $ctx := . -}}
{{- $curPage := .page -}}
{{- $otherVar := .otherVar -}}
{{- with $curPage -}}
{{ $section := .CurrentSection }}
{{ if .IsHome }}
<span class="post-section">{{ $section.Title }}</span>
{{ else }}
{{ $section.Title }}
{{ end }}
{{- end -}}
I then add {{- $curPage := . -}} at the top of the template where I want the partial to appear, then call the partial as {{ partial "partial-name.html" (dict "page" $curPage "otherVar" .) }}. However, the content returns nil on the homepage while it works everywhere else sitewide. Could anyone look at my code and tell me where I went wrong?
Sorry - didn't see your with statement.
So {{- $curPage := .page -}}
is a typo.
.Page
Tested on local - most present version of hugo.
Also note - I don't think homepage has a section so your span will output very little as most of the currentsection or section related will return nothing.
Since you call the partial like this:
{{ partial "partial-name.html" (dict "page" $curPage "otherVar" .) }}
^^^^^^^^^^^^
Notice
The dot (.) is contained in .otherVar. To find out if you are on the home page, use something simple like this at the top of partial-name.html:
{{ if .otherVar.IsHome }}
<pre>Debugging: YES .IsHome</pre>
{{ else }}
<pre>Debugging: NOT .IsHome</pre>
{{ end }}
After you test with the above GO HTML fragment, you can update your original code above.
TIP: In the Hugo world, it is common to use "context", "ctx", "page", or "Page" rather than "otherVar" as the name of the dictionary key that contains the dot. For a discussion about this, see Naming convention for page context (within a dictionary) when calling partial: seeking opinions in the Hugo discussion group.
ANOTHER TIP: There are some Hugo weirdnesses related to case sensitivity so I would not use "otherVar" anyway. Instead use "othervar", "context", or any name that is all lower case with no whitespace. I do this because I have spent a lot of time messing around with case sensitivity Hugo issues.
I am writing a layout template for a Hugo generated post/page. The .yaml header is
image:
- post/mytitle/image.jpg
The template incorporates the variable in Hugo as
{{ .Params.Image }}
When served, the variable is returned as
[post/mytitle/image.jpg]
My html then becomes
<img src="[post/mytitle/image.jpg]"/>
which is 404 in the browser. I've tried a number of Hugo functions to no avail like {{ trim .Param.Image "[]" }} and {{ subset .Params.Image 1 -1 }} and {{ print .Params.Image }}. Each time Hugo returned the error: "error calling substr: unable to cast []string{"post/mytitle/image.jpg"} of type []string to string".
How do I get the variable to return the string without the brackets, or alternatively, how do I omit the brackets from the string?
In Go template, you access an item in a slice with index:
{{ index .Params.Image 0 }}
The question is why the value is a sequence in the first place. You could simply do
image:
post/mytitle/image.jpg
Then you could keep the original syntax since it is now a simple value, not a sequence.
If you want to possibly include multiple images, you'd do
{{ range .Params.Image }}<img src="{{.}}">{{ end }}
Then you can have
image:
- post/mytitle/image.jpg
- post/mytitle/anotherimage.jpg
I'm developing a big hugo template. I try to simplfy the problem, so I have two datafile:
PROMO_00_1.yaml
PROMO_00_2.yaml
that are phisically stored in this directory:
themes/data/hp/
So, in the site config the user will decide which of this data file will be used simply indicate it in a param (HpElement).
In the template I call the partial in this way:
{{ partial "multiplepages/homepage/promos/00_promo_singleslide_text_video" (dict "context" . "data" $.Site.Params.HpElement) }}
In a partial I write:
{{ $data_partial := (printf "$.Site.Data.homepage.%s" .data)}}
{{ $data_partial}}
and the Hugo output is on the website:
$.Site.Data.homepage.PROMO_00_1
What I need is to access the single variable inside the .yaml file but the user MUST can decide which YAML file have to use. How can I achieve that?
Thanks
I just finished up a similar use case where I needed to select a YAML based on a date stamp. The index function is key to this operation.
https://gohugo.io/functions/index-function/
Here is a simplified version of my situation:
{{ $date := now.Format "s20060102"}}
{{ $data := (index .Site.Data.schedule $date) }}
{{ with $data }}
<h1>.longdate</h1>
{{ range .times }}
<h2>{{ .name }} - {{ .location }}
{{ end}
{{ end}
The example in the Hugo documentation uses an example where the data file is selected based on an attribute in the front matter.
I would like to use the 2 values of a group variable in two different places. My current group vars looks like this.
[test1:vars]
fooname=foo1
barname=bar1
I am using jinja template in the playbook as {{ fooname }} and {{ barname }} based on my requirements in the playbook in multiple places. Now instead of two different variables, i would like to use it as one variable as names and i would like to use the values of it in different places.
Expected group variables :
[test1:vars]
names=foo1,bar1
Is there a way that now i can call {{ names }} variable with some function or condition like {{ names is search(foo) }} or {{ names is search(bar) }} inside the playbook like we use in condition so that i can avoid declaring 2 variables instead of one. I will use these variables in different places in my playbook.
I tried using the above one which prints "True" instead i need the value of my variable which has only foo or bar when i search accordingly.
Note : I have close to 400 groups which same pattern with makes the extra variable makes my inventory lengthy. So, i would like to minimize it.
Ansible 2 supports Arrays in the inventory File.
[test1:vars]
names=["foo","bar"]
But there are some limitations.
[example:vars]
# working
var1=["foo","bar"]
var2=[1,2]
var3=[True, False]
# not working
var4=[yes, no] # Boolean need to be True and False
var5=[foo, bar] # Interpreted as one string
var6='["foo","bar"]' # Interpreted as one string as well
Usage example:
- debug:
- msg: "Item: {{ example[0] }}"
- debug:
- msg: "Item: {{ example | first }}"
I need to write a template where I first define some variables, and then use them in what will be generated from the template:
{{ if $value.Env.CADDY_URL }}
{{ $url := $value.Env.CADDY_URL }}
{{ else }}
{{ $url := printf "http://%s.example.info" $value.Name }}
{{ end }}
{{/* more template */}}
{{/* and here I would like to use $url defined above */}}
{{ $url }}
I get the error
undefined variable "$url"
Reading the documentation, I see that
A variable's scope extends to the "end" action of the control structure ("if", "with", or "range") in which it is declared, or to the end of the template if there is no such control structure.
Does this mean that there are no global (or scoped on the whole template) variables? Or is there a way to define $url so that it can be reused later in the template?
Variables are scoped. You create the $url variable inside the {{if}} and {{else}} blocks, so they are not visible outside of those blocks.
Create the variable before the {{if}}, and use assignment = instead of declaration :=:
{{$url := ""}}
{{ if . }}
{{ $url = "http://true.com" }}
{{ else }}
{{ $url = "http://false.com" }}
{{ end }}
{{ $url }}
Testing it:
t := template.Must(template.New("").Parse(src))
fmt.Println(t.Execute(os.Stdout, true))
fmt.Println(t.Execute(os.Stdout, false))
Output (try it on the Go Playground):
http://true.com<nil>
http://false.com<nil>
Note: Modifying template variables with assignment was added in Go 1.11, so you need to build your app with Go 1.11 or newer. If you're using an older version, you cannot modify values of template variables.
Edit: I've found a duplicate: How do I assign variables within a conditional
You can mimic "changeable template variables" in earlier versions, see this question for examples: In a Go template range loop, are variables declared outside the loop reset on each iteration?