Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Pass variables to layouts #77

Open
mzeinstra opened this issue Aug 17, 2023 · 3 comments
Open

Feature: Pass variables to layouts #77

mzeinstra opened this issue Aug 17, 2023 · 3 comments

Comments

@mzeinstra
Copy link
Contributor

mzeinstra commented Aug 17, 2023

It might be interesting to allow passing variables to layouts to make layouts more flexible and self contained.

e.g. I want to get references from a statement on Wikibase and want to print it in something like:

<h2>{{ .title }}</h2>
<p>reference 1</p>
<p>reference 2</p>

I would now do:

<h1>{{ .title }}</h1>
{{ $data := query "references_by_statement.rq" .linkToStatement }}
{{ template "references" $data }}

with the template for "references" being

{{ if . }}
	{{ range . }}
	<p>{{ .text }}</p>
	{{ end }}
{{ end }}

Instead it would be interesting to reserve pass the title to the layout so that the H1 can be part of the layout. i.e.

{{ $data := query "references_by_statement.rq" .linkToStatement}}
{{ template "references" $data .title }}
{{ if . }}
	<h1>{{ .$0 }}</h1>
	{{ range . }}
	<p>{{ .text }}</p>
	{{ end }}
{{ end }}

Because sometimes you want to use that overarching information in your css or js to make the site more interactive.

With this structure we could allow passing up to 9 variables to the layout.

@Abbe98
Copy link
Collaborator

Abbe98 commented Aug 18, 2023

How does this idea compare to using multiply block definitions in a layout?

@mzeinstra
Copy link
Contributor Author

I see I made a typo. I wanted to do this:

{{ $data := query "references_by_statement.rq" .linkToStatement}}
{{ template "references" $data .title }}

how would you do this in multiple blocks? Maybe my understanding of blocks and scoping is insufficient.

@Abbe98
Copy link
Collaborator

Abbe98 commented Aug 22, 2023

As of right now template can take a variable as input, the common pattern is to pass . to it, for it to inherit the global set of view-variables. With #37 it would be trivial to modify it and thus achieve the same outcome as using multiple variables, however, I would imagine such a use being a code-smell, as having local computation modify layout behavior outside of blocks is a type of side-effect.

It would be interesting to see what downsides there are with the following pattern using blocks:

layouts/base.html

<h1>{{ block "title" . }}{{ end }}</h1>
{{ block "references" . }}{{ end }}

my-template.html

{{ template "base" . }}
{{ define "title" }}{{ .title }}{{ end }}

{{ $data := query "references_by_statement.rq" .linkToStatement }}
{{ define "references"}}
  {{ range $data }}
    <p>{{ .text }}</p>
  {{ end }}
{{ end }}

There is also include/include_text for working with non-layout templating as well as current_view which is commonly used in layouts to manage things like JavaScript and CSS resources.

Notably include/include_text only supports one argument as well but that should be fixed as passing data to a child template is not an issue. #79

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants