Giter VIP home page Giter VIP logo

Comments (3)

reinink avatar reinink commented on May 22, 2024

Hah, sometimes I feel like I'm going in circles. Plates used to work in this way, but many folks found it confusing that nested templates automatically received all other template variables, which is why they became isolated like they are now. Albeit, you're not asking for that, but rather that the sections be shared. I'll need to think about this one a little more, because I believe this would be a backwards breaking change.

One question, have you tried using actual layouts instead? Layouts in Plates do share all sections, and you can even stack layouts for more complex configurations. I believe it would do exactly what you're looking for.

from plates.

gmazzap avatar gmazzap commented on May 22, 2024

Yes, I use layouts, I use them layout a lot, but I think you didn't understand the problem or, more probably, I didn't explain it well.

First of all let me say that this issue is completely different from partial-specific data VS shared data between partial and templates, because you can pass partial-specific data to partial, but you can't pass partial-specific sections.

In Plates, when using partial, is not possible to use sections at all: start() and stop() methods have no effects in partials, because isn't possible override a section that is defined in a partial.

Let me try to better explain this.

Have a look to this stacked layouts code:

template.php

<html>
<head>
    <title><?=$this->e($title)?></title>
</head>
<body>

    <div id="wrapper">

        <div id="main">
             <?= $this->section('main', 'Welcome to home page') ?>
        </div>

        <div id="sidebar">
             <?= $this->section('sidebar', $this->fetch('default-sidebar') ?>
        </div>

    </div>
</body>
</html>

blog.php

<?php $this->layout('template') ?>

<?php $this->start('main') ?>

    <header>
        <?php if ($this->section('header')): ?>
            <?= $this->section('header') ?>
        <?php else: ?>
             <h1>My Blog</h1>
             <p><em>An awesome blog you will like.</em></p>
        <?php endif ?>
    <header>

    <?php if ($this->section('content')) : ?>
            <?= $this->section('content') ?>
    <?php else : ?>
            <ul id="posts">
                 <?php foreach($posts as $post) : ?>
                     <li><a href="<?= $post->link ?>"><?= $this->e($post->title) ?></a></li>
                  <?php endforeach ?>
            </ul>
    <?php endif ?>

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

article.php

<?php $this->layout('blog') ?>

<?php $this->start('header') ?>
    <h1><?= $this->e($post->title) ?></h1>
<?php $this->stop() ?>

<?php $this->start('content') ?>
<article>
    <?=  $this->e($post->content) ?>
    <footer><?=  $this->e($post->date) ?></footer>
</article>
<?php $this->stop() ?>

Here I used stacked layouts with different sections (as seen in Twig or other modern engines) instead of the unique content section that IMHO is a very limited way to implement template inheritance.

As example, in code above, I can create a article-with-slideshow.php template by overriding only the header section, without touching the content section. And please note that this is a very trivial example: create complex views using stacked layouts with only the content section is something impossible. Or is possible with a lot of copy and paste, that is something that should be avoided...

However, in Plates code above just works, and that's awesome.

But now, let's assume I want to create another template: article-adv.php that is identical to article.php but it has an adv frame somewhere in the sidebar.

Going up to layout stack I see that I can override 'sidebar' section with a custom one, e.g.

article-adv.php

<?php $this->layout('article') ?>

<?php $this->start('sidebar') ?>
<?= $this->fetch('adv-sidebar') ?>
<?php $this->stop() ?>

Fine. However, adv-sidebar.php will be just a copy and paste of default-sidebar.php with just a very little modification. But isn't template inheritance all about write DRY templates code?

Having to copy and paste a template to modify only a div is something that a modern template engine should avoid, IMHO.

Now lets assume that default-sidebar.php somewhere contains

<?= $this->section('side-adv', '') ?>

In the file article-adv.php I could just define the section 'side-adv' to add the adv code, but that is not possible in Plates, because any section defined in a partial can't be overridden.

The only way to do that in Plates is to put the default sidebar code directly inside the main template file, instead of in the default-sidebar.php partial.

This means that in Plates you can't use partials and sections togheter that is something allowed by all the modern template engines and, to me, make a lot of sense, as I tried to explain here.

Sorry for the book-sized issue, at least I hope that my point is clear now.

from plates.

ragboyjr avatar ragboyjr commented on May 22, 2024

Closing in favor of #169 Thanks!

from plates.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.