Skip to content

Commit

Permalink
Be able to put an HTML separator between the children of a RepeatingW…
Browse files Browse the repository at this point in the history
…idget.

We also need to support it for the CompoundWidget since it uses the same template
  • Loading branch information
LeResKP committed Sep 19, 2013
1 parent 1c133c9 commit db71764
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 10 deletions.
17 changes: 17 additions & 0 deletions tests/test_widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,23 @@ class DummyRepeatingTestWidget(wd.RepeatingWidget):
children=[DummyWidget()]
w = DummyRepeatingTestWidget()

def test_separator(self):
widget = RepeatingTestWidget
attrs = {
'id': "rw",
'repetitions': 3,
'validator': AlwaysValidateFalseValidator,
'separator': '<hr />'}
params = {'separator': '<hr />'}
expected = ('<div id="rw">'
'<p>Test Widget</p><hr />'
'<p>Test Widget</p><hr />'
'<p>Test Widget</p>'
'</div>')
for engine in self._get_all_possible_engines():
yield (self._check_rendering_vs_expected,
engine, attrs, params, expected)

class DisplayOnlyTestWidget(wd.DisplayOnlyWidget):
child = twc.Variable(default=AlwaysValidateFalseWidget)
template = "tw2.core.test_templates.display_only_test_widget"
Expand Down
9 changes: 7 additions & 2 deletions tw2/core/templates/display_children.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
<?python
num = len(w.children)
?>
<div xmlns:py="http://genshi.edgewall.org/" py:attrs="w.attrs">
<py:for each="c in w.children">${c.display()}</py:for>
</div>
<py:for each="i,c in enumerate(w.children)">${c.display()}
<py:if test="w.separator and i != (num-1)">${w.separator}</py:if>
</py:for>
</div>
1 change: 1 addition & 0 deletions tw2/core/templates/display_children.jinja
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<div {{w.attrs | xmlattr }}>
{% for c in w.children %}
{{c.display()}}
{% if w.separator and not loop.last %}{{w.separator|safe}}{% endif %}
{% endfor %}
</div>
9 changes: 7 additions & 2 deletions tw2/core/templates/display_children.kajiki
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
<?python
num = len(w.children)
?>
<div xmlns:py="http://genshi.edgewall.org/" py:attrs="w.attrs">
<py:for each="c in w.children">${c.display()}</py:for>
</div>
<py:for each="i,c in enumerate(w.children)">${c.display()}
<py:if test="w.separator and i != (num-1)">${w.separator}</py:if>
</py:for>
</div>
5 changes: 4 additions & 1 deletion tw2/core/templates/display_children.mak
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ if 'class' in attr_keys:
<%namespace name="tw" module="tw2.core.mako_util"/>\
<div ${tw.attrs(attrs=w.attrs)}>
% for c in w.children:
${c.display() | n}
${c.display() | n}
% if w.separator and not loop.last:
${w.separator |n}
%endif
% endfor
</div>
10 changes: 5 additions & 5 deletions tw2/core/templates/display_children.pt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div tal:define="compile import: tw2.core.mako_util" tal:omit-tag=""></div>
<div tal:attributes="id w.attrs.get('id')"><span
tal:repeat="c w.children"
tal:content="structure c.display()"
tal:omit-tag=""
></span></div>
<div tal:attributes="id w.attrs.get('id')">
<loop tal:repeat="c w.children" tal:omit-tag="">
<span tal:replace="c.display()"></span>
<span tal:condition="w.separator and not repeat.c.end" tal:replace="structure w.separator"></span>
</loop></div>
9 changes: 9 additions & 0 deletions tw2/core/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from . import params as pm
import six
from six.moves import filter
from markupsafe import Markup

try:
import formencode
Expand Down Expand Up @@ -577,6 +578,8 @@ class CompoundWidget(Widget):
"CompoundWidgets that have no id",
)
template = 'tw2.core.templates.display_children'
separator = pm.Param('HTML snippet which will be inserted '
'between each repeated child', default=None)

@classmethod
def post_define(cls):
Expand Down Expand Up @@ -617,6 +620,8 @@ def prepare(self):
Propagate the value for this widget to the children, based on their id.
"""
super(CompoundWidget, self).prepare()
if self.separator:
self.separator = Markup(self.separator)
v = self.value or {}
if not hasattr(self, '_validated'):
if hasattr(v, '__getitem__'):
Expand Down Expand Up @@ -788,6 +793,8 @@ class RepeatingWidget(Widget):
repetition = pm.ChildVariable('The repetition of a child widget.')

template = 'tw2.core.templates.display_children'
separator = pm.Param('HTML snippet which will be inserted '
'between each repeated child', default=None)

@classmethod
def post_define(cls):
Expand Down Expand Up @@ -821,6 +828,8 @@ def prepare(self):
index.
"""
super(RepeatingWidget, self).prepare()
if self.separator:
self.separator = Markup(self.separator)
value = self.value or []
if self.repetitions is None:
reps = len(value) + self.extra_reps
Expand Down

0 comments on commit db71764

Please sign in to comment.