Skip to content

Commit

Permalink
New appends Blade directive.
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Nov 7, 2013
1 parent c546c9a commit f0ae98b
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/Illuminate/View/Compilers/BladeCompiler.php
Expand Up @@ -33,6 +33,7 @@ class BladeCompiler extends Compiler implements CompilerInterface {
'Language',
'SectionStart',
'SectionStop',
'SectionAppend',
'SectionOverwrite',
);

Expand Down Expand Up @@ -398,6 +399,19 @@ protected function compileSectionStop($value)
return preg_replace($pattern, '$1<?php $__env->stopSection(); ?>$2', $value);
}

/**
* Compile Blade section append statements into valid PHP.
*
* @param string $value
* @return string
*/
protected function compileSectionAppend($value)
{
$pattern = $this->createPlainMatcher('append');

return preg_replace($pattern, '$1<?php $__env->appendSection(); ?>$2', $value);
}

/**
* Compile Blade section stop statements into valid PHP.
*
Expand Down
21 changes: 21 additions & 0 deletions src/Illuminate/View/Environment.php
Expand Up @@ -497,6 +497,27 @@ public function stopSection($overwrite = false)
return $last;
}

/**
* Stop injecting content into a section and append it.
*
* @return string
*/
public function appendSection()
{
$last = array_pop($this->sectionStack);

if (isset($this->sections[$last]))
{
$this->sections[$last] .= ob_get_clean();
}
else
{
$this->sections[$last] = ob_get_clean();
}

return $last;
}

/**
* Append content to a given section.
*
Expand Down
7 changes: 7 additions & 0 deletions tests/View/ViewBladeCompilerTest.php
Expand Up @@ -296,6 +296,13 @@ public function testStopSectionsAreCompiled()
}


public function testAppendSectionsAreCompiled()
{
$compiler = new BladeCompiler($this->getFiles(), __DIR__);
$this->assertEquals('<?php $__env->appendSection(); ?>', $compiler->compileString('@append'));
}


public function testCustomExtensionsAreCompiled()
{
$compiler = new BladeCompiler($this->getFiles(), __DIR__);
Expand Down
13 changes: 13 additions & 0 deletions tests/View/ViewEnvironmentTest.php
Expand Up @@ -237,6 +237,19 @@ public function testSectionExtending()
}


public function testSessionAppending()
{
$environment = $this->getEnvironment();
$environment->startSection('foo');
echo 'hi';
$environment->appendSection();
$environment->startSection('foo');
echo 'there';
$environment->appendSection();
$this->assertEquals('hithere', $environment->yieldContent('foo'));
}


public function testYieldSectionStopsAndYields()
{
$environment = $this->getEnvironment();
Expand Down

0 comments on commit f0ae98b

Please sign in to comment.