I have created a file in the webroot with resources.ExecuteAsTemplate…
that works…
I was hoping to copy the rendered/created file to a few different locations, as the content is accessed via a few different mechanisms, and since the rendered file is <1k I thought it would be the most “elegant”/least complex way of having the same content accessible from several distinct paths (that do not end with a trailing / or .html)
I didn’t want to use the upstream webserver to alias/rewrite, as that would make localdev a little more wonky.
I tried a few different ways, which clearly didn’t work, and figured that clearly I’m doing something daft…
this shows what I’ve tried… and what didn’t work…
I have a gut feeling that perhaps resources.GetMatch might be the way to go, but figured it wouldn’t hurt to ask here in case there’s a less janky way to accomplish this
{{- $.Scratch.Set "includeTarget" true }}
{{- $.Scratch.Set "durableResource" false }}
{{- $.Scratch.Set "fileData" false }}
{{- $.Scratch.Set "fileTarget" "/someasset.json" }}
{{- $.Scratch.Set "additionalTargets" slice ('/someOtherAsset.json' '/myAssetBringsAllTheBoysToTheYard.json' '/polkaPartyPrince.json') }}
{{- $jsonFileTemplate := resources.Get 'jsonTemplate.json-template' }}
{{- $primaryTarget := ($.Scratch.Get "fileTarget") }}
{{- $.Scratch.Set "fileData" ($jsonFileTemplate | resources.ExecuteAsTemplate $primaryTarget .) }}
{{- if eq ($.Scratch.Get "includeTarget" ) true }}
{{- /* We should include the created file in page metadata */ }}
{{- warnf "[DEBUG] add Target %s to header metadata " $primaryTarget }}
{{- $renderedFileRsrc := ($.Scratch.Get "fileData").RelPermalink }}
{{- $.Scratch.Set "durableResource" $renderedFileRsrc }}
<link href="{{ $renderedFileRsrc }}" />
{{- else }}
{{- warnf "[DEBUG] don't add Target (%s) to metadata. Just .Publish it. " $primaryTarget }}
{{- $renderedFileRsrc := ($.Scratch.Get "fileData").Publish }}
{{- end }}
{{- with $additionalTargets := ($.Scratch.Get "additionalTargets") }}
{{- warnf "I have yet to figure out how to copy a template-created resource. So... Cannot duplicate the file. 💩 your day!" }}
{{- /* I had hoped something like this would work. */ }}
{{- /* Copy the main file to its variants and publish them. */ }}
{{- /* $resource := ($.Scratch.Get "durableResource") */ }}
{{- /* range $variant := $fileVariants */ }}
{{- /* No worky */ }}
{{- /* $variantFile := resources.Copy $variant (resources.Get $renderedFileRsrc ) */ }}
{{- /* Also No worky */ }}
{{- /* $variantFile := resources.Copy $variant (resources.Get ($.Scratch.Get "fileData") ) */ }}
{{- /* You GUESSED IT! Also ALSO No worky */ }}
{{- /* $variantFile := resources.Copy $variant (resources.Get ($.Scratch.Get "fileTarget") ) */ }}
{{- /* warnf "[DEBUG] DON'T add TargetVariant %s to header metadata. Just Publish it. " (string $variantFile ) */ }}
{{- /* $variantFile.Publish */ }}
{{- /* end }}
{{- end }}