zombor / KOstache

Logic-less View/Mustache Module for Kohana v3
MIT License
138 stars 44 forks source link

Kohana_Layout renders the content partial twice #55

Open dolfelt opened 11 years ago

dolfelt commented 11 years ago

I was having difficulty getting some lambda functions working until I realized that the $m->setPartials() function takes unrendered content. The current code in Kohana_Layout renders the partial before it is set, then Mustache renders it again. Normally this wouldn't be a big deal, but if you attempt to change the delimiter, it matters. I ended up extending the Kohana_Layout and changing the render function.

zombor commented 11 years ago

I'm really lost about what the problem is here. :)

dolfelt commented 11 years ago

If you change your delimiter using {{=<% %>=}} so you can use {{ }} for your JS templates, it won't work within the content partial because the content is rendered twice. First time changes the delimiter and keeps the curly braces, and the second time removes those braces.

dolfelt commented 11 years ago

Place the following inside a content template (not the layout template):

{{=<% %>=}}
{{should_show}}
{{#also_show}}
    {{var}}
{{/also_show}}

The previous code example should render the mustache tags into the HTML sent to the browser, but that is not what is happening.

vukers commented 11 years ago

@dolfelt, I was facing a similar issue: I had a variable $foo:

public $foo = 'These {{brackets}} should appear.';

which I wanted to place in a page which used Kostache_Layout. Stepping through the code showed that it would render twice, causing the {{brackets}} portion to be stripped out.

I came to the solution of using:

{{=<% %>=}}{{ foo }}<%={{ }}=%>

which properly rendered the brackets.

I think the same solution can address your issue if you were to put your code section:

{{should_show}}
{{#also_show}}
    {{var}}
{{/also_show}}

within a variable, possibly even a partial.

zombor commented 11 years ago

Sounds like a bug in the mustache renderer? I know this used to work in mustache v1.

zsoerenm commented 11 years ago

Yes, Kostache_Layout::CONTENT_PARTIAL => parent::render($class, $template) is not needed as partials get rendered anyway. file_get_contents would do the job!

evanpurkhiser commented 11 years ago

@zsoerenm is right.

The Kostache_Layout::render() method actually renders two things. First the actual content of the page is rendered and then set as the content partial for the layout template. Then the layout is rendered. Since the content partial was already rendered and injected as a partial this opens up potential bugs like the one describe above.

The content partial should be set to the source mustache file, NOT the rendered mustache file.

zombor commented 11 years ago

We only have a filesystem partial loader. I'm busy for the next week, so if there's a patch you have, let's see it :)

polarina commented 11 years ago

Could the patch be applied anytime soon? I stumbled upon this bug too, shot me in the foot. I manually applied the patch provided by EvanPurkhiser and it works fine now. Thanks.