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

Passing information to partials #66

Open
w-biggs opened this Issue Oct 5, 2018 · 12 comments

Comments

Projects
None yet
5 participants
@w-biggs
Copy link

w-biggs commented Oct 5, 2018

Is your feature request related to a problem? Please describe.
Say I have a {{> head}} partial that includes everything inside my <head> tag. Inside the head is a <title> tag. If I want to set the title to something different in each page, I, well, can't. There doesn't seem to be any way to pass data to a partial, like you normally can with handlebars-style templating engines.

Describe the solution you'd like
The ability to pass custom parameters to a partial, like {{> head title="About Us"}}.

Describe alternatives you've considered
I can just not use partials for the <head> (or any other element that's mostly repeated with minor differences in each page, like a <nav>), but that makes the code very non-DRY and kinda takes away a lot of the advantage of using a templating engine.

Additional context
Most handlebars-style templating engines (ex. Panini) let you do this.

@srobbin srobbin added the enhancement label Oct 5, 2018

@srobbin

This comment has been minimized.

Copy link
Contributor

srobbin commented Oct 5, 2018

@w-biggs Thanks, this is a good request.

How did you imagine we'd reference "title" in the partial? Like this?

<!DOCTYPE html>
<html>
<head>
  <title>{{title}}</title>
</head>
<body>

The issue I see here is that {{title}} would then be interpreted as a field in the General section. Maybe it could be written with an underscore, so it stayed a part of the private namespace (the parser currently ignores underscored variables, except _permalink, _created_at, and _updated_at).

<title>{{_title}}</title>

Or we could take a different approach, and instead of passing variables to a partial, we could use front matter, so that a page can have variables (which partials can also access). Middleman uses this, and it's pretty nice.

# index.html
---
title: My Page
---
{{> head}}
# _head.html
<title>{{_page.title}}</title>
@w-biggs

This comment has been minimized.

Copy link
Author

w-biggs commented Oct 5, 2018

I was actually thinking about suggesting front matter myself - it would work well for a wide number of use cases, such as <body class="{{_this.title}}"> (or however you'd call a variable from the page itself).

You'd probably want to allow template tags on variables, though, so clients can be given the ability to change variables (like page names) from the dashboard - something like:

---
title: {{aboutpagetitle label="About Page Title"}}
---

Although this might be a bit clunky and would probably be better solved by having each page be its own section with a single {{title}} field (which would require #12 to be implemented). But that's probably out of the scope of this issue.

@walidvb

This comment has been minimized.

Copy link

walidvb commented Oct 6, 2018

What if you want to render the same partial with different _title values? i.e a component which can has a title and a body

@Jack89ita

This comment has been minimized.

Copy link

Jack89ita commented Oct 8, 2018

@srobbin Nice solution! I'm currently having the same problem, not for normal pages in which i include different partials, but in listing item page in which i do not have the control of the "current item". Infact i am not able to differentiate items title and description.
Any tips about this?
Thanks

@srobbin

This comment has been minimized.

Copy link
Contributor

srobbin commented Oct 9, 2018

@Jack89ita I'm not sure I entirely understand, but it sounds like a good topic to bring up in the Vapid forums.

@walidvb

This comment has been minimized.

Copy link

walidvb commented Nov 11, 2018

@srobbin would you accept PRs for this?

@srobbin

This comment has been minimized.

Copy link
Contributor

srobbin commented Nov 12, 2018

@walidvb Yes, I'm open to a PR, but I think we should probably discuss the implementation a little bit first. Were you thinking of using front matter?

@walidvb

This comment has been minimized.

Copy link

walidvb commented Nov 12, 2018

@srobbin i might have spoken too fast, was speaking with another friend about this and we were both needing the functionality to improve DRY(as per #47 ).

I have a rails and haml background, so i would go with {> partial variable=value}. My opinion is that partials should be able to receive variables, and the frontmatter solution actually brings global variables to a template/html file.
This could maybe be solved by adding a {{variable hidden=true default=default_value}}?

@srobbin

This comment has been minimized.

Copy link
Contributor

srobbin commented Nov 12, 2018

@walidvb Re: frontmatter and partials, I was thinking it might work something like this:

# index.html
---
title: My Page
---
{{> head }}

{{> another foo=bar}}
# _head.html
<title>{{_page.title}}</title>
# _another.html
<h1>{{_page.title}}</h1>
<p>{{_page.foo}}</p>

Where partial variables are merged into the frontmatter "_page" namespace. That might provide a nice way to set values for both the template and partial.

Also, re: DRY, this might naturally lead to a discussion of layouts.

@walidvb

This comment has been minimized.

Copy link

walidvb commented Nov 14, 2018

Can this be done in 2 steps?
First, accept variables from the the direct syntax { > partial varName=varValue}, then add front-matter for global variables? what do you think?

@stale

This comment has been minimized.

Copy link

stale bot commented Feb 12, 2019

This issue has been automatically marked as stale after 90 days of inactivity. It will be closed if no further activity occurs.

@stale stale bot added stale and removed stale labels Feb 12, 2019

@enigmatic-user

This comment has been minimized.

Copy link

enigmatic-user commented Mar 13, 2019

Starting my first Vapid project, I also miss this feature very much. I hope the discussion here hasn't died...

@srobbin srobbin added the pinned label Mar 14, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.