8000 Add the ability to append content to sections. by reinink · Pull Request #138 · thephpleague/plates · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add the ability to append content to sections. #138

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 28, 2016
Merged

Conversation

reinink
Copy link
Contributor
@reinink reinink commented Dec 27, 2016

Closes #135.

@reinink reinink force-pushed the add_append_feature branch 2 times, most recently from 67b248e to b5a7880 Compare December 27, 2016 19:57
if (isset($this->sections[$name])) {
$content = $this->sections[$name];
unset($this->sections[$name]);
$this->sections[$name] = $content;
Copy link
Contributor
@ragboyjr ragboyjr Dec 27, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@reinink So, are you implementing a bug fix with this feature as well?

Copy link
Contributor
@ragboyjr ragboyjr Dec 27, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like this code is forcing the current section to always be the last item in the sections array so that end will refer to that started section. Maybe a comment would helpful :).

The only thing this doesn't take into account is what happens when someone defines another section while defining a section. It seems like that behavior is unwanted according to #75. But if someone accidentally ran another section before stopping another section, they'll get bizarre unexpected results.

For instance If a user forgot to stop a started section and then started a new section, they'd encounter unexpected behavior, and it might be hard for them to debug why their sections are all messed up. This might occur in a very large template file where there could be many sections to add/update.

Part of me thinks we should throw a logic exception if someone tries to call start before stopping a previously started section. The reason I bring this up here is that, we might need to alter the above logic if we do that start check.

I'm thinking something along the lines of this:

<?php

class Templa
8000
te 
{
    private $current_section;

    public function start($name) 
    {
        if ($this->current_section) {
            throw new LogicException('Cannot nest sections...');
        }

        $this->current_section = $name;

        if (!isset($this->sections[$name])) {
            $this->sections[$name] = '';
        }

        ob_start();
    }

    public function stop() 
    {
        if (!$this->current_section) {
            throw new LogicException('No section has been started...');
        }

        $this->sections[$this->current_section] = ob_get_clean();
    }
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You got it. And yes, just using a template parameter is a better solution. I'll update the PR.

@ragboyjr
Copy link
Contributor

Ah looks great! 👍

@reinink reinink force-pushed the add_append_feature branch 2 times, most recently from e9044af to 4cd2300 Compare December 27, 2016 22:23
@reinink reinink merged commit b1684b6 into master Dec 28, 2016
@reinink reinink deleted the add_append_feature branch December 28, 2016 00:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
0