HUGO

Question about {{ printf "%#v" . }}

(Note: This is about Hugo variables, but on a hosted blogging site I do not have access to except as a user. I’m hoping this particular question has enough of a hook into the general case that the specifics of that blog system won’t matter, but you never know.)

The output of the above on, e.g. a photos page, is:

&
hugolib.PageOutput {
    Page: ( * hugolib.Page)(0xc000d22000),
    paginator: ( * hugolib.Pager)(0xc000b5cc00),
    paginatorInit: sync.Once {
        m: sync.Mutex {
            state: 0,
            sema: 0x0
        },
        done: 0x0
    },
    resources: resource.Resources {},
    resourcesInit: sync.Once {
        m: sync.Mutex {
            state: 0,
            sema: 0x0
        },
        done: 0x1
    },
    targetPathDescriptor: hugolib.targetPathDescriptor {
        PathSpec: ( * helpers.PathSpec)(0xc0004a1380),
        Type: output.Format {
            Name: "photoshtml",
            MediaType: media.Type {
                MainType: "text",
                SubType: "html",
                mimeSuffix: "",
                Delimiter: ".",
                Suffixes: [] string {
                    "html"
                },
                fileSuffix: ""
            },
            Path: "photos",
            BaseName: "index",
            Rel: "alternate",
            Protocol: "",
            IsPlainText: false,
            IsHTML: false,
            NoUgly: false,
            NotAlternative: true
        },
        Kind: "home",
        Sections: [] string(nil),
        BaseName: "",
        Dir: "",
        LangPrefix: "",
        IsMultihost: false,
        URL: "",
        Addends: "",
        ExpandedPermalink: "",
        UglyURLs: false
    },
    outputFormat: output.Format {
        Name: "photoshtml",
        MediaType: media.Type {
            MainType: "text",
            SubType: "html",
            mimeSuffix: "",
            Delimiter: ".",
            Suffixes: [] string {
                "html"
            },
            fileSuffix: ""
        },
        Path: "photos",
        BaseName: "index",
        Rel: "alternate",
        Protocol: "",
        IsPlainText: false,
        IsHTML: false,
        NoUgly: false,
        NotAlternative: true
    }

}

Is there no way to grab that Path”photos" for use in an if eq?

(The system in question is still on 0.53 and is working toward upgrade.)

Thanks.

Hi there,

Have a look at Page Variables. Perhaps .Dir or other .File variables might be helpful.

Thanks but alas no, none of the typical variables work because both pages are considered “home” and so there’s no specific hooks to “photos” or “archives” to grab. (That’s why I’m asking about the info that the debugging template tag does produce but I can’t seem to figure out how to actually make use of.)

it seems what you call a “photos page” is a Custom Output Format isn’t it?

If so, unfortunately there is currently no way to test for the “current” output format.

What I do though is use .AlternateOutputFormats and OutputFormats and check what’s left.

This is the returning partial I use, I’m sure it could be improved (you should replace: $outputs with your own.

{{/*
  GetCurrentOutput
  Retrieves the OuputFormat of the page, from a list of predefined outputs  
  
  @author @regisphilibert

  @context Page (.)

  @access public

  @return A String among the predefined list

  @example - Go Template
    {{ $currentOutputFormat := partial "func/GetCurrentOutput" . }}

  @warning This partial cannot be cached.
*/}}

{{/*  We create a slice listing the concerned output formats */}}
{{ $outputs := slice "default" "ug1" "ug2" }}
{{ $alt := slice }}
{{/* We range on the page's Alternative Output Formats which returns all output formats
except the current one.  */}}
{{ range .AlternativeOutputFormats }}
{{/* If an output format matches one in the concerned list, we add it to our slice of outputs */}}
  {{ if in $outputs .Name }}
    {{ $alt = $alt | append .Name }}
  {{ end }}
{{ end }}
{{ $current := "default" }}
{{/* If any alternate output formats part of the "concerned" ones have been found, we range on them. */}}
{{ with $alt }}
  {{ range $outputs }}
    {{/* If the output format is not listed as an "alternate", it means it is the current one.  */}}
    {{ if not (in $alt .) }}
      {{ $current = . }}
    {{ end }}
  {{ end }}
{{ end }}

{{ return $current }}

Thanks. It will take me awhile to understand that (I’m not by any means a programmer, but typically if I spend some time I can follow the story of a piece of code and that often suffices), but just for my own clarity in the meantime: can someone explain why you can see what I considered in the original post to be the “relevant” bit of data when doing the printf "%#v" . but can’t actually call upon that bit of data in template tags themselves?

Thanks.

FWIW, I ended up doing an end run around Hugo and simply output that printf to a hidden <span> and then used JavaScript to use that string to make css selectors which let me hide unwanted content on those pages. :man_shrugging: