Skip to content

Scratch and Output Formats #3592

@sascha53

Description

@sascha53

I use partials for inlining svg defs, for example:

{{- $icon_id := "line-chart" -}}
{{- $icon_name := add "icon-" $icon_id -}}
{{- if not (.Scratch.Get $icon_name) -}}{{- .Scratch.Set $icon_name true -}}<svg class="clip" xmlns="http://www.w3.org/2000/svg" width="0" height="0"><defs><svg id="{{ $icon_id }}" xmlns="http://www.w3.org/2000/svg" fill="currentcolor" viewBox="0 0 1792 1792"><path d="M1920 1536v128h-2048v-1536h128v1408h1920zm-128-1248v435q0 21-19.5 29.5t-35.5-7.5l-121-121-633 633q-10 10-23 10t-23-10l-233-233-416 416-192-192 585-585q10-10 23-10t23 10l233 233 464-464-121-121q-16-16-7.5-35.5t29.5-19.5h435q14 0 23 9t9 23z"/></svg></defs></svg>{{- end }}

The goal is to inline a svg defs just once per page, as i reference them with xlink:href. This is what the if statement is made for and that worked well before activating another output format like AMP, which is also using this icon partial in its template.

It seems that the vars defined in Scratch are "reseted" per page, but if you use multiple Output Formats, they are shared and not reseted. The undesired behaviour in my case: Either on the HTML or AMP version of the same page, which is rendered after the other (which is random) the var is already set to true and the svg def isn't rendered.

This also doesn't work:

{{- $id := "facebook" -}}
{{- $name := add "icon-" $id -}}
{{- if lt (.Scratch.Get $name) (len .OutputFormats) -}}
{{- .Scratch.Set $name (add (int (.Scratch.Get $name)) 1) -}}
<svg class="clip" xmlns="http://www.w3.org/2000/svg" width="0" height="0">
<defs>
<svg viewBox="0 0 1792 1792" xmlns="http://www.w3.org/2000/svg" id="{{ $id }}" fill="currentcolor"><path d="M1343 12v264h-157q-86 0-116 36t-30 108v189h293l-39 296h-254v759h-306v-759h-255v-296h255v-218q0-186 104-288.5t277-102.5q147 0 228 12z"/></svg>
</defs>
</svg>
{{- end }}

The solution would be either a proper reset of the Scratch vars for every Output Format. Or a page variable that tells me which output format is currently rendered (or extension) and that i can use in an if statement. Or maybe something else.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions