Help with conditional or ((or (in A b) (in A c))

Do you agree that this variable $IsRemote

{{- $destination := .Destination }}
{{- $isRemote := or (or (in $destination ":") (strings.HasPrefix $destination "www")) (strings.HasPrefix $destination "//") }}

Should equal true for [essai](www.aol.fr) ? It doesn’t.
Complete code:

{{- $destination := .Destination }}
{{- $isRemote := or (in $destination ":") (strings.HasPrefix $destination "www") (strings.HasPrefix $destination "//") }}

{{- if not $isRemote }}
  {{- $url := urls.Parse .Destination }}
  {{- $path := strings.TrimSuffix "/_index.md" $url.Path }}
  {{- $path = strings.TrimSuffix "/_index" $path }}
  {{- $path = strings.TrimSuffix ".md" $path }}
  {{- $page := site.GetPage $path }}
  {{- if $page }}
    {{- $destination = $page.RelPermalink }}
    {{- if $url.Fragment }}
      {{- $destination = print $destination "#" $url.Fragment }}
    {{- end }}
  {{- else if fileExists (print .Page.File.Dir .Destination) }}
    <!-- Nothing -->
  {{- else -}}
		{{ with resources.Get .Destination }}
		{{$destination = .RelPermalink}}
		{{end}}
 {{- end }}
 {{- end }}
<a href="{{ $destination }}"{{ with .Title}} title="{{ . }}"{{ end }}>{{ .Text | safeHTML }}</a>

By the way, why can’t conditionals like or, and if have multiple operands ? It would simplify writing. Otherwise, this works perfectly, no need for relref, nor giving the path for images. It finds the resource wherever it is, pages assets or static.

both and and or supports multiple operands. But plain if only supports single operand, need to combine with and and or.


also, i think because you are new member the link above doesnt work. you can write it inside codeblock

Ahah no, I just forgot to escape it.
Ok I just followed someone’s code without trying.
However, {{- $isRemote := or (in $destination ":") (strings.HasPrefix $destination "www") (strings.HasPrefix $destination "//") }} doesn’t make it see www.aol.fr as a remote link either.

your first codeblock and second codeblock (complete code) are different tho. I think that’s why it doesnt produce true

First code block:

{{- $isRemote := or (or (in $destination ":") (strings.HasPrefix $destination "www")) (strings.HasPrefix $destination "//") }}

Second code block: (it’s not checking www string prefix)

{{- $isRemote := or (or (in $destination ":") (strings.HasPrefix $destination "//")) (strings.HasPrefix $destination "//") }}

Yeah I made a mistake, sorry.
But neither the first one or {{- $isRemote := or (in $destination ":") (strings.HasPrefix $destination "www") (strings.HasPrefix $destination "//") }}
work.

it works for me, $isRemote returns true

It worked for me too, but I lacked safeURL.
Ok, I understood that I had two issues. First off, this one: have the same issue that:

solved {{with | safeURL}}.
Then I thought resources.Get .Destination could work like .Get and find the resource with only the name, without the path. I was wrong. Now it’s perfect.
Thanks !

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.