views:

70

answers:

3

I've got some pages such as the user-agreement that I want to appear with or without the layout (extends tag) depending on how it is called. I'm using jQuery Fancybox to load up these pages in iframes. If JS is disabled, the links should open in a new window w/ the full layout, otherwise, if they're in an iframe, they don't need the layout.

As of now, I'm doing using jQuery to strip out the header/footer (replacing the body with just the content portion)

if(window.location != window.parent.location) {
    $('body').html($('#content'));
}

But it doesn't seem right to load stuff that will never be displayed (or worse, will be displayed for a half second until the JS kicks in). I guess what I could do is create a partial template containing only the content, and then two container pages, one with the header/footer and one without; they both include the partial. Then use JS to modify the link... which would have to point to different views, which would in turn call the different templates... seems like an awful lot of work. There's gotta be a better way?


@ZeissS:

Something like this is not possible in Django:

{% if not iframe %}
    {% extends "layouts/default.html" %}
{% endif %}

The {% extends %} tag must be the first tag in the template, thus it cannot be conditionally commented out. I guess maybe I could do it in the layout itself...and then it'd apply to all my pages.... let's try that.

+1  A: 

You could always pass a GET parameter and load a different template or vary the template itself.

For some URL like example.com/some/view/?show_simple=yes:

show_simple = request.GET.get('show_simple', False)

if show_simple:
    # return minimal template
# return regular template
Bryan
I'm not really sure what `request.GET.get('show_simple', False)` returns if `show_simple` isn't actually given a value... but it should be true. Thus, I think `'show_simple' in request.GET` would work better. You're close to a solution, but your answer still requires two different templates... it's better to pass `show_simple` off to the template and do the logic there.
Mark
A: 

Modify your layout to conditionally include the header footer...

    {% if not iframe %}
    <div id="header">
         ...
    </div>
    {% endif %}

And then just pass {'iframe':'iframe' in request.GET} into your template. Voila! Not nearly as bad as I thought it would be.

Mark
+1  A: 

Using Bryan's and Mark's answer as a basis you could write your own context-processor: http://docs.djangoproject.com/en/1.2/ref/templates/api/#writing-your-own-context-processors

Doing so would allow you to conditionally serve using if statements in your templates.

Probably the most elegant, as you can still use the wrapper as needed.

You could combine this with your own template tag, which provides you with extend functionality, but conditional based on a provided variable. (Not sure if you can replicate extends functionality).

http://docs.djangoproject.com/en/1.2/howto/custom-template-tags/#writing-custom-template-tags

The other option is to use django's {% include %} tag, and you can wrap that using if statements.

Mike Scott
I've already tried replicating the extends functionality because of another issue I had. It's not easy. Using a context processor would work well too though.
Mark
Yeah I'm not suprised. Extends would be a rather unique one. However just looking at the extends code, it seems extends takes a variable. So you could conditionally change that variable and extend from a minimal template, or a fully featured one?
Mike Scott
I'm not even sure conditionally extending would solve the issue... actually, I'm sure it wouldn't. Well, unless I conditionally chose a *different* template to extend. But I actually do want all the `<head>` stuff from the layout, just not the header and footer. I prompted some discussion of extending the extends node here: http://stackoverflow.com/questions/3209715/how-to-make-a-template-extend-another-one/3385098#3385098 if interested
Mark