Comments (5)
I think I'd argue against a method called hasSection()
having the side-effect of echoing some content. But I wonder if something like this could work inside a layout:
<?php $this->startDefault('sidebar') ?>
<ul>
<li><a href="/link">Example Link</a></li>
<li><a href="/link">Example Link</a></li>
</ul>
<?php $this->stopDefault() ?>
The result would be that if sidebar
is defined it gets used and this default section gets thrown away, otherwise the default section is used. It would use output buffering just like start()
and stop()
.
from plates.
@jk3us I'm agree that hasSection
is not a great name, just not found a better one. Maybe printSection
, doSection
, useSection
?
Your approach can work for sure, but:
- content between
startDefault
andstopDefault
is always processed, even if a section is defined, and if it contains php code it may slow down page rendering and possibly have undesidered side effects. - To implement it, you need to store rendered default sections in a variable, having a sort of duplication of section array: it's an additional layer of unnecessary complexity that I really would avoid
I really think that if we find a good name my approach is ease to use and to implement and makes templates cleaner, otherwise the standard if / else approach is still the preferable way to go.
from plates.
This is something I fiddled around with quite a bit when first writing the section logic. I really like how Twig does this. In the end I settled with what you see in the documentation because it's very clear what's going on.
I sort of agree with @jk3us that having something randomly printed is kind of weird, although it certainly does solve this issue, and in the context of templating this might be acceptable, especially of the function name indicates that printing will occur.
The challenge here is coming up with a good function name, and to make things more complicated I would want it to be one word, since all other Plates functions are one word. I'm thinking that the function output()
might make the most sense:
<div id="sidebar">
<?php if (!$this->output('content')): ?>
<ul>
<li><a href="/link">Example Link</a></li>
<li><a href="/link">Example Link</a></li>
</ul>
<?php endif ?>
</div>
What do you think?
from plates.
output()
was the first name that I thought, the problem is that without any reference to sections it's hard to understand what's going on.
See your code, how can I understand that "content"
is a section name and not a variable, a function or something else?
I understand the one word policy but in this case I think that with current Plates features is not possible to respect it and at same time keep all readable...
The problem with the if / else way is more evident when using nested sections, that means nested if / else blocks: templates become almost unreadable.
Moreover, due to #47 is not possible to use partials to "compose" complex layouts improving readability.
This is the main reason why I gave up using Plates and I went back to Twig for current projects, but I'm writing my library for native PHP templates, and in my library template inheritance is handled in the same way used by Twig.
I'm writing a different library instead of contribute to Plates because to obtain that I'm using a completely different approach that had required a too big rewrite for Plates...
from plates.
Closing in favor #169.
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.