Add the ability to append content to sections. #138

Merged
merged 1 commit into from Dec 28, 2016

Projects

None yet

2 participants

@reinink
Member
reinink commented Dec 27, 2016

Closes #135.

src/Template/Template.php
+ if (isset($this->sections[$name])) {
+ $content = $this->sections[$name];
+ unset($this->sections[$name]);
+ $this->sections[$name] = $content;
@ragboyjr
ragboyjr Dec 27, 2016 edited Contributor

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

@ragboyjr
ragboyjr Dec 27, 2016 edited Contributor

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 Template 
{
    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();
    }
}
@reinink
reinink Dec 27, 2016 Member

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

@ragboyjr
Contributor

Ah looks great! 👍

@reinink reinink Add the ability to append content to sections.
Closes #135.
576b6ce
@reinink reinink merged commit b1684b6 into master Dec 28, 2016

3 checks passed

Scrutinizer 3 updated code elements
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
continuous-integration/travis-ci/push The Travis CI build passed
Details
@reinink reinink deleted the add_append_feature branch Dec 28, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment