-
Notifications
You must be signed in to change notification settings - Fork 179
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #192 from thephpleague/183-default-layout-ext
Default Layouts #183
- Loading branch information
Showing
11 changed files
with
87 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
src/Extension/LayoutSections/DefaultLayoutRenderTemplate.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
namespace League\Plates\Extension\LayoutSections; | ||
|
||
use League\Plates; | ||
|
||
final class DefaultLayoutRenderTemplate extends Plates\RenderTemplate\RenderTemplateDecorator | ||
{ | ||
private $layout_path; | ||
|
||
public function __construct(Plates\RenderTemplate $render, $layout_path) { | ||
parent::__construct($render); | ||
$this->layout_path = $layout_path; | ||
} | ||
|
||
public function renderTemplate(Plates\Template $template, Plates\RenderTemplate $rt = null) { | ||
$ref = $template->reference; | ||
$contents = $this->render->renderTemplate($template, $rt ?: $this); | ||
|
||
if ($ref()->get('layout') || $ref()->get('is_default_layout')) { | ||
return $contents; | ||
} | ||
|
||
$layout = $ref()->fork($this->layout_path, [], ['is_default_layout' => true]); | ||
$ref()->with('layout', $layout->reference); | ||
|
||
return $contents; | ||
} | ||
|
||
public static function factory($layout_path) { | ||
return function(Plates\RenderTemplate $rt) use ($layout_path) { | ||
return new self($rt, $layout_path); | ||
}; | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<div> | ||
<?=$v->section('content')?> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<div>main</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
use League\Plates; | ||
|
||
describe('Layout Sections Extension', function() { | ||
it('allows default layout', function() { | ||
$plates = new Plates\Engine([ | ||
'base_dir' => __DIR__ . '/fixtures/default-layout', | ||
'default_layout_path' => './_layout', | ||
]); | ||
|
||
$html = <<<html | ||
<div> | ||
<div>main</div> | ||
</div> | ||
html; | ||
expect($plates->render('main'))->equal($html); | ||
}); | ||
}); |