Comments (3)
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.
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.
Closing in favor of #169 Thanks!
from plates.
Related Issues (20)
- ResolveTemplatePath not exist ?
- Replace 'callback' type with 'callable' in PhpDoc HOT 1
- Don't seem to be able to install last version using composer HOT 1
- Website blocked in India HOT 1
- Whats holding 3.5v release? HOT 1
- Any hope of documented Themes functionality being release? If not, recommended workaround? HOT 3
- Data documentation outdated
- I have cloned a php project from git. The project is using platesphp. But i am not able to run my branch in localhost. HOT 1
- league/plates v4.0.0-alpha to v3.4.0 create error HOT 3
- Atualização HOT 1
- Release 3.5 with PHP 8.2 compatibility and theme support? HOT 2
- Variables from the rendering process leak into templates
- Bug when pushing to sections inside of fetched templates HOT 8
- Documentation "Engine -> Accessing the engine and template"
- Does it support custom template tags? HOT 2
- How to properly use asset extension? HOT 2
- I can't use League\Plates\Extension\Asset;
- Expected behaviour for multiple sections build over multiple layouts HOT 1
- Deprecated dynamic property in extensions HOT 3
- Passing data to insert() inside layout() HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from plates.