Skip to content

Commit

Permalink
Allow rendering of subtiles.
Browse files Browse the repository at this point in the history
Now it's possible to reference and resolve tiles in tiles.
  • Loading branch information
thet committed May 12, 2018
1 parent 3d3a340 commit 5c8576b
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 11 deletions.
4 changes: 4 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ Breaking changes:

New features:

- Allow rendering of subtiles.
Now it's possible to reference and resolve tiles in tiles.
[thet]

- Added events to notify before/after tile rendering.
[thet]

Expand Down
57 changes: 53 additions & 4 deletions plone/app/blocks/tests/test_tiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,20 @@ def __call__(self):
url=self.request.getURL())


class TestTile2(Tile):

def __call__(self):
return """\
<html>
<body>
<p>
I'm a tile calling another tile as subtile.
</p>
<div data-tile="./@@test.tile1/subtile"/>
</body>
</html>"""


@implementer(ITestTile)
class TestTileBroken(TestTile):

Expand Down Expand Up @@ -74,6 +88,16 @@ def setUpZope(self, app, configurationContext):
for="*"
/>
<plone:tile
name="test.tile2"
title="Test Tile 2"
description="Tile calling tile1 as subtile"
add_permission="cmf.ModifyPortalContent"
class="plone.app.blocks.tests.test_tiles.TestTile2"
permission="zope2.View"
for="*"
/>
<plone:tile
name="test.tile1.broken"
title="Broken Test Tile"
Expand All @@ -88,14 +112,14 @@ def setUpZope(self, app, configurationContext):
</configure>
""", context=configurationContext)

BLOCKS_TILES_FIXTURE = TestTilesLayer()

BLOCKS_TILES_FIXTURE = TestTilesLayer()
BLOCKS_TILES_INTEGRATION_TESTING = IntegrationTesting(
bases=(BLOCKS_TILES_FIXTURE,), name="Blocks:Tiles:Integration")


testLayout1 = """\
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!DOCTYPE html>
<html data-layout="./@@default-site-layout">
<head></head>
<body>
Expand All @@ -114,11 +138,11 @@ def setUpZope(self, app, configurationContext):
</div>
</body>
</html>
"""
""" # noqa


testLayout2 = """\
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!DOCTYPE html>
<html data-layout="./@@default-site-layout">
<head></head>
<body>
Expand All @@ -137,6 +161,20 @@ def setUpZope(self, app, configurationContext):
</div>
</body>
</html>
""" # noqa


testLayout3 = """\
<!DOCTYPE html>
<html data-layout="./@@default-site-layout">
<head></head>
<body>
<h1>Welcome!</h1>
<div data-panel="panel1">
<div data-tile="./@@test.tile2"/>
</div>
</body>
</html>
"""


Expand Down Expand Up @@ -164,3 +202,14 @@ def testRenderTilesError(self):
self.assertNotIn('This is a demo tile with id tile3', result)
self.assertIn('There was an error while rendering this tile', result)
self.assertIn('This is a demo tile with id tile4', result)

def testRenderSubTile(self):
serializer = getHTMLSerializer([testLayout3])
request = self.layer['request']
tree = serializer.tree
renderTiles(request, tree)
result = serializer.serialize()
# The incomplete @@test.tile2 is calling @@test.tile1 and both are
# resolved.
self.assertIn("I'm a tile calling another tile as subtile.", result)
self.assertIn("This is a demo tile with id subtile", result)
16 changes: 9 additions & 7 deletions plone/app/blocks/tiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,7 @@ def renderTiles(request, tree):
except RuntimeError:
tileTree = None
except NotFound:
logger.warn(
'NotFound while trying to render tile: {0}'.format(
tileHref
)
)
logger.warn('NotFound while trying to render tile: %s', tileHref)
continue
if tileTree is not None:
tileRoot = tileTree.getroot()
Expand Down Expand Up @@ -110,12 +106,18 @@ def renderTiles(request, tree):
tileBody.append(result)
except XSLTApplyError:
logger.exception(
'Failed to transform tile {0:s} for {0:s}'.format(
tileHref, baseURL))
'Failed to transform tile %s for %s',
tileHref,
baseURL
)
if tileHead is not None:
for tileHeadChild in tileHead:
headNode.append(tileHeadChild)
utils.replace_with_children(tileNode, tileBody)
notify(events.AfterTileRenderEvent(tileHref, tileNode))

if utils.headTileXPath(tree) or utils.bodyTileXPath(tree):
# Recursively render tiles, as long as they are referenced
tree = renderTiles(request, tree)

return tree

0 comments on commit 5c8576b

Please sign in to comment.