whoward / cadenza

parser and renderer library for liquid-like templates
13 stars 6 forks source link

{{ super }} within nested blocks #17

Closed joefiorini closed 11 years ago

joefiorini commented 11 years ago

Hey there,

I'm finally getting around to setting up a slightly advanced layout in Cadenza. Calling {{super}} isn't working quite as I'm expecting. I've created a failing test case and pushed it to my fork of this project on the nested-super branch.

Here's my setup:

layout.html.cadenza

{% block layout %}
  {% block header %}
    <header></header>
  {% endblock %}
  {% block content %}{% endblock %}
{% endblock %}

super.html.cadenza

{% extends "nested_blocks/super-layout.html.cadenza" %}

{% block layout %}
  <section id="container">
  {% block header %}
    {{ super }}
  {% endblock %}
  {% block content %}
    <section id="content"></section>
  {% endblock %}
{% endblock %}

super.htmlexpected output

<section id="container">
  <header></header>
  <section id="content"></section>
</section>

super.htmlactual output

<section id="container">
    <section id="content"></section>
</section>

So I'm expecting that including the header block and calling super should get the block from the layout.

Is this a bug or am doing something wrong?

Thanks!

whoward commented 11 years ago

haha you do like nesting things :smile: (love it)

I'll admit I hadn't thought of this use case and I'm not surprised the code isn't quite keeping up here - let me have a look at the source and I'll see if I can get this particular approach working.

whoward commented 11 years ago

for reference the failing test is in this commit: joefiorini/cadenza@a8ba29801e0d13168e418747855c795febc2532a

joefiorini commented 11 years ago

I read a thread on the django issue tracker where someone asked about this very issue. They basically said that that's just the way nested blocks work and you have to deal with it.

Either which way, I actually changed the markup so that the nesting wasn't necessary. A much better, if less intuitive solution IMO.

Makes me wonder if that's the typical case; maybe a need for this kind of nesting indicates a larger problem in your markup?

whoward commented 11 years ago

well it is a bit "out there" and maybe not the best way to use liquid/cadenza/django as they are intended to be template languages with simplicity such that non-programmers (like designers) would be able to figure it out.

That said however I don't think "this is the way things are, deal with it" is an appropriate answer, the ability to use complex stuff like this should be available since the behaviour is pretty much undefined right now.

I don't want Cadenza to handcuff you from doing complex layouts, in fact I'd love to have it as an option replace Rails/Sinatra views with it because it intentionally prevents you from executing code in your views that you shouldn't. I think experts would appreciate things like this.

I'm wondering if this is an indication that nested blocks should work like namespacing in other languages, consider this ruby example:

class Foo
   class Bar
   end
end

You don't end up with two classes Foo and Bar, you end up with two classes Foo and Foo::Bar

Perhaps Cadenza should function in a similar fashion?

{% block layout %}
  {% block header %}
    <header></header>
  {% endblock %}
  {% block content %}{% endblock %}
{% endblock %}

Perhaps this should give you three named blocks: layout, layout::header and layout::content instead of how it currently works (three blocks named layout, header, content)

joefiorini commented 11 years ago

Makes some sense. Would the namespace be something the user would have to refer to it by or could they just say "header" instead of having to say "layout.header"?

Sent from my mobile device

On Jan 30, 2013, at 10:44 AM, William Howard notifications@github.com wrote:

well it is a bit "out there" and maybe not the best way to use liquid/cadenza/django as they are intended to be template languages with simplicity such that non-programmers (like designers) would be able to figure it out.

That said however I don't think "this is the way things are, deal with it" is an appropriate answer, the ability to use complex stuff like this should be available since the behaviour is pretty much undefined right now.

I don't want Cadenza to handcuff you from doing complex layouts, in fact I'd love to have it as an option replace Rails/Sinatra views with it because it intentionally prevents you from executing code in your views that you shouldn't. I think experts would appreciate things like this.

I'm wondering if this is an indication that nested blocks should work like namespacing in other languages, consider this ruby example:

class Foo class Bar end end You don't end up with two classes Foo and Bar, you end up with two classes Foo and Foo::Bar

Perhaps Cadenza should function in a similar fashion?

{% block layout %} {% block header %}

{% endblock %} {% block content %}{% endblock %} {% endblock %} Perhaps this should give you three named blocks: layout, layout::header and layout::content instead of how it currently works (three blocks named layout, header, content)

— Reply to this email directly or view it on GitHub.

whoward commented 11 years ago

I'm hoping not, unless you're doing crazy hacky stuff that is.

Inside the template the parser should be able to infer the namespace by tracking the block nesting On Jan 30, 2013 3:03 PM, "Joe Fiorini" notifications@github.com wrote:

Makes some sense. Would the namespace be something the user would have to refer to it by or could they just say "header" instead of having to say "layout.header"?

Sent from my mobile device

On Jan 30, 2013, at 10:44 AM, William Howard notifications@github.com wrote:

well it is a bit "out there" and maybe not the best way to use liquid/cadenza/django as they are intended to be template languages with simplicity such that non-programmers (like designers) would be able to figure it out.

That said however I don't think "this is the way things are, deal with it" is an appropriate answer, the ability to use complex stuff like this should be available since the behaviour is pretty much undefined right now.

I don't want Cadenza to handcuff you from doing complex layouts, in fact I'd love to have it as an option replace Rails/Sinatra views with it because it intentionally prevents you from executing code in your views that you shouldn't. I think experts would appreciate things like this.

I'm wondering if this is an indication that nested blocks should work like namespacing in other languages, consider this ruby example:

class Foo class Bar end end You don't end up with two classes Foo and Bar, you end up with two classes Foo and Foo::Bar

Perhaps Cadenza should function in a similar fashion?

{% block layout %} {% block header %}

{% endblock %} {% block content %}{% endblock %} {% endblock %} Perhaps this should give you three named blocks: layout, layout::header and layout::content instead of how it currently works (three blocks named layout, header, content)

— Reply to this email directly or view it on GitHub.

— Reply to this email directly or view it on GitHubhttps://github.com/whoward/cadenza/issues/17#issuecomment-12909397.

whoward commented 11 years ago

success! I've got your use case working, turns out it was a pretty simple bug

the namespacing issue is probably a separate one, will have to tackle that one later

joefiorini commented 11 years ago

Nice, thanks! Great work!

On Thu, Feb 7, 2013 at 9:36 PM, William Howard notifications@github.comwrote:

success! I've got your use case working, turns out it was a pretty simple bug

the namespacing issue is probably a separate one, will have to tackle that one later

— Reply to this email directly or view it on GitHubhttps://github.com/whoward/cadenza/issues/17#issuecomment-13274242.