cannot use snippets in component form

7 years ago

dawoyan
Member | 3
+
0
-

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?

7 years ago

Vojtěch Dobeš
Member | 1317
+
0
-

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.

7 years ago

dawoyan
Member | 3
+
0
-

it extends an ExtendedForm which is inherited from Nette\Application\UI\Form.
so the problem is this form's inheritance and not presenters and templates? It is also a clue. Will try finding the problem in ExtendedForm, and will post the result if I find.

thanks for reply

7 years ago

Vojtěch Dobeš
Member | 1317
+
0
-

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.

7 years ago

dawoyan
Member | 3
+
0
-

this render method seems to be useful, I can overwrite the ExtendedForm's one in this particular case.

thank you very much