Snippet does not invalidate on AJAX Request if you put it in included latte file

5 years ago

Member | 2

Hi there!

I guess i found a bug or maybe i miss here something. But thing is that if you structure you template into several files and use {include ‘filename.latte’} latte macros for including additional files into your template, than you will expierence problem with your snippets. They wont invalidate on AJAX requests.

Lets say you have:


<!DOCTYPE html>
    <head> ... </head>
        {include header.latte}
        {include main.latte}
        {include footer.latte}

        {block scripts}
        <script src="{$basePath}/js/vendor/jquery.js"></script>
        <script src="{$basePath}/js/vendor/netteForms.js"></script>
        <script src="{$basePath}/js/vendor/nette.ajax.js"></script>


{form subscribeForm class => ajax}
    {snippet errors}
    <div n:if="$control[subscribeForm]->ownErrors">
        <p n:foreach="$control[subscribeForm]->ownErrors as $error">{$error}</p>
    {label email /}
    {input email}
    {input send}


protected function createComponentSubscribeForm()
    $form = new Form;
    $form->addText('email', 'E-mail:')->addRule(Form::EMAIL, 'Please enter your e-mail.');
    $form->addSubmit('send', 'Submit');
    $form->onSuccess[] = $this->subscribeFormSuccess;
    return $form;

public function subscribeFormSuccess($form)
    if ($form->isSubmitted() && $form->isValid())
        $values = $form->getValues();

        $this->database->table('emails')->insert(array('email' => $values->email));
        $form->addError('E-mail address is incorrect!');

    if ($this->isAjax()) $this->redrawControl('errors');
    else $this->redirect('this');

After AJAX request you end up with this response:


Once you put the contents of the main.latte into @layout.latte directly it will work correctly and you get snippet update:

{"state":[],"snippets":{"snippet--errors":"\t\t\t\t<div>\n\t\t\t\t\t<p>E-mail address is incorrect!<\/p>\n\t\t\t\t<\/div>\n"}}

So i guess this is a bug or it has to do something with variable scope or i don't know. But i just came here to let you know. Maybe you experienced similar problem and didn't know how to solve it.


5 years ago

Member | 1219

I Think You cannot use snippet in a form. You have to wrap The form by snippet.

5 years ago

Member | 2

Well i made a quick test. I wrapped the form into the snippet and it didn't work either. Maybe i have to pass some parameters into the main.latte template like this:

{include main.latte param=$param, ...}

But i am not sure what parameters to put there.