Error Checking on Nested Logic

Below I have included a short template snippet and a sample XML file.

“Snippet 1” does what it is supposed to (extract lat/long/elevation from a KML place mark).

This works as long as the node (kmlFolder/kmlNode) being search for exists. If the node is not found Hugo throws a variety of errors depending on what is missing. I’ve tried wrapping the XML access functions in {{ with }} statements {{ else }} report error {{ end }}. The Error reporting works but now the scope/context of $coordsString is not correct to this fails “{{ $coords := split $coordsString “,” }}”

I can see how I could move all of the template clauses to the {{ where }} but with deeply nested tests this would get unwieldly quickly.

So Questions:

  1. is this the idiomatic Hugo way to check for missing nodes in deeply nest data? Or Is there another construct I should be using.
  2. If this is the idiomatic way, how can I surface a variable assignment made in nested “with” context to the page context so that I can use it in the following template lines.

I’m enjoying Hugo for the most part but I keep stumbling upon these little things that really break my brain… maybe that is why I enjoy it.

Thanks for your help.

Snippet 1 - Works

{{ $coordsString := "" }}
{{ $folderName := .Params.kmlFolder }}
{{ $placeName := .Params.kmlName }}

{{ $folder := index (where .Site.Data.X_Isle.Document.Folder "name" $folderName) 0 }}
{{ $placeMark := (index ( where $folder.Placemark "name" $placeName) 0 ) }}
{{ $coordsString := (index ( where $folder.Placemark "name" $placeName) 0 ).Point.coordinates }}
{{ $coordsString }}
{{ $coords := split $coordsString "," }}
{{ $lat := (index $coords 0) }}
{{ $lon := (index $coords 1) }}
{{ $elevation := (index $coords 2) }}

Snippet 2 - fails due to scope/context of $coordsString

{{ $coordsString := "" }}
{{ $folderName := .Params.kmlFolder }}
{{ $placeName := .Params.kmlName }}

{{ $folder := index (where .Site.Data.X_Isle.Document.Folder "name" $folderName) 0 }}
{{ debug.Dump $folder }}
{{ with $folder }}
  {{ $placeMark := (index ( where $folder.Placemark "name" $placeName) 0 ) }}
  <hr/>
  "Placemark: "
  {{ debug.Dump $placeMark }}

  {{ $coordsString := (index ( where $folder.Placemark "name" $placeName) 0 ).Point.coordinates }}
  <hr/>
  "Coord: "
  {{ debug.Dump $coordsString }}
    {{ with $coordsString }}
    {{ else }}
        {{ errorf "Could not find a place mmark for %s/%s" $folderName $placeName}}
    {{ end }}
{{ else }}
    {{ errorf "Could not find a XML Folder for " $folderName}}
{{ end }}
{{ $coordsString }}
{{ $coords := split $coordsString "," }}
{{ $lat := (index $coords 0) }}
{{ $lon := (index $coords 1) }}
{{ $elevation := (index $coords 2) }}

Sample XML

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">
<Document>
	<Folder>
    asdsad
		<name>Places1</name>
		<open>1</open>
		<Placemark>
			<name>home</name>
			<LookAt>
				<longitude>-126.3367438000934</longitude>
				<latitude>50.22476255664978</latitude>
				<altitude>0</altitude>
				<heading>35.42705455875917</heading>
				<tilt>41.41090368844717</tilt>
				<range>1762.066150215274</range>
				<gx:altitudeMode>relativeToSeaFloor</gx:altitudeMode>
			</LookAt>
			<styleUrl>#m_ylw-pushpin</styleUrl>
			<Point>
				<gx:drawOrder>1</gx:drawOrder>
				<coordinates>-126.3363483039566,50.22617188222286,0</coordinates>
			</Point>
		</Placemark>
		<Placemark>
			<name>work</name>
			<visibility>0</visibility>
			<LookAt>
				<longitude>-125.7237372351031</longitude>
				<latitude>49.8833713008373</latitude>
				<altitude>0</altitude>
				<heading>-66.16030558763055</heading>
				<tilt>25.77323748991006</tilt>
				<range>2453.223795630301</range>
				<gx:altitudeMode>relativeToSeaFloor</gx:altitudeMode>
			</LookAt>
			<styleUrl>#m_ylw-pushpin4</styleUrl>
			<Point>
				<gx:drawOrder>1</gx:drawOrder>
				<coordinates>-125.7197422049882,49.88759872356041,0</coordinates>
			</Point>
		</Placemark>
	</Folder>
    <Folder>
		<name>Places2</name>
		<open>1</open>
		<Placemark>
			<name>home</name>
			<LookAt>
				<longitude>-126.3367438000934</longitude>
				<latitude>50.22476255664978</latitude>
				<altitude>0</altitude>
				<heading>35.42705455875917</heading>
				<tilt>41.41090368844717</tilt>
				<range>1762.066150215274</range>
				<gx:altitudeMode>relativeToSeaFloor</gx:altitudeMode>
			</LookAt>
			<styleUrl>#m_ylw-pushpin</styleUrl>
			<Point>
				<gx:drawOrder>1</gx:drawOrder>
				<coordinates>-126.3363483039566,50.22617188222286,0</coordinates>
			</Point>
		</Placemark>
		<Placemark>
			<name>work</name>
			<visibility>0</visibility>
			<LookAt>
				<longitude>-125.7237372351031</longitude>
				<latitude>49.8833713008373</latitude>
				<altitude>0</altitude>
				<heading>-66.16030558763055</heading>
				<tilt>25.77323748991006</tilt>
				<range>2453.223795630301</range>
				<gx:altitudeMode>relativeToSeaFloor</gx:altitudeMode>
			</LookAt>
			<styleUrl>#m_ylw-pushpin4</styleUrl>
			<Point>
				<gx:drawOrder>1</gx:drawOrder>
				<coordinates>-125.7197422049882,49.88759872356041,0</coordinates>
			</Point>
		</Placemark>
	</Folder>
</Document>
</kml>

I haven’t attempted to understand the details, but you are initializing (:=) $coordsString inside of the with block where you should be assigning (=) a value to it.

Ok. That could do it. I feel like I should have been able to figure that out. I think that’s the first time I’ve encounter a language where initialization is a different syntax than assignment. I was surprised to see the := in the first place. Haven’t seen that since turbo Pascal :slight_smile:

I’ll try it on the morning

Thanks

https://go.dev/tour/basics/10
https://pkg.go.dev/text/template#hdr-Variables

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