masking only a small part of a template

37 views
Skip to first unread message

tWoolie

unread,
Jul 8, 2012, 10:06:36 PM7/8/12
to django...@googlegroups.com
I have a template in a 3rd party app that i'm trying to override, but only a single block. 
Is there any way that I can do this without copy-pasting the entire template into the global templates dir just to change a few lines?
Is there any way to create a template that "extends" itself from further down the search tree?

yati sagade

unread,
Jul 9, 2012, 12:44:33 AM7/9/12
to django...@googlegroups.com
I'm not sure by what you mean; There has always been template inheritance in Django :) Just specify the template you want to extend as the first statement in the template and to override a block in the template, just start a block with the same name as the block you want to override. For example, to override a block called "main" in "some_3rd_party/base.html",  you'd do something like:

    {% extends "some_3rd_party/base.html" %}
    ...
    {% block main %}
       <your stuff>
    {% endblock %}
    ...

All the blocks you don't override in the base template get included in the resultant template as is. Sorry if you already knew all that :)

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/snmqZSyfNlkJ.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.



--
Yati Sagade

Twitter: @yati_itay | Github: yati-sagade

Organizing member of TEDx EasternMetropolitanBypass
http://www.ted.com/tedx/events/4933
https://www.facebook.com/pages/TEDx-EasternMetropolitanBypass/337763226244869


tWoolie

unread,
Jul 9, 2012, 2:06:46 AM7/9/12
to django...@googlegroups.com
Sorry, I should have been clearer. What I have is an external project that is included "as-is". It has it's templates and it's views are hardcoded against the template names.
I want to know if it's possible to create a template called, say, "app/dashboard.html" in the global template dir that then extends "app/dashboard.html" in the app's template directory, thereby allowing me to change ONLY the block I need to change, without requiring me to copy-paste the entire template and re-edit every time I update that app.

Tomas Neme

unread,
Jul 9, 2012, 9:59:22 AM7/9/12
to django...@googlegroups.com
http://djangosnippets.org/snippets/1376/

this is what you want, I'm using it and it works like a charm

you override in templates/path/to/template.html and do

{% extends "app:path/to/template.html" %}, and this loader searches in
`app`'s template dir by default
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/django-users/-/G2ExbPmO4e0J.
>
> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to
> django-users...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/django-users?hl=en.



--
"The whole of Japan is pure invention. There is no such country, there
are no such people" --Oscar Wilde

|_|0|_|
|_|_|0|
|0|0|0|

(\__/)
(='.'=)This is Bunny. Copy and paste bunny
(")_(") to help him gain world domination.

lacry...@gmail.com

unread,
Jul 9, 2012, 10:18:26 AM7/9/12
to django...@googlegroups.com

I think he means to override a template without changing it's name (since you don't control 3rd party app's views and the templates they call, most times). I've found a snippet to do this as {% extends "app:template/path.html" %}, a custom template loader. I don't have the code right now, but may be you can find it.

-----Mensaje original-----
De: yati sagade
Enviados: 09/07/2012 01:44:33
Asunto: Re: masking only a small part of a template

I'm not sure by what you mean; There has always been template inheritance
in Django :) Just specify the template you want to extend as the first
statement in the template and to override a block in the template, just
start a block with the same name as the block you want to override. For
example, to override a block called "main" in "some_3rd_party/base.html",
you'd do something like:

{% extends "some_3rd_party/base.html" %}
...
{% block main %}
<your stuff>
{% endblock %}
...

All the blocks you don't override in the base template get included in the
resultant template as is. Sorry if you already knew all that :)

On Mon, Jul 9, 2012 at 7:36 AM, tWoolie <rocker....@gmail.com> wrote:

> I have a template in a 3rd party app that i'm trying to override, but only
> a single block.
> Is there any way that I can do this without copy-pasting the entire
> template into the global templates dir just to change a few lines?
> Is there any way to create a template that "extends" itself from further
> down the search tree?
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/django-users/-/snmqZSyfNlkJ.
> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to
> django-users...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/django-users?hl=en.
>



--
Yati Sagade <http://twitter.com/yati_itay>

Twitter: @yati_itay <http://twitter.com/yati_itay> | Github:
yati-sagade<https://github.com/yati-sagade>
--
You received this message because you are subscribed to the Google Groups "Django users" gr
Correo truncado a 2,000 caracteres.
:::0:8cd9b62d1bad8b6e64024aa7ff45475a:7d0::::
Reply all
Reply to author
Forward
0 new messages