views:

126

answers:

2

App_1 has a view and a template for this view. It would like to "aggregate" information into this view from other sources (i.e. other apps), without having to add or change anything itself. Perhaps App_2 wants to put "hello world" into this designated area in App_1's view. What is the best way of achieving this?

Signals come to mind naturally. But what should signal receivers send back? In my mind, App_1 template includes a custom tag that sends a signal and receives a list of templates (e.g. 'app_2/template.html') back, and it will register each one. However, it seems like inclusion_tag only supports 1 template rendering for each tag.

What recourse do I have?

A: 

I hope I'm understanding right, but it would seem that your solution to this problem is in your second sentence: 'aggregate'. The view function in App_1 should aggregate, or collect, all of the necessary data, then pass it to the template via a context variable.

I think the question that needs to be resolved is: How does the view function know which data to aggregate?

Typically a view function is coded ahead of time with a knowledge of which data it needs to use. For example, a calendar app might be hard-coded to fetch Event model objects and pass those to the template (via context). In your case however, it seems that the data coming into App_1 is completely arbitrary and not previously defined.

You mentioned signals, but I think the problem that you're going to have here is that signals only give notification that an event has occurred. They don't allow the reeving function to pass data back, nor does the code that emits the signal wait for all of the signal receivers to finish processing before it continues.

Instead perhaps you could establish a registration system. App_ 1 maintains a list of something. The other apps 'register' items to that list and when the view function in App_1 fires, it turns the list into usable data, then passes it to the template via a context variable.

T. Stone
Ah, but signals do have return values :) These 'pull from listener' functions are quite handy. Anyway, I coded up a multi-template inclusion_tag anyway, since the logic of rendering other templates shouldn't reside in views (which was what I originally had done).
MTsoul
A: 

I ended up doing what I described, but without the default inclusion_tag support. I made a custom tag that sent signals, collected template names, and created a Node that renders each template in series and concatenates the result. This seems to work fine, and has the same logic as the inclusion tag shebang.

MTsoul