Accessing parent scope in content view

I have a basic li content view:

<li>
     <a href="{{ .Params.link }}">{{ .Title }}</a> (Published on {{ .Date.Format "Jan 2, 2006" }}) <br/>
     {{ with .Site.GetPage "section" .Section }} <a href="/{{ .Section | urlize }}/">{{ .Title }}</a> {{ end }}
    {{ range .Params.tags }} <a href="/tags/{{ . |urlize }}">{{ . }}</a> {{ end }}
</li>

I call it from index.html and section.html with the range:

{{ range .Data.Pages }}
    {{ .Render "li"  }}
{{ end }}

I want the Section hyperlink to show if this li content view is invoked from inside a home page, but not from inside a section (since all the links will already be for that section).

I can’t figure out how to pass that information in. I know the .Kind variable in the template will return home or section but by the time I am in the content view, my context is now the individual link/page, and the .Kind is always a page.

How can I pass in the parental scope or some other equivalent indicator for the li element to display slightly differently? Obviously, I could render li_home and li_section but that seems like too much of a duplication if the difference is one if/display flag.

I did not test this, but for me, it looks like your li content view has access to the scope from {{ range .Data.Pages }} this would be my approach to make this working:

index.html / section.html

{{ $pageScope := . }}
{{ range .Data.Pages }}
    {{ .Render "li"  }}
{{ end }}

li.html

{{ $pageScope | jsonify }} -> here you should have access to this variable...
<li>
     <a href="{{ .Params.link }}">{{ .Title }}</a> (Published on {{ .Date.Format "Jan 2, 2006" }}) <br/>
     {{ with .Site.GetPage "section" .Section }} <a href="/{{ .Section | urlize }}/">{{ .Title }}</a> {{ end }}
    {{ range .Params.tags }} <a href="/tags/{{ . |urlize }}">{{ . }}</a> {{ end }}
</li>

I did not test this out, so its more like a guess but I hope it works :slightly_smiling:

I am afraid not:

template: link/li.html:6: undefined variable "$pageScope"

I’ve double checked that both parent templates define that variable. And if I paste the line from li.html up to index.html it works there. I’d say the context is reset within the range loop.

Try to use dict

This does not seem to work for content views, all the examples I found were for partials. I am not sure whether the implementations are different.

ERROR 2017/03/10 18:24:30 Error while rendering “page tutorial/_index.md”: template: …/layouts/_default/baseof.html:10:57: executing “main” at <“This is some text.”>: wrong type for value; expected string; got map[string]interface {}

The include line is:
{{ .Render "li" (dict "page" . "custom_heading" "This is some text.") }}

Did you resolve this issue? I am also having the same problem – I would like to pass dict with a .Render call, but it does not seem possible