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

Include Template can't read parent's variables #9815

Closed
dylangrech92 opened this issue Dec 31, 2020 · 5 comments
Closed

Include Template can't read parent's variables #9815

dylangrech92 opened this issue Dec 31, 2020 · 5 comments

Comments

@dylangrech92
Copy link
Contributor

dylangrech92 commented Dec 31, 2020

4.6.2

Description

I have to explicitly pass variables to Include templates when using renderWith().
Is this the correct behaviour?

Steps to Reproduce

return $this->renderWith('MyTemplate', [
    'MyVar' => new MyClass()
]);

This works

MyTemplate.ss

<p>$MyVar.SomeMethod</p>

This doesn't work

MyTemplate.ss 
<% include MyInclude %>

MyInclude.ss
<p>$MyVar.SomeMethod</p>

This works

MyTemplate.ss 
<% include MyInclude MyVar=$MyVar %>

MyInclude.ss
<p>$MyVar.SomeMethod</p>
@kinglozzer
Copy link
Member

Just out of curiosity - does this behave differently if you use:

return $this->customise([
    'MyVar' => new MyClass()
])->renderWith('MyTemplate');

@dhensby
Copy link
Contributor

dhensby commented Jan 4, 2021

I believe this is by design

@lerni
Copy link
Contributor

lerni commented Jan 4, 2021

related? to #8580 (comment) and also links to "by design".

@dhensby
Copy link
Contributor

dhensby commented Jan 4, 2021

I'm not sure those two are related, but includes are not meant to inherit scope; their context needs to be passed in as show in the "this works" section.

I even had this "problem" way back in ss3, I think. It's not necessarily intuitive (as in PHP an "include" will inherit the variables declared in scope), but the templates aren't PHP and I don't think there's any desire from maintainers to change this behaviour especially when a thought out and reasonable decision was made at the time to explicitly exclude the external scope.

As such, I'm going to close this issue

@dhensby dhensby closed this as completed Jan 4, 2021
@dylangrech92
Copy link
Contributor Author

Thanks for the clarifications guys, it's ok that it's by design just something to keep in mind moving forward :)

Thanks again.

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

4 participants