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
Inspired by @reinink plates and @symfony template slot #46
Conversation
|
I like the idea, but I think we can modify the implementation to use a closure. Instead of start() and end(), we might do something like this: And then in the template: But I still want to think about it a little. |
|
I am not sure why you prefer callable. Is there anything we get via callable ? may be we need to pass the data also here in that case. After a sleep what I have in mind is place the value in |
|
After reviewing again, it looks like Plates does sections by buffering input to a variable ... <?php $this->start('section_name') ?>
<h1>Welcome!</h1>
<p>Hello <?=$this->e($this->name)?></p>
<?php $this->end() ?>... and then echoing the variable: <?= $this->section_name ?>We can do the same thing using <?php
// first ...
$this->section_name = $this->render('section_name');
// later...
echo $this->section_name;
?>Does that seem sufficient? |
|
@pmjones I am not sure of the outcome though. The idea is in Can we make something like this ? Only render if $view_registry = $view->getViewRegistry();
$view_registry->set('layout', function () {
echo "This is the main content." . PHP_EOL;
echo $this->section('the_section_name');
});
$layout_registry = $view->getLayoutRegistry();
$layout_registry->set('wrapper', function () {
echo "Before the main content." . PHP_EOL;
echo $this->content;
echo "After the main content." . PHP_EOL;
$this->section()->the_section_name = $this->render('the_section_name');
})
$layout_registry->set('nosection', function () {
echo "No section content." . PHP_EOL;
})
$view->setView('main_content');
$view->setLayout('wrapper');
$output = $view(); |
In Plates, it looks like you do need to test if the variable is initialized. If you never start a section named "foo", and try to echo |
|
Tell you what -- how does this look as an addition to Aura.Html? <?php
namespace Aura\Html\Helper;
class Section extends AbstractHelper
{
protected $section = array();
protected $capture = array();
public function __invoke()
{
return $this;
}
public function __set($name, $spec)
{
$this->section[$name] = $spec;
}
public function __get($name)
{
return $this->section[$name];
}
public function __isset($name)
{
return isset($this->section[$name]);
}
public function __unset($name)
{
unset($this->section[$name]);
}
public function begin($name)
{
$this->capture[] = $name;
ob_start();
}
public function end()
{
$spec = ob_get_clean();
$name = array_pop($this->capture);
$this->__set($name, $spec);
}
}
// usage:
// set directly into section helper:
$this->section()->foo = $this->render('_foo');
// capture into section helper:
$this->section()->start('bar');
echo "bar bar bar";
$this->section()->end();
// either way, echo directly:
echo $this->section()->foo;
echo $this->section()->bar;
?> |
|
Hm. On further consideration I see that sections are not necessarily HTML specific, and might belong in the view after all. What about this in AbstractView? <?php
protected function beginSection($name)
{
$this->capture[] = $name;
ob_start();
}
protected function endSection()
{
$body = ob_get_clean();
$name = array_pop($this->capture);
$this->setSection($name, $body);
}
protected function setSection($name, $body)
{
$this->section[$name] = $body;
}
protected function getSection($name)
{
return $this->section[$name];
}
protected function hasSection($name)
{
return isset($this->section[$name]);
}
?>Usage: <?php
// usage:
// set directly:
$this->setSection('foo', $this->render('_foo'));
// capture into buffer
$this->beginSection('bar');
echo "bar bar bar";
$this->endSection();
// either way, echo directly:
echo $this->getSection('foo');
echo $this->getSection('bar');
?> |
|
@pmjones I like the last one. protected function getSection($name)
{
if ($this->hasSection($name)) {
return $this->section[$name];
}
return '';
} |
|
Why do we think it's important to return an empty string instead of checking with hasSection()? Avoiding an if() statement does not strike me as a benefit here. |
|
I don't need to write an if () in template :) . |
This is adding sections in a view. The section must be rendered before the used variable.