Jsonify minify

jsonify function when invoked as follows: {{- $.Scratch.Get "index" | jsonify -}} produces an unminified output (Example here, generated with the code below). There ought to be a minification option (by invoking https://golang.org/pkg/encoding/json/#Compact ?)

Appendex: Code used to generate example json

{{- $.Scratch.Add "index" slice -}}
{{- range .Site.Pages -}}
    {{$FileLogicalName := ""}}
    {{ with .File }}{{$FileLogicalName := .LogicalName }}{{ end }}
    {{ $params := merge .Params (dict "title" .Title "relUrl" (replace .Permalink .Site.BaseURL "") "logicalName" $FileLogicalName) }}
    {{- $.Scratch.Add "index" $params -}}
{{- end -}}
{{- $.Scratch.Get "index" | jsonify -}}

jsonify produces minified output. the one who left all those whitespaces are your template.
you can add extra dash - to each template delimiter {{- and -}}.

{{- $.Scratch.Add "index" slice -}}
{{- range .Site.Pages -}}
    {{- $FileLogicalName := "" -}}
    {{- with .File -}}{{- $FileLogicalName := .LogicalName -}}{{- end -}}
    {{- $params := merge .Params (dict "title" .Title "relUrl" (replace .Permalink .Site.BaseURL "") "logicalName" $FileLogicalName) -}}
    {{- $.Scratch.Add "index" $params -}}
{{- end -}}
{{- $.Scratch.Get "index" | jsonify -}}

or run hugo with --minify flags.

1 Like

Thanks! Separate (incidental) question:

Why am I getting any empty dict for .File above?

(Source code, if useful is at https://github.com/vvasuki/saMskAra )

use = if you want to overwrite outer variable inside a with scope.

{{- $FileLogicalName := "" -}}
{{- with .File -}}
++{{- $FileLogicalName = .LogicalName -}}
{{- end -}}
1 Like

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