views:

41

answers:

1

Hi

I know about passing the context to templates, but I am bit confused with the given scenario, please help

class X:

id:

name:

status:

Class Main:

number1: object of X

number2: object of X

message: "Hello World!"

I get Object of Main which has two X objects but with different contexts. I want to write one template for X and pass different conetext to it for code re usability and maintainability.

so I am trying to do this in my presentation logic, where I have object of Main

<div class="ui-tabs-panel" id="tab-results">
    {% include "render/objectX.html" %}
  </div>

and objectX.html is:

{% block content %}
<div id="d">
 <table id="c">
  <tbody>
    <tr>
     <td>id : {{ x.id }}</td>
     <td>name : {{ x.name }}</td>
     </tr>
   </tbody>
 </table>
</div>
{% endblock %}

how can I pass the Main.number1(object of X) explicitly to template??

Thank you

+2  A: 

One simple way would be to wrap the include with a {% with %} template tag. For example, assuming you have main in your context:

<div class="ui-tabs-panel" id="tab-results">
    {% with main.number1 as x %}
        {% include "render/objectX.html" %}
    {% endwith %}
</div>

This will put the number1 object into the context as variable named x, which can be used in the included template.

robhudson
This was perfect, Thank you
learner