Skip to content

Template viewData not inherited  #75

@neznaika0

Description

@neznaika0

If I understood correctly, my example should have inherited data. But I only see the first ones.
I think there is a problem when using extend() and include().
Since the normal behavior is to transfer data internally to the final template

Examples
class Home extends AbstractController
{
    protected $data = [];
    public function index(): string
    {
        $this->data = ['foo' => 'FOO'];
        // 1.
        // return ''
        //  . view('/bug/view1', $this->data + ['bar' => 'BAR'])
        //  . view('/bug/view2', $this->data + ['baz' => 'BAZ'])
        //  . view('/bug/view3', $this->data + ['too' => 'TOO'])
        //  ;
        // 2.
        return ''
            . view_fragment('/bug/view1', ['fragment1'], $this->data + ['bar' => 'BAR'])
            . view_fragment('/bug/view2', ['fragment2'], $this->data + ['baz' => 'BAZ'])
            . view_fragment('/bug/view3', ['fragment3'], $this->data + ['too' => 'TOO'])
            . view_fragment('/bug/include', ['include'], $this->data + ['inc' => 'INC'])
            // . view('/bug/include', $this->data + ['inc' => 'INC'])
        ;
    }
}
// layout
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <?= $this->renderSection('content') ?>
    <?= $this->include('/bug/include') ?>
</body>
</html>
// view1
<?= $this->extend('/bug/layout') ?>
<?php $this->section('content') ?>
<div style="background-color: lightpink;width:200px;height: 130px;">
    <b>View 1</b><br>
    <b>foo: </b> <?= isset($foo) ? 'YES' : 'NO' ?><br>
    <b>bar: </b> <?= isset($bar) ? 'YES' : 'NO' ?><br>
    <b>baz: </b> <?= isset($baz) ? 'YES' : 'NO' ?><br>
    <b>too: </b> <?= isset($too) ? 'YES' : 'NO' ?><br>
    <b>inc: </b> <?= isset($inc) ? 'YES' : 'NO' ?><br>
</div>
<?php $this->endSection() ?>
<?php $this->fragment('fragment1') ?>
<div style="background-color: lightpink;width:200px;height: 130px;">
    <b>Fragment 1</b><br>
    <b>foo: </b> <?= isset($foo) ? 'YES' : 'NO' ?><br>
    <b>bar: </b> <?= isset($bar) ? 'YES' : 'NO' ?><br>
    <b>baz: </b> <?= isset($baz) ? 'YES' : 'NO' ?><br>
    <b>too: </b> <?= isset($too) ? 'YES' : 'NO' ?><br>
    <b>inc: </b> <?= isset($inc) ? 'YES' : 'NO' ?><br>
</div>
<?php $this->endFragment() ?>
// view2, view3 similar
<div style="background-color: lightgreen;width:200px;height: 130px;">
    <b>View 2</b><br>
    <b>foo: </b> <?= isset($foo) ? 'YES' : 'NO' ?><br>
    <b>bar: </b> <?= isset($bar) ? 'YES' : 'NO' ?><br>
    <b>baz: </b> <?= isset($baz) ? 'YES' : 'NO' ?><br>
    <b>too: </b> <?= isset($too) ? 'YES' : 'NO' ?><br>
    <b>inc: </b> <?= isset($inc) ? 'YES' : 'NO' ?><br>
</div>
<?php $this->fragment('fragment2') ?>
<div style="background-color: lightgreen;width:200px;height: 130px;">
    <b>Fragment 2</b><br>
    <b>foo: </b> <?= isset($foo) ? 'YES' : 'NO' ?><br>
    <b>bar: </b> <?= isset($bar) ? 'YES' : 'NO' ?><br>
    <b>baz: </b> <?= isset($baz) ? 'YES' : 'NO' ?><br>
    <b>too: </b> <?= isset($too) ? 'YES' : 'NO' ?><br>
    <b>inc: </b> <?= isset($inc) ? 'YES' : 'NO' ?><br>
</div>
<?php $this->endFragment() ?>
// include
<?php $this->fragment('include') ?>
<div style="background-color: lightpink;width:200px;height: 130px;">
    <b>Include</b><br>
    <b>foo: </b> <?= isset($foo) ? 'YES' : 'NO' ?><br>
    <b>bar: </b> <?= isset($bar) ? 'YES' : 'NO' ?><br>
    <b>baz: </b> <?= isset($baz) ? 'YES' : 'NO' ?><br>
    <b>too: </b> <?= isset($too) ? 'YES' : 'NO' ?><br>
    <b>inc: </b> <?= isset($inc) ? 'YES' : 'NO' ?><br>
</div>
<?php $this->endFragment() ?>

Variant 2, add ['inc' => 'INC'] data. No last baz, too, inc

View 1
foo: YES
bar: YES
baz: NO
too: NO
inc: NO
Include Frag
foo: YES
bar: YES
baz: NO
too: NO
inc: NO
Fragment 1
foo: YES
bar: YES
baz: NO
too: NO
inc: NO
Fragment 2
foo: YES
bar: YES
baz: YES
too: NO
inc: NO
Fragment 3
foo: YES
bar: YES
baz: YES
too: YES
inc: NO
Include Frag
foo: YES
bar: YES
baz: NO
too: NO
inc: NO

If uncomment last view(). baz, too, inc appears

// the same as above, but last 
Include Frag
foo: YES
bar: YES
baz: NO
too: NO
inc: NO
Include Frag
foo: YES
bar: YES
baz: YES
too: YES
inc: YES

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions