cannot use snippets in component form
- dawoyan
- Member | 3
Hello
I have a component form. I have initiated it in presenter, then put it into template (default.latte) everything works file until I use snippet in template (or even use {block #content})
it throws exception saying Undefined variable: basePath
…\app\components\BaseComponents\BaseForm.php
$template = $this->getTemplate();
250: foreach ($this->formatTemplateFiles($file) as $path) {
251: if (is_file($path)) {
252: $template->setFile($path);
253: break;
254: }
255: }
256: $template->presenter = $template->_presenter = $this->presenter;
257: $template->control = $template->_control = $this->parent;
258: $template->form = $this->name;
259: $template->render();
260: }
261:
seems it tries to re-render the global layout in these sections.
what is wrong here?
- Vojtěch Dobeš
- Gold Partner | 1316
What does your BaseForm.php
inherits from? The best practice is
to extend Nette\Application\UI\Control
, because it already comes
with fully equiped template.
- Vojtěch Dobeš
- Gold Partner | 1316
Can I just ask what is the application you are working on? In private way if neccessary :).
Yeah – it is much better to extend UI\Control
and define form
as its subcomponent via standard createComponentForm
factory
method. Whole control can have template like {control form}
then.
Or use manual rendering or whatever. But it will support snippets etc.
This is minimal “BaseForm”, that fully supports snippets:
class BaseFormControl extends UI\Control
{
protected function template() {}
public function render()
{
$args = func_get_args();
$path = __DIR__ . '/' . ClassType::from($this)->getShortName() . '.latte';
if (!is_file($path)) {
return call_user_func_array(array($this['form'], 'render'), $args);
}
$template = $this->template;
$template->_form = $template->form = $this['form']; // allows snippets anywhere!
$template->setFile($path); // automatické nastavení šablony
array_unshift($args, $template);
call_user_func_array(array($this, 'template'), $args); // inherit and override 'template' to configure it
$template->render();
}
}
The algorithm determining the path to template is wrong, but the rest is some useful goods.