Skip to content

Commit

Permalink
Add context arguments to TemplateColumn (#509)
Browse files Browse the repository at this point in the history
* Add context arguments to TemplateColumn
* Update arguments name for TemplateColumn
* Fix consistent style of template variables in the example
* Improving the grammar of TemplateColumn documentation
* Add tests for extra_context argument in TemplateColumn
  • Loading branch information
ad-m authored and jieter committed Dec 5, 2017
1 parent 07941d7 commit 0bf3a9f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
11 changes: 8 additions & 3 deletions django_tables2/columns/templatecolumn.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class TemplateColumn(Column):
Arguments:
template_code (str): template code to render
template_name (str): name of the template to render
extra_content (dict): optional extra template context
A `~django.template.Template` object is created from the
*template_code* or *template_name* and rendered with a context containing:
Expand All @@ -26,24 +27,27 @@ class TemplateColumn(Column):
- *value* -- value from `record` that corresponds to the current column
- *default* -- appropriate default value to use as fallback
- *row_counter* -- The number of the row this cell is being rendered in.
- any context variables passed using the `extra_context` argument to `TemplateColumn`.
Example:
.. code-block:: python
class ExampleTable(tables.Table):
foo = tables.TemplateColumn('{{ record.bar }}')
# contents of `myapp/bar_column.html` is `{{ value }}`
bar = tables.TemplateColumn(template_name='myapp/name2_column.html')
# contents of `myapp/bar_column.html` is `{{ label }}: {{ value }}`
bar = tables.TemplateColumn(template_name='myapp/name2_column.html',
extra_context={'label': 'Label'})
Both columns will have the same output.
'''
empty_values = ()

def __init__(self, template_code=None, template_name=None, **extra):
def __init__(self, template_code=None, template_name=None, extra_context=None, **extra):
super(TemplateColumn, self).__init__(**extra)
self.template_code = template_code
self.template_name = template_name
self.extra_context = extra_context or {}

if not self.template_code and not self.template_name:
raise ValueError('A template must be provided')
Expand All @@ -52,6 +56,7 @@ def render(self, record, table, value, bound_column, **kwargs):
# If the table is being rendered using `render_table`, it hackily
# attaches the context to the table as a gift to `TemplateColumn`.
context = getattr(table, 'context', Context())
context.update(self.extra_context)
context.update({
'default': bound_column.default,
'column': bound_column,
Expand Down
4 changes: 4 additions & 0 deletions tests/columns/test_templatecolumn.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,18 @@ def test_should_handle_context_on_table():
class TestTable(tables.Table):
col_code = tables.TemplateColumn(template_code='code:{{ record.col }}-{{ foo }}')
col_name = tables.TemplateColumn(template_name='test_template_column.html')
col_context = tables.TemplateColumn(template_code="{{ label }}:{{ record.col }}-{{ foo }}",
extra_context={'label': 'label'})

table = TestTable([{'col': 'brad'}])
assert table.rows[0].get_cell('col_code') == 'code:brad-'
assert table.rows[0].get_cell('col_name') == 'name:brad-empty\n'
assert table.rows[0].get_cell("col_context") == "label:brad-"

table.context = Context({'foo': 'author'})
assert table.rows[0].get_cell('col_code') == 'code:brad-author'
assert table.rows[0].get_cell('col_name') == 'name:brad-author\n'
assert table.rows[0].get_cell("col_context") == "label:brad-author"

# new table and render using the 'render_table' template tag.
table = TestTable([{'col': 'brad'}])
Expand Down

0 comments on commit 0bf3a9f

Please # to comment.