Get variable value by index outside of `.Section`

Trying to access a variable by index value in a .Section outside of the section (i.e. index.html).

So, in a YAML file, I have something like this…



        total: 23
        nonICU: 20
        icu: 3
{{ $pages := where site.RegularPages.ByTitle.Reverse "Section" "reports" }}

{{ range $i,$e := $pages | last 2 }}

  {{ }}


returns something along the lines of this:

map[icu:8 nonicu:102 total:110] map[icu:11 nonicu:108 total:119]

I was hoping to access the items along the lines of something like this:

{{ (index 0 ).total }}

Obviously, that’s not working. I’ve tried .NextInSection with the range here, but it returns everything in the section, so, I can’t take advantage of it like it would work inside the section.

If anyone could tell me how to access these values in individually, that would be awesome.

The devil is in the details; I don’t see any 'inHospital´ in the YAML you post.

Sorry, must have deleted it. I’ll make the edit to reflect it.

This is what I ended up doing to get this working satisfactorily, but I think this is really hackish…

{{ $pop := 389410.00 }}
{{ $prevTotalICU := 0}}
{{ $prevTotalNonICU := 0}}
{{ $prevTotalInHospital := 0 }}
{{ range ( where .Site.RegularPages.ByTitle "Type" "reports" | last 2 ).Reverse  }}
    {{ $previousICU := }}
    {{ $previousNonICU := }}
    {{ $previousTotal := }}
    {{ $prevTotalICU = add $prevTotalICU $previousICU }}
    {{ $prevTotalNonICU = add $prevTotalNonICU $previousNonICU }}
    {{ $prevTotalInHospital = add $prevTotalInHospital $previousTotal }}
{{ end }}
{{ range ( where .Site.RegularPages.ByDate "Type" "reports" | last 1 ).Reverse  }}
<section>{{ $report := }}{{ $cases := $report.cases }}{{ $active := $ }}
  <h2 class="[ h4 p--050 mb--050 ]">Hospitalization</h2>{{ $inHospital := $cases.inHospital.totals }}
  <figure class="[ mb--1 ]">{{ $icu := $ }}
    <table class="[ table--aggregate ] [ mb--1 ]">
      <thead>{{ $nonICU := $inHospital.nonICU }}
        <tr>{{ $total := $ }}
          <th class="[ text--left ]">Care Unit </th>
          <th>New </th>
        </tr>{{ $icuPointChange := sub (mul $icu 2) $prevTotalICU }}
      </thead>{{ $nonICUPointChange := sub (mul $nonICU 2) $prevTotalNonICU }}
      <tbody>{{ $totalPointChange := sub (mul $total 2) $prevTotalInHospital }}
          <th class="[ text--left reg ]">Non-ICU</th>
          <td class="[ text--center px--050 ]">{{if eq $nonICUPointChange 0}}—{{else}}{{$nonICUPointChange}}{{end}}</td>
          <td class="[ text--center px--050 ]">{{if eq $nonICU 0}}—{{else}}{{ $nonICU }}{{end}}</td>
          <th class="[ text--left reg ]">ICU</th>
          <td class="[ text--center px--050 ]">{{if eq $icuPointChange 0}}—{{else}}{{$icuPointChange}}{{end}}</td>
          <td class="[ text--center px--050 ]">{{if eq $icu 0}}—{{else}}{{$icu}}{{end}}</td>
          <th class="[ text--left reg ]">Total Cases </th>
          <td class="[ text--center px--050 ]">{{if eq $totalPointChange 0}}—{{else}}{{$totalPointChange}}{{end}}</td>
          <td class="[ text--center px--050 ]">{{if eq $total 0}}—{{else}}{{$total}}{{end}}</td>
    <figcaption>Fig.3.1 — COVID-19 (Coronavirus) hospitalizations in The Bahamas as of {{ .Date.Format "Monday, Jan 2, 2006" }}.</figcaption>
  <figure class="[ mb--d15 ]">
    <table class="[ table--rates ] [ mb--1 ]"> 
          <th class="[ text--left ]">Rate </th>
          <th>Per 100K</th>
          <td class="[ text--Center px--050 ]">{{ lang.NumFmt 2 (mul (div (float $total) $active ) 100) }}%</td>
          <td class="[ text--Center px--050 ]">{{ lang.NumFmt 2 (mul (div (float $total) $pop ) 100000) }}</td>
      <p>Fig.3.2 — Hospitalization Rate in The Bahamas as of {{ .Date.Format "Monday, Jan 2, 2006" }}.</p>
      <!-- <p>This rate is calculated using a twenty-eight day average of hospitalization total.</p> -->

Accessing by index would be a cleaner way.

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