How to get a layout-less version of a page?
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开发者_如何学C 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.
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.
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
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.
精彩评论