Golang iris framework render inside a range - go

I'm trying to loop over items and render it in separate template, but {{ . }} in item.html contains root variables instead of a single item. I tried to assign item to a variable {{ range $i, $item := .Items }}, but the result is the same.
<!-- list.html -->
{{ range .Items }}
{{ . }} this one is OK
{{ render "item.html" }}
{{ end }}
<!-- item.html -->
{{ . }} this one is not

Related

How can I write an if statement in resources.Get Hugo?

I have the following code:
{{ with resources.Get .Site.Params.image }}
<meta property="og:image" content="{{ .Permalink }}" />
{{ end }}
I get a image path in the config.toml file. However I now want to be able to overwrite the file by frontmatter data... Something like this (in pseudo code):
{{ if isset .Image }}
{{ $image := .Image }}
{{ else }}
{{ $image := .Site.Params.image }}
{{ end }}
{{ with resources.Get $image }}
...
How can I write this statement? Also if you have good tutorials on these statements and syntax let me know!
Use the or function:
{{ with resources.Get (or .Params.thumbnailImage .Site.Params.ogImage) }}
<meta property="og:image" content="{{ .Permalink }}" />
{{ end }}
EDIT: I updated to #brendan solution.
This is my solution:
{{ $ogImage := "" }}
{{ if .Params.thumbnailImage }}
{{ $ogImage = .Params.thumbnailImage }}
{{ else }}
{{ $ogImage = .Site.Params.ogImage }}
{{ end }}
{{ $ogImageRender := resources.Get $ogImage }}
{{ with $ogImageRender }}
<meta property="og:image" content="{{ .Permalink }}" />
{{ end }}
:= defines variables and set initial value
= set variables to another value

Looping through a map in helm

I am new to yaml and helm templating and have a question in it.
I have the following in my values.yaml file
regionInfo:
usa:
DrRegions: uk
region: usa
region_key: usa
uk:
DrRegions: usa
region: uk
region_key: lhr
I need to iterate over the above map and the output should be in this format for each region
{{ $region }}:
dnsAbbrev: {{ region_key }}
abbrev: {{ region_key }}
drRegion: {{ DrRegions }}
I tried the following
Method 1
{{ range $reg, $props := .Values.regionInfo }}
{{ $reg }}:
dnsAbbrev: {{ $props.region_key }}
abbrev: {{ $props.region_key }}
drRegion: {{ $props.DrRegions }}
{{ end }}
Method 2
{{ range reg, props := .Values.regionInfo }}
{{ range $props }}
{{ $reg }}:
dnsAbbrev: {{ .region_key }}
abbrev: {{ .region_key }}
drRegion: {{ .DrRegions }}
{{ end }}
{{ end }}
I was getting couldn't range over map in both the scenarios... Any help or guidance on this is highly appreciated.

Use the same template with different param/variables in 1 page

I am using Go gin gonic for my web app. How do I use the same template file multiple times in 1 page with different variables passed to the template.
segment.tmpl
{{ define "segment" }}
<div>{{ .Variable }}</div>
{{ end }}
layout.tmpl
<!DOCTYPE HTML>
<html>
<body>
{{ template "segment . }} #with a variable 1
{{ template "segment . }} #with different variable
{{ template "segment . }} #another same template with another
</body>
</html>
main.go
r.GET("/home/", func(c *gin.Context) {
tmpl := template.Must(template.ParseFiles("templates/layout.tmpl", "templates/product_add.tmpl", "templates/segment.tmpl")
r.SetHTMLTemplate(tmpl)
c.HTML(200, "layout", gin.H {
"Variable1": "var1",
"variable2": "var2",
})
}
How do I use segment.tmpl multiple times in the page "home" and passing different kind of variables to the segment.tmpl?
I have searched everywhere and have found nothing, the closest thing is template.Clone, but still couldn't find any examples of it.
You can pass any value as the "pipeline" to the template, it doesn't have to be the "dot", i.e. you could pass the result of a function call, or, in this case, the result of accessing a map's value.
{{ template "segment" .Variable1 }}
and then inside the template "segment" you can refer to the pipeline using the dot, i.e. {{ . }}.
segment.tmpl
{{ define "segment" }}
<div>{{ . }}</div>
{{ end }}
layout.tmpl
<!DOCTYPE HTML>
<html>
<body>
{{ template "segment .Variable1 }}
{{ template "segment .Variable2 }}
{{ template "segment .AnotherVariable }}
</body>
</html>

URL's are repeated

I want to output my breadcrumbs using schema, but the #id repeats the URL twice or sometimes more than that?! So if I visit the about page, I see:
"#id":"http://localhost:1313/about/http://localhost:1313/about/",
When I use pagination, it repeats the URL even more:
"#id":"http://localhost:1313/blog/http://localhost:1313/blog//http://localhost:1313/blog/http://localhost:1313/blog//http://localhost:1313/blog/http://localhost:1313/blog/",
The code I am using:
Taken from: https://gohugohq.com/partials/breadcrumb-partial-with-structured-data-in-hugo/
{{ $url := replace .Permalink ( printf "%s" .Site.BaseURL) "" }}
{{ $.Scratch.Add "path" .Site.BaseURL }}
{{ $.Scratch.Add "breadcrumb" (slice (dict "url" .Site.BaseURL "name" "home" "position" 1 )) }}
{{ range $index, $element := split $url "/" }}
{{ $.Scratch.Add "path" $element }}
{{ $.Scratch.Add "path" "/" }}
{{ if ne $element "" }}
{{ $.Scratch.Add "breadcrumb" (slice (dict "url" ($.Scratch.Get "path") "name" . "position" (add $index 2))) }}
{{ end }}
{{ end }}
<script type="application/ld+json">
{
"#context": "http://schema.org",
"#type": "BreadcrumbList",
"itemListElement": [{{ range $.Scratch.Get "breadcrumb" }}{{ if ne .position 1 }},{{ end }}{
"#type": "ListItem",
"position": {{ .position }},
"item": {
"#id": "{{ .url }}",
"name": "{{ .name }}"
}
}{{ end }}]
}
</script>
So I'm not sure what your list page template looks like, but for example in mine I had
{{ partial "header.html" . }}
when it should have been
{{ partial "header" . }}
This removed the repeating url's. I have all of the same code you have rendering in my header partial.
There are 2 reasons URLs are repeated
Its known bug with hugo hot reloading. But final production version will not have it. So run hugo and check the public folder.
If the issue persists on public folder then check how many partials you are doing {{ $.Scratch.Add "path" .Site.BaseURL }}. After you add it once then same data is available via scratch on all partials of the same page.
I have blog post on breadcrumb partial for hugo with json-ld
I reuse the same scratch to display breadcrumbs on page.
DONT JUST ADD BREADCRUMBS FOR SEARCH ENGINE. SHOW THEM TO USERS ALSO.

Symfony2 + CreateFormBuilder how to render an image in a form

I'm Symfony2 and CreateFormBuilder to create my form.
Currently I'm using {{ form_widget(form) }} to display the form.
My entity have path property that is the path of an image save on filesystem.
I want to display the image in the form (with a <img> html tag), how can I achieve this result? Should I handle the form in my template field by field? Or is there a way to own only one field in the template and render the other ones with {{ form_widget(form) }} ?
What you could do is handling the form field by field, and display the image if the value is set.
Let's say you have a field name image. In Twig, you can access its value through form.vars.value.image. Then, it's quite easy to display an img tag:
{% if form.vars.value.image is not null %}
<img src="{{ asset('upload/dir/' ~ form.vars.value.image) }}" />
{% endif %}
Here, upload/dir/ is a path where you store your images. If you have a constant for this path, you can use it in Twig:
{{ asset(constant('Acme\\DemoBundle\\Model\\Object::UPLOAD_DIR') ~ '/' ~ form.vars.value.image) }}
An alternative could be to create your own type with its own template:
http://symfony.com/doc/current/book/forms.html#form-theming
http://symfony.com/doc/current/cookbook/form/form_customization.html
Edit: I forgot an interesting alternative. You can customize an individual field: http://symfony.com/doc/current/cookbook/form/form_customization.html#how-to-customize-an-individual-field. Here is a draft of what you could do:
{% form_theme form _self %}
{% block _object_image_row %}
<div class="name_row">
{{ form_label(form) }}
{{ form_errors(form) }}
{{ form_widget(form) }}
{% if form.vars.value.image is not null %}
<img src="{{ asset('upload/dir/' ~ form.vars.value.image) }}" />
{% endif %}
</div>
{% endblock %}

Resources