Re: included templates to load own objects
Hi Mae, On Mon, 2006-08-28 at 14:53 +, Mae wrote: > I want included-including templates to be able to act independently > from each other. I want the including template to not have to know > anything about the particulars of the template it's including. > > I want to be able to write {%include magic_template%} and > magic_template would be a url that would get mapped to its own view > function, which would do complex database access operations, open > sockets, do business logic, whatever, before returning > magic_template.html. > > I have dozens of pages. All of those pages are different from each > other, but they all have one thing in common -- they include > ticker.html. Now this ticker.html requires certain variables to be > pre-populated. Right now, I have to manually attach every single one > of those variables to every single view function for every single page. > If I need to add another variable, I'll have to paste its name into > dozens of "render_to_response" statements. This is a maintenance mess. > And every single page is obliged to know that it's including > ticker.html, because it has to pass variables to it. That's an > architectural mess. This can't be done with the {% include %} tag, but it is the ideal example of when a custom template tag is useful. After writing the tag, including the tag as {% ticker_data %} wherever you needed it in your template (plus a "{% load ... %}" at the top) is all you would need to do. If your ticker data is best formatted via a small template fragment, look at the inclusion_tag() shortcut for template tags ([1]). Otherwise, you probably just need a simple_tag() template tag -- see [2] -- since you are just returning a string from minimal (in fact, no) input parameters. [1] http://www.djangoproject.com/documentation/templates_python/#inclusion-tags [2] http://www.djangoproject.com/documentation/templates_python/#shortcut-for-simple-tags Regards, Malcolm --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users -~--~~~~--~~--~--~---
Re: included templates to load own objects
Hmm. Alan, your post gives me much to think about and try. Thank you. Mae --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users -~--~~~~--~~--~--~---
Re: included templates to load own objects
Hi Mae, On 8/29/06, Mae <[EMAIL PROTECTED]> wrote: > I think what you're talking about is this paragraph from the doc: > "Also, you can give RequestContext a list of additional processors, > using the optional, third positional argument, processors. Actually, I was more thinking that you could write a TemplateContextProcessor, calling it TickerContextProcessor and then include TickerContextProcessor in the TEMPLATE_CONTEXT_PROCESSORS configuration setting. TickerContextProcessor would add to the context all the variables required for the ticker. This works well if the Ticker is required for every request. If not, you might consider writing a custom tag that can be called by ticker.html and would load the context with the correct variables. Hope it works out for you, Alan. -- Alan Green [EMAIL PROTECTED] - http://bright-green.com --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users -~--~~~~--~~--~--~---
Re: included templates to load own objects
I want included-including templates to be able to act independently from each other. I want the including template to not have to know anything about the particulars of the template it's including. I want to be able to write {%include magic_template%} and magic_template would be a url that would get mapped to its own view function, which would do complex database access operations, open sockets, do business logic, whatever, before returning magic_template.html. I have dozens of pages. All of those pages are different from each other, but they all have one thing in common -- they include ticker.html. Now this ticker.html requires certain variables to be pre-populated. Right now, I have to manually attach every single one of those variables to every single view function for every single page. If I need to add another variable, I'll have to paste its name into dozens of "render_to_response" statements. This is a maintenance mess. And every single page is obliged to know that it's including ticker.html, because it has to pass variables to it. That's an architectural mess. So I'm looking for a way to include a template, passing it variables, without ever having to type the names of those variables into anything. Is this a better explanation? Mae Corey Oordt wrote: > Mae, > > I think it may be because we're not sure of what you are asking for. > I know it didn't really make sense to me when I red it the first time. > > What exactly are you trying to accomplish? > > Corey > > On Aug 25, 2006, at 10:35 AM, Mae wrote: > > > > > Nobody? Is it because I asked the question wrong or because there > > just > > is no answer? > > > > Mae > > > > > > > --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users -~--~~~~--~~--~--~---
Re: included templates to load own objects
Alan, I think what you're talking about is this paragraph from the doc: "Also, you can give RequestContext a list of additional processors, using the optional, third positional argument, processors. In this example, the RequestContext instance gets a ip_address variable: def ip_address_processor(request): return {'ip_address': request.META['REMOTE_ADDR']} def some_view(request): # ... return RequestContext(request, { 'foo': 'bar', }, [ip_address_processor]) " This isn't what I want, because it still forces me to explicitly write something in every single view function, for every single variable, that I ever want to appear in any included templates. That sounds like a maintenace nightmare. What I want is a way for an included template to have its _own_ view function and load its own objects. And I want the containing template to know nothing of the variables the included template uses. Mae Alan Green wrote: > Hi Mae, > > Two thoughts here: > > 1. Have a look at the TemplateContextProcessors: > http://www.djangoproject.com/documentation/templates_python/#subclassing-context-requestcontext > > 2. Custom tags aren't that hard to write. > > Alan. > > On 8/25/06, Mae <[EMAIL PROTECTED]> wrote: > > > > Hi all, here's my problem: > > I have a bunch of templates that look like this: > > > > base.html: > > {% block ticker %} > > { % include "ticker.html" % } > > {% endblock %} > > > > content_page1.html: > > {% extends "base.html" %} > > do other stuff... > > > > content_page2.html: > > {% extends "base.html" %} > > do completely other stuff... > > > > > > I want the ticker.html template to use a Ticker object. I could load > > the ticker object in the views for content_page1 and content_page2, and > > then pass Ticker to ticker.html. But that's messy. What if I have 40 > > content pages? I'd have to pass Ticker in 40 different view methods! > > And I don't want the content pages to have to know how ticker.html > > works, they should just include it, and delegate the details of its > > operation to it. > > > > So what I need, is to do something like > > { % include "ticker" % } > > where "ticker" is registered in urls.py as its own view, loading its > > own set of objects. > > > > Can I do this? I've read the templates doc page, and found no > > suggestions. Except maybe to use a custom tag, but I'd like to be able > > to do this for a bunch of objects, casually, and I don't want to have > > to write a custom tag for every occasion. > > > > I've checked out this > > http://groups.google.com/group/django-users/browse_thread/thread/1fbc16605cdb43b/819c3bbf9a52c375?lnk=st=included+template+object+django=3=en#819c3bbf9a52c375 > > post about inclusion tags, and it's not exactly what I want. As far as > > I can tell, inclusion tags would allow me to load an object in the > > parent template, and then pass it to the included template. But I want > > the included template to be responsible for loading their own objects. > > > > Any ideas? > > Thanks, > > Mae > > > > > > > > > > > > -- > Alan Green > [EMAIL PROTECTED] - http://bright-green.com --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users -~--~~~~--~~--~--~---
Re: included templates to load own objects
Hi Mae, Two thoughts here: 1. Have a look at the TemplateContextProcessors: http://www.djangoproject.com/documentation/templates_python/#subclassing-context-requestcontext 2. Custom tags aren't that hard to write. Alan. On 8/25/06, Mae <[EMAIL PROTECTED]> wrote: > > Hi all, here's my problem: > I have a bunch of templates that look like this: > > base.html: > {% block ticker %} > { % include "ticker.html" % } > {% endblock %} > > content_page1.html: > {% extends "base.html" %} > do other stuff... > > content_page2.html: > {% extends "base.html" %} > do completely other stuff... > > > I want the ticker.html template to use a Ticker object. I could load > the ticker object in the views for content_page1 and content_page2, and > then pass Ticker to ticker.html. But that's messy. What if I have 40 > content pages? I'd have to pass Ticker in 40 different view methods! > And I don't want the content pages to have to know how ticker.html > works, they should just include it, and delegate the details of its > operation to it. > > So what I need, is to do something like > { % include "ticker" % } > where "ticker" is registered in urls.py as its own view, loading its > own set of objects. > > Can I do this? I've read the templates doc page, and found no > suggestions. Except maybe to use a custom tag, but I'd like to be able > to do this for a bunch of objects, casually, and I don't want to have > to write a custom tag for every occasion. > > I've checked out this > http://groups.google.com/group/django-users/browse_thread/thread/1fbc16605cdb43b/819c3bbf9a52c375?lnk=st=included+template+object+django=3=en#819c3bbf9a52c375 > post about inclusion tags, and it's not exactly what I want. As far as > I can tell, inclusion tags would allow me to load an object in the > parent template, and then pass it to the included template. But I want > the included template to be responsible for loading their own objects. > > Any ideas? > Thanks, > Mae > > > > > -- Alan Green [EMAIL PROTECTED] - http://bright-green.com --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users -~--~~~~--~~--~--~---
included templates to load own objects
Hi all, here's my problem: I have a bunch of templates that look like this: base.html: {% block ticker %} { % include "ticker.html" % } {% endblock %} content_page1.html: {% extends "base.html" %} do other stuff... content_page2.html: {% extends "base.html" %} do completely other stuff... I want the ticker.html template to use a Ticker object. I could load the ticker object in the views for content_page1 and content_page2, and then pass Ticker to ticker.html. But that's messy. What if I have 40 content pages? I'd have to pass Ticker in 40 different view methods! And I don't want the content pages to have to know how ticker.html works, they should just include it, and delegate the details of its operation to it. So what I need, is to do something like { % include "ticker" % } where "ticker" is registered in urls.py as its own view, loading its own set of objects. Can I do this? I've read the templates doc page, and found no suggestions. Except maybe to use a custom tag, but I'd like to be able to do this for a bunch of objects, casually, and I don't want to have to write a custom tag for every occasion. I've checked out this http://groups.google.com/group/django-users/browse_thread/thread/1fbc16605cdb43b/819c3bbf9a52c375?lnk=st=included+template+object+django=3=en#819c3bbf9a52c375 post about inclusion tags, and it's not exactly what I want. As far as I can tell, inclusion tags would allow me to load an object in the parent template, and then pass it to the included template. But I want the included template to be responsible for loading their own objects. Any ideas? Thanks, Mae --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users -~--~~~~--~~--~--~---