Skip to content

Commit

Permalink
Merge pull request #192 from thephpleague/183-default-layout-ext
Browse files Browse the repository at this point in the history
Default Layouts #183
  • Loading branch information
ragboyjr committed Jan 8, 2018
2 parents d63e524 + d95cbb8 commit 17868f2
Show file tree
Hide file tree
Showing 11 changed files with 87 additions and 11 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
"peridot-php/leo": "^1.6",
"peridot-php/peridot": "^1.19",
"peridot-php/peridot-code-coverage-reporters": "^3.0",
"squizlabs/php_codesniffer": "~1.5"
"squizlabs/php_codesniffer": "~1.5",
"symfony/var-dumper": "^4.0"
},
"autoload": {
"psr-4": {
Expand Down
6 changes: 4 additions & 2 deletions src/Engine.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public function __construct($config = []) {
$this->container = new Util\Container();

$this->container->add('engine_methods', []);
$this->container->add('config', array_merge([
$this->container->add('config', [
'render_context_var_name' => 'v',
'ext' => 'phtml',
'base_dir' => null,
Expand All @@ -20,7 +20,7 @@ public function __construct($config = []) {
'validate_paths' => true,
'php_extensions' => ['php', 'phtml'],
'image_extensions' => ['png', 'jpg'],
], $config));
]);
$this->container->add('compose', function($c) {
return Util\id();
});
Expand Down Expand Up @@ -63,6 +63,8 @@ public function __construct($config = []) {
$this->register(new Extension\RenderContext\RenderContextExtension());
$this->register(new Extension\LayoutSections\LayoutSectionsExtension());
$this->register(new Extension\Folders\FoldersExtension());

$this->addConfig($config);
}

/** @return string */
Expand Down
37 changes: 37 additions & 0 deletions src/Extension/LayoutSections/DefaultLayoutRenderTemplate.php
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);
};
}
}


6 changes: 6 additions & 0 deletions src/Extension/LayoutSections/LayoutRenderTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ public function renderTemplate(Plates\Template $template, Plates\RenderTemplate

return ($rt ?: $this)->renderTemplate($layout);
}

public static function factory() {
return function(Plates\RenderTemplate $render) {
return new static($render);
};
}
}


7 changes: 6 additions & 1 deletion src/Extension/LayoutSections/LayoutSectionsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,15 @@ final class LayoutSectionsExtension implements Plates\Extension
public function register(Plates\Engine $plates) {
$c = $plates->getContainer();

$c->merge('config', ['default_layout_path' => null]);
$c->wrap('compose', function($compose, $c) {
return Plates\Util\compose($compose, sectionsCompose());
});
$c->wrap('renderTemplate.factories', function($factories) {
$c->wrap('renderTemplate.factories', function($factories, $c) {
$default_layout_path = $c->get('config')['default_layout_path'];
if ($default_layout_path) {
$factories[] = DefaultLayoutRenderTemplate::factory($default_layout_path);
}
$factories[] = LayoutRenderTemplate::factory();
return $factories;
});
Expand Down
3 changes: 2 additions & 1 deletion src/Extension/RenderContext/RenderContextExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ public function register(Plates\Engine $plates) {
'plates' => Plates\Util\stackGroup([
aliasNameFunc($c->get('renderContext.func.aliases')),
splitByNameFunc($c->get('renderContext.func.funcs'))
])
]),
'notFound' => notFoundFunc(),
];
});
$c->add('renderContext.func.aliases', [
Expand Down
6 changes: 6 additions & 0 deletions src/Extension/RenderContext/func.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,3 +188,9 @@ function splitByNameFunc(array $handlers) {
return $next($args);
};
}

function notFoundFunc() {
return function(FuncArgs $args) {
throw new FuncException('The function ' . $args->func_name . ' does not exist.');
};
}
6 changes: 0 additions & 6 deletions src/RenderTemplate/RenderTemplateDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,4 @@ public function __construct(Plates\RenderTemplate $render) {
}

abstract public function renderTemplate(Plates\Template $template, Plates\RenderTemplate $rt = null);

public static function factory() {
return function(Plates\RenderTemplate $render) {
return new static($render);
};
}
}
3 changes: 3 additions & 0 deletions test/integration/fixtures/default-layout/_layout.phtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div>
<?=$v->section('content')?>
</div>
1 change: 1 addition & 0 deletions test/integration/fixtures/default-layout/main.phtml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div>main</div>
20 changes: 20 additions & 0 deletions test/integration/layout-sections.spec.php
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);
});
});

0 comments on commit 17868f2

Please sign in to comment.