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

Variables visibility between blocks depend on block order #178

Closed
milo opened this issue May 21, 2018 · 5 comments
Closed

Variables visibility between blocks depend on block order #178

milo opened this issue May 21, 2018 · 5 comments
Milestone

Comments

@milo
Copy link
Member

milo commented May 21, 2018

Version: master (f7b0fa8)

Following code emits notice: PHP Notice: Undefined variable: x

<?php
# composer require latte/latte dev-master
require __DIR__ . '/vendor/autoload.php';

$latte = '
{define a}
	{var x => "X"}
	{include #b}
{/}

{define b}
	{$x}
{/}

{include a}
';

$engine = new Latte\Engine;
$engine->setLoader(new Latte\Loaders\StringLoader);
echo $engine->compile($latte);
$engine->render($latte);

To make it work, I have to pass $x explicitly to block. Or, If I change blocks order, it works:

$latte = '
{define b}
	{$x}
{/}

{define a}
	{var x => "X"}
	{include #b}
{/}

{include a}
';

The only difference between compiled template is in method blockA():

# Non working
function blockA($_args)
{
        extract($_args);
        $x = "X";
        $this->renderBlock('b', $this->params, 'html');
}

# Working
function blockA($_args)
{
        extract($_args);
        $x = "X";
        $this->renderBlock('b', get_defined_vars(), 'html');
}

I'm not sure it is bug or by desing.

@matej21
Copy link
Contributor

matej21 commented May 21, 2018

it's related to #163

@dg dg added this to the v3.0 milestone Jul 16, 2019
@dg dg closed this as completed in f809bcb Nov 15, 2020
dg added a commit that referenced this issue Nov 15, 2020
dg added a commit that referenced this issue Nov 15, 2020
dg added a commit that referenced this issue Nov 15, 2020
dg added a commit that referenced this issue Nov 15, 2020
dg added a commit that referenced this issue Nov 15, 2020
dg added a commit that referenced this issue Nov 15, 2020
@milo
Copy link
Member Author

milo commented Nov 16, 2020

Thank you!

dg added a commit that referenced this issue Nov 21, 2020
dg added a commit that referenced this issue Nov 21, 2020
dg added a commit that referenced this issue Nov 21, 2020
dg added a commit that referenced this issue Nov 21, 2020
dg added a commit that referenced this issue Nov 21, 2020
dg added a commit that referenced this issue Nov 21, 2020
dg added a commit that referenced this issue Nov 21, 2020
dg added a commit that referenced this issue Nov 21, 2020
dg added a commit that referenced this issue Nov 22, 2020
dg added a commit that referenced this issue Nov 22, 2020
dg added a commit that referenced this issue Nov 22, 2020
dg added a commit that referenced this issue Nov 22, 2020
dg added a commit that referenced this issue Nov 22, 2020
dg added a commit that referenced this issue Nov 22, 2020
dg added a commit that referenced this issue Nov 22, 2020
dg added a commit that referenced this issue Nov 22, 2020
dg added a commit that referenced this issue Nov 22, 2020
dg added a commit that referenced this issue Nov 22, 2020
dg added a commit that referenced this issue Nov 22, 2020
dg added a commit that referenced this issue Nov 22, 2020
dg added a commit that referenced this issue Nov 22, 2020
dg added a commit that referenced this issue Nov 23, 2020
dg added a commit that referenced this issue Nov 23, 2020
dg added a commit that referenced this issue Nov 24, 2020
@solcik
Copy link

solcik commented Nov 27, 2020

@dg Hello, this template was ok till v2.9.0, but with v2.9.1 it is throwing a warning, that variable $T is not defined.

When I skip the warning, the translator is working just fine.
This use-case is also mentioned in the docs: https://latte.nette.org/en/template-inheritance

Data outside of a blocks in a child template is executed before the layout template is rendered, thus you can use it to define variables like {var $foo = bar} and propagate data to the whole inheritance chain:

Also mentioned in: #163 (comment)

{templateType HomepageTemplate}
{var $T = 'app.' . HomepagePresenter::class}

{block h1}
    {_"$T.heading.h1"}
{/block}

{block content}
    <div></div>
{/block}

@filipmelik
Copy link

@solcik I had very similar code to yours and this stopped working in v2.9.1:

Template code:

{var $sanitizedEmployeeDisplayName = "TEST TEST"}

{block title}
    {_messages.pageTitle.job-detail} {$sanitizedEmployeeDisplayName}
{/block}

{block content}

    <div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
        <h1 class="h2">
           {$sanitizedEmployeeDisplayName} - {_messages.labour.job.detail.title}
        </h1>
    </div>
{/block}

@layout.latte (template base) - simplified example:

<!doctype html>
<html lang="en" class="js">
<head>
    <title>{block title|stripHtml}MEOW{/block}</title>
</head>

<body>
    {include #content}
</body>
</html>

When i changed @layout.latte html body tag content to from {include #content} to {block content} it started to work again. I guess it now "replaces" the body tag including its context from the "child" template and thus it works.

@dg
Copy link
Member

dg commented Nov 30, 2020

@solcik @filipmelik fixed in 2.9-dev

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

5 participants