Giter VIP home page Giter VIP logo

Comments (5)

jk3us avatar jk3us commented on May 22, 2024

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.

gmazzap avatar gmazzap commented on May 22, 2024

@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 and stopDefault 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.

reinink avatar reinink commented on May 22, 2024

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.

gmazzap avatar gmazzap commented on May 22, 2024

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.

ragboyjr avatar ragboyjr commented on May 22, 2024

Closing in favor #169.

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.