-
Notifications
You must be signed in to change notification settings - Fork 179
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
Bug when pushing to sections inside of fetched templates #305
Comments
Good find. My gut tells me that this should be using the parent section content when pushing or not or define its own. I think this should be doable to implement. |
Hey glad to see you're back! I did some debugging in the past, but it's been too long, but I want to make some context for others reading this. We have the plates/src/Template/Template.php Lines 308 to 317 in ec07462
The child template ( Given that I think a possible solution would be to code the fetch method something like this to have access to the child's metadata: public function fetch($name, array $data = array())
{
$template = $this->engine->make($name);
$this->sections = array_merge($this->sections, $template->sections);
// here we could grab more info from the child
return $template->render($data);
} I'm assuming a high risk here of talking something completely nonsense hahaha, but this seems to make sense. |
Yeah, my idea above would never work, but I made some tests and implemented some working code in #306. There's a section mode issue though, described in the PR, that would be very nice if you could give your opinion. |
I think I covered everything I wanted at this point, it would be nice to have your input on the PR, thanks! |
And I thought I would be the only one with the use case ;) Thanks for the efforts so far. I like the input #306 (comment) from ragboyjr. This is a (quick and dirty) fix (based on extensions) for my use case, where I want to push some script data to the layout: // Register Extension
// file: e.g. index.php (somewhere near the engine definition)
$engine->loadExtension(new class implements ExtensionInterface {
private string $content = '';
public function register(Engine $engine)
{
$engine->registerFunction('addToSection', function($content) {
$this->content .= ' ' . $content;
});
$engine->registerFunction('getSection', function () {
return $this->content;
});
}
});
// Add some data
// file home.php
$this->addToSection(<<<EOD
<script>
alert("Hello World");
</script>
EOD);
// Output somewhere in the layout (or any other template)
// file: layout.php
echo $this->getSection(); |
@basteyy ohhh so that's why there are extensions like that! I wasn't getting the point of using something like it but now it's clear. Did any of you check my latest changes in the PR? It's actually looking good now, with clear APIs and automated tests. I really want some input to have this in the next Plates version as soon as possible. |
Hi guys, Since this issue is still open, here is my solution. Just a thought for you guys to have a look. In my situation, I have
And I have the following
So if I use My fix is Solution: In
The One disadvantage is adding variable(s) to the nested template. For example, in my User page, I have a variable called |
I spent another day on this issue and I believe that this is a design "issue". The "issue" happens at the plates/src/Template/Template.php Lines 158 to 188 in ec07462
I will continue using my example in the above comment here to explain my findings. When using the
My thought is adding the above line of code to the partial, then the partial has a layout name, and it could So to fix this "issue", I think we have to re-design the When I have a second thought of the
In this comment, I made an improvement of the solution. Make it not that dirty and fix the variable same name issue. The improved solution has two parts. 1, A new helper function. It will be used to replace the "insert/fetch" function. Here is the helper I wrote, a simplified version. You could add your logic in it.
2, Implement "loadDependencies" function in partial. In my example, on
Then in the parent page, the
I know this solution is not perfect, and if I have some better idea, I will make comment again (if this issue is still open, 😃). Conclusion: I like the A very nice project, great work. I will keep using it and thanks the author for sharing. 👍 |
Imagine de following file structure.
One
layout.phtml
:One
details.phtml
page:One
item.phtml
page:And finally
The push inside the fetched template (
item.phtml
) will never appear in the final$renderedView
string.The text was updated successfully, but these errors were encountered: