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

should includes inherit parent properties? #15

Closed
RandomEtc opened this issue Feb 3, 2013 · 4 comments
Closed

should includes inherit parent properties? #15

RandomEtc opened this issue Feb 3, 2013 · 4 comments

Comments

@RandomEtc
Copy link

If I do:

res.locals.user = { name: 'Tom' }
res.render('page')

And in page.ect I do:

<% include 'user' %>

I'd like user.ect to have access to @user. Currently I must pass user explicitly:

<% include 'user', { user: @user } %>

This can get tedious for complex page. Is there a better way?

@baryshev
Copy link
Owner

baryshev commented Feb 3, 2013

By default all variables available in all templates hierarchy. But it can be overwritten. You don't need to pass user variable into include in your case.

@RandomEtc
Copy link
Author

Right. Sorry.

The use-case I'm thinking of is when I want to use an include to render an item in a collection, so I want to pass some one-off parameters to the included template as well as the parameters from the calling page.

So if I do:

res.locals.users = [ { name: 'RandomEtc' }, { name: 'baryshev' } ]
res.locals.otherInfo = 'foo'
res.render('page')

And in page.ect I do:

<% for user, i in @users: %>
  <% include 'user', { user: user, index: i } %>
<% end %>

I'd like user.ect to have access to @otherInfo. Currently I must pass it explicitly:

<% for user, i in @users: %>
  <% include 'user', { user: user, index: i, otherInfo: @otherInfo } %>
<% end %>

I should have used this example previously, apologies.

...

Thinking about it, I guess I could just expose an extend (or inherit or merge) helper method and do this:

<% for user, i in @users: %>
  <% include 'user', @extend(this, { user: user, index: i }) %>
<% end %>

But I'm wondering if this would be useful default behavior?

@baryshev
Copy link
Owner

baryshev commented Feb 4, 2013

Sorry, there is no native way to merge parameters right now. Merge may cause performanse issues in this context.

@RandomEtc
Copy link
Author

OK I'll write a helper.

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