| | 558 | The ``render_to_string()`` shortcut |
|---|
| | 559 | =================================== |
|---|
| | 560 | |
|---|
| | 561 | To cut down on the repetitive nature of loading and rendering |
|---|
| | 562 | templates, Django provides a shortcut function which largely |
|---|
| | 563 | automates the process: ``render_to_string()`` in |
|---|
| | 564 | ``django.template.loader``, which loads a template, renders it and |
|---|
| | 565 | returns the resulting string:: |
|---|
| | 566 | |
|---|
| | 567 | from django.template.loader import render_to_string |
|---|
| | 568 | rendered = render_to_string('my_template.html', { 'foo': 'bar' }) |
|---|
| | 569 | |
|---|
| | 570 | The ``render_to_string`` shortcut takes one required argument -- |
|---|
| | 571 | ``template_name``, which should be the name of the template to load |
|---|
| | 572 | and render -- and two optional arguments:: |
|---|
| | 573 | |
|---|
| | 574 | dictionary |
|---|
| | 575 | A dictionary to be used as variables and values for the |
|---|
| | 576 | template's context. This can also be passed as the second |
|---|
| | 577 | positional argument. |
|---|
| | 578 | |
|---|
| | 579 | context_instance |
|---|
| | 580 | An instance of ``Context`` or a subclass (e.g., an instance of |
|---|
| | 581 | ``RequestContext``) to use as the template's context. This can |
|---|
| | 582 | also be passed as the third positional argument. |
|---|
| | 583 | |
|---|
| | 584 | See also the `render_to_response()`_ shortcut, which calls |
|---|
| | 585 | ``render_to_string`` and feeds the result into an ``HttpResponse`` |
|---|
| | 586 | suitable for returning directly from a view. |
|---|
| | 587 | |
|---|
| | 588 | .. _render_to_response(): ../shortcuts/#render-to-response |
|---|
| | 589 | |
|---|