SnippetArea forcing components to render twice

Notice: This thread is very old.
repli2dev
Member | 57
+
0
-

Recently I have encountered a weird behaviour when using snippet-area macro in Nette 2.3 where I have some static control.

First I have component Foo:

class Foo extends \Nette\Application\UI\Control
{
    private static $count = 1;

    public function render()
    {
        $this->template->setFile(__DIR__ . '/foo.latte');
        $this->template->count = self::$count++;
        $this->template->render();
    }

    public function handleShow()
    {
        $this->redrawControl();
    }
}
{snippet}
    {$count}
{/snippet}

And I have template Homepage/default.latte

{* abridged *}
{snippetArea flashes-wrapper}
	{include ../flashes.latte}
{/snippetArea}
{control modal}
<a href="/?do=modal-show" class="ajax">open modal</a>
{* abridged *}

Where flashes.latte contains:

{snippet flashes}
    <div n:foreach="$flashes as $flash" n:class="flash, $flash->type">{$flash->message}</div>
{/snippet}

And BasePresenter.php

protected function beforeRender()
    {
        parent::beforeRender();
        $this->flashMessage(
            "You can not publish demands nor offers until you verify your email. We have sent email with verification link after your registration, please check your email box (sometimes it can end up in SPAM)"
        );
        if ($this->isAjax()) {
            $this->redrawControl('flashes-wrapper');
            $this->redrawControl('flashes');
        }
    }

Expected behaviour:

  1. Click on the ajax link
  2. Flash message should be added
  3. Component should be rerendered and show “1”

Actual result: the number is “2” when loaded via ajax, or “1” when loaded into new tab (without ajax).

Is this inconsistency known? Is it a bug?

UPDATE: when I inline the flashes.latte the problem disappears.

David Matějka
Moderator | 6445
+
0
-

It's possibly a bug. snippets has been rewritten in nette 2.4 so I think it should work now OK. but i'm afraid no one will fix it in 2.3 :)

repli2dev
Member | 57
+
0
-

Thanks for info.

I have tried in Nette 2.4 and in the exactly same situation the problem doesn't happen… However it is kind of sad that 2.3 is getting treatment as Cinderella :-)

David Matějka
Moderator | 6445
+
0
-

However it is kind of sad that 2.3 is getting treatment as Cinderella :-)

  • nette 2.3 has been released 2 years ago. you can't expect it will be maintained forever
  • it is very hard to debug snippets in nette <= 2.3. and sometimes it is almost impossible to fix that. that is why snippets has been rewritten.
Pavel Kravčík
Member | 1180
+
0
-

TL;DR: Better find new girl with bigger tits (2.4) than Ciderella. :)