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

supply() template method does not work when same engine instance is used to render more templates #31

Closed
brad-jones opened this issue Feb 19, 2016 · 8 comments
Assignees
Labels
Milestone

Comments

@brad-jones
Copy link
Contributor

As per the title, here is my use case.

/views/layouts/master.php

<!DOCTYPE html>
<html>
    <head>
        <title><?= $T->v('title', 'Foo Bar') ?></title>
    </head>
    <body>
        <main><?= $T->supply('main') ?></main>
    </body>
</html>

/views/default.php

<?php $T->layout('layouts/master') ?>

<?php $T->section('main') ?>

    <h1>Something</h1>

<?php $T->stop() ?>

This next bit is overly simplified, I am running a PHP-DI container inside a Wordpress theme with a PSR-7 router. But none of that should matter (I don't think).

$engine = Foil::boot(['folders' => ['/views'], 'alias'   => 'T'])->engine();
$html = $engine->render('default');

The Result:

<!DOCTYPE html>
<html>
    <head>
        <title>Foo Bar</title>
    </head>
    <body>
        <main></main>
    </body>
</html>

Expected Result:

<!DOCTYPE html>
<html>
    <head>
        <title>Foo Bar</title>
    </head>
    <body>
        <main><h1>Something</h1></main>
    </body>
</html>

If I modify the master layout to look like this, I get the expected result:

<!DOCTYPE html>
<html>
    <head>
        <title><?= $T->v('title', 'Foo Bar') ?></title>
    </head>
    <body>
        <main>
            <?php $T->section('main') ?>
            <?php $T->stop() ?>
        </main>
    </body>
</html>

The issue appears to be here:
https://github.com/FoilPHP/Foil/blob/master/src/Template/Template.php#L172

$this->sections is empty. I have tried to follow how the sections are defined and added to the stack but between dependency injection, events, magic methods & the fact it's now 20mins past midnight. I got confused rather quickly. I will have another look in the morning with fresh eyes.

All the same the supply method certainly doesn't appear to work or at least it doesn't work exactly like the Laravel Blade @yield syntax. Maybe I am missing something obvious??? I'll probably discover my error the second I hit the "Submit new issue" button.

@gmazzap gmazzap self-assigned this Feb 19, 2016
@gmazzap
Copy link
Contributor

gmazzap commented Feb 19, 2016

Hi @brad-jones and thanks for your interest in Foil!

Sorry, but I tested your exact example and supply() works as expected.

I think your problem is in <?php $T->layout('layouts/master') ?>

Using layouts/master and having instantiated the engine with 'folders' => ['/views'], Foil will search for views/layouts/master.php, are you sure that is the right path?

@gmazzap gmazzap removed the invalid label Feb 19, 2016
@gmazzap
Copy link
Contributor

gmazzap commented Feb 19, 2016

I created a Gist with all the files I used to test this issue https://gist.github.com/Giuseppe-Mazzapica/6e3ae0aeb8e9fce54818 You can download it, and after composer install you can see that the rendered content is what expected...

@brad-jones
Copy link
Contributor Author

Hi @Giuseppe-Mazzapica,

Ok I got to the bottom of it, I knew I wasn't crazy.
Have a look at: https://gist.github.com/brad-jones/6279ce76cbedc2584675

Let me know what you think. For now my quick fix is to make sure the Foil engine is newed up each time it is requested from my DI container.

Cheers Brad

@gmazzap gmazzap changed the title Supply Shortcut does not Work supply() template method does not work when same engineinstance is used to render more templates Feb 22, 2016
@gmazzap gmazzap changed the title supply() template method does not work when same engineinstance is used to render more templates supply() template method does not work when same engine instance is used to render more templates Feb 22, 2016
@gmazzap gmazzap added the bug label Feb 22, 2016
@gmazzap gmazzap added this to the 0.6.4 milestone Feb 22, 2016
@gmazzap
Copy link
Contributor

gmazzap commented Feb 22, 2016

@brad-jones Ok, I see the issue. This is something that was noted already and fixed when sections are rendered using template inheritance (see #25), but was not fixed when sections are used with supply().

I'll fix this as soon as possible.

gmazzap added a commit that referenced this issue Feb 22, 2016
See #31

Template have to get sections via section factory, that way after a
rendering they are reset.
@gmazzap
Copy link
Contributor

gmazzap commented Feb 22, 2016

@brad-jones it should be fixed now.

Can you please require Foil from dev-master and give it a try? If it works I'll release 0.6.4 including this change.

I ran the test in your Gist (https://gist.github.com/brad-jones/6279ce76cbedc2584675) replacing

"require": {
    "foil/foil": "*"
 }

with

"require": {
    "foil/foil": "dev-master"
}

in composer.json and it works, but some tests in real world project would be nice.

Thanks for reporting!

@brad-jones
Copy link
Contributor Author

@Giuseppe-Mazzapica mate you are a legend!

I just got into the office, I will get back to you in the next few hours with some real world testing in my wordpress theme.

@brad-jones
Copy link
Contributor Author

@Giuseppe-Mazzapica feel free to release v0.6.4.

Seems to be working fine for me

Thanks again.

@gmazzap
Copy link
Contributor

gmazzap commented Feb 22, 2016

@brad-jones thanks a lot for reporting and testing.

@gmazzap gmazzap closed this as completed Feb 22, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants