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:
- is this the idiomatic Hugo way to check for missing nodes in deeply nest data? Or Is there another construct I should be using.
- 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>