Latte {ifset #block} breaking whole layout down
- Pavel Janda
- Member | 977
Hello,
I found interesting bug in Latte:
- Download Nette Sandbox
- Edit @layout file to look like this:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body style="border: 5px solid pink;">
{ifset sidebar}
{include content}
{include sidebar}
{else}
{include content}
{/ifset}
</body>
</html>
- Edit Homepage/default.latte to look like this:
{block content}
Hello
{/block}
What is the result? Pink-bordered site content, good.
- Edit Homepage/default.latte to look like this:
{block sidebar}
Sidebar
{/block}
{block content}
Hello
{/block}
What is the result? Pink-bordered site content with sidebar, good.
- Edit Homepage/default.latte to look like this:
{if TRUE}
{block sidebar}
Sidebar
{/block}
{/if}
{block content}
Hello
{/block}
What is the result? Pink-bordered site content with sidebar, good.
- Edit Homepage/default.latte to look like this:
{if FALSE}
{block sidebar}
Sidebar
{/block}
{/if}
{block content}
Hello
{/block}
What is the result? “Hello” without even rendering @layout.latte (Can not see any pink border, right?). Whole site is broken.
Last edited by Beton (2015-03-19 16:04)
- David Grudl
- Nette Core | 8218
Template is not programming language, so block is defined when it exists in template, regardless of its context.
- Pavel Janda
- Member | 977
One way or other, that behaviour seems at least questionable.
Template is not programming language and yet we have IF and ELSE in it..
It would be really nice to have an opportunity to put {block} into template depending on some condition. :)
- Jan Tvrdík
- Nette guru | 2595
@Beton Templates are like classes and blocks are like methods. Most programming languages does not allow you to write sth like:
class HomepageDefaultTemplate
{
if (FALSE) {
public function blockSidebar()
{
...
}
}
}
- Pavel Janda
- Member | 977
Jan Tvrdík wrote:
@Beton Templates are like classes and blocks are like methods. Most programming languages does not allow you to write sth like:
class HomepageDefaultTemplate { if (FALSE) { public function blockSidebar() { ... } } }
Well, not by default. But you can make them act like they do. For instance, Nette provides Nette\Object::extensionMethod :)
But here i just quibble.
I thought i just quibble, but when i look deeper into this problem, i see exactly behaviour of extensionMethod. If i define block under particular condition, it depends on the result if it will be stored is some magical static array or not.
And if the block pops up when iterating through that static array, it is the right time to behave like it is defined.