Hello. I have a code in my template
{{ .Scratch.Set "hosting_type" "dedicated" }}
{{ $hosting_type := .Scratch.Get "hosting_type" }}
{{ $url_type = printf "%s/" $hosting_type }}
{{ $url_windows_en := delimit (slice "/hosting/" $url_type "windows/") "" }}
<br>
<b>{{ .RelPermalink }}</b>
<br>
<b>{{ $url_windows_en }}</b>
<br>
{{ if or (eq .URL $url_windows_en) }}
test code
{{ end }}
But on a page I see a result:
<br>
<b>/hosting/dedicated/windows/</b>
<br>
<b>/hosting/dedicated/windows/</b>
<br>
Why I don’t see the phrase “test code”?
zwbetz
2
To start, your if/or statement is missing a 2nd arg.
@zwbetz, which argument I lost?
{{ if or (eq .URL $url_windows_en) }} is as {{ if eq .URL $url_windows_en }}, right?
One arg - .URL
Two arg - $url_windows_en
If you don’t see test code
it means your if
statement evaluates to false
.
or
is missing a second arg:
{{ if or (eq .URL $url_windows_en) }}
'------ or arg 1 -------' '-- or arg 2 missing --'
What is the value of .URL
? (Page .URL
is being deprecated, by the way)
To help you debug, print {{ eq .URL $url_windows_en }}
. It is probably false
.
2 Likes
It doesn’t matter.
Okay, I add this code
{{ .Scratch.Set "mytype" "" }}
{{ $mytype := .Scratch.Get "mytype" }}
{{ $myurl := "" }}
{{ if ne $hosting_type ""}}
{{ $myurl = printf "%s/" $mytype }}
{{ end }}
{{ $url_windows := delimit (slice "/hosting/" $myurl "windows/") "" }}
<b>{{ .RelPermalink }}</b><br>
<b>{{ $url_windows }}</b><br>
{{ eq .RelPermalink $url_windows }}
And the result is
<b>/hosting/windows/</b><br>
<b>/hosting/windows/</b><br>
false
Why? I think something problem with this code
{{ $url_windows := delimit (slice "/hosting/" $myurl "windows/") "" }}
Maybe type does not string?
Solution:
{{ .Scratch.Set "mytype" "" }}
{{ $mytype := .Scratch.Get "mytype" }}
{{ $myurl := "" }}
{{ if ne $hosting_type ""}}
{{ $myurl = printf "%s/" $mytype }}
{{ end }}
{{ $url_windows := delimit (slice "/hosting/" $myurl "windows/") "" }}
<b>{{ .RelPermalink }}</b><br>
<b>{{ $url_windows }}</b><br>
{{ eq .RelPermalink (string $url_windows) }}