HUGO

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…

report:

  cases: 
   
 inHospital:

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

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

  {{ .Params.report.cases.inHospital.totals }}

{{end}}

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 .Params.report.cases.inHospital.totals 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 := .Params.report.cases.inHospital.totals.icu }}
    {{ $previousNonICU := .Params.report.cases.inHospital.totals.nonICU }}
    {{ $previousTotal := .Params.report.cases.inHospital.totals.total }}
    {{ $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 := .Params.report }}{{ $cases := $report.cases }}{{ $active := $cases.active }}
  <h2 class="[ h4 p--050 mb--050 ]">Hospitalization</h2>{{ $inHospital := $cases.inHospital.totals }}
  <figure class="[ mb--1 ]">{{ $icu := $inHospital.icu }}
    <table class="[ table--aggregate ] [ mb--1 ]">
      <thead>{{ $nonICU := $inHospital.nonICU }}
        <tr>{{ $total := $inHospital.total }}
          <th class="[ text--left ]">Care Unit </th>
          <th>New </th>
          <th>Total</th>
        </tr>{{ $icuPointChange := sub (mul $icu 2) $prevTotalICU }}
      </thead>{{ $nonICUPointChange := sub (mul $nonICU 2) $prevTotalNonICU }}
      <tbody>{{ $totalPointChange := sub (mul $total 2) $prevTotalInHospital }}
        <tr> 
          <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>
        </tr>
        <tr> 
          <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>
        </tr>
        <tr> 
          <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>
        </tr>
      </tbody>
    </table>
    <figcaption>Fig.3.1 — COVID-19 (Coronavirus) hospitalizations in The Bahamas as of {{ .Date.Format "Monday, Jan 2, 2006" }}.</figcaption>
  </figure>
  <figure class="[ mb--d15 ]">
    <table class="[ table--rates ] [ mb--1 ]"> 
      <thead> 
        <tr> 
          <th class="[ text--left ]">Rate </th>
          <th>%</th>
          <th>Per 100K</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>Hospitalization</td>
          <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>
        </tr>
      </tbody>
    </table>
    <figcaption> 
      <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> -->
    </figcaption>
  </figure>
</section>
{{end}}

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.