def test_gizmo(self, mock_tgin): token1 = base.Token(token_type='TOKEN_TEXT', contents='token test_options') gizmos_templatetags.gizmo(parser='', token=token1) # Check Result mock_tgin.assert_called_with('test_options', None) token2 = base.Token(token_type='TOKEN_TEXT', contents='token test_gizmo_name test_options') gizmos_templatetags.gizmo(parser='', token=token2) # Check Result mock_tgin.assert_called_with('test_options', 'test_gizmo_name')
def contextblock(parser, token): """ A special :tag:`block` tag which does not render anything but can be used to modify a template context. The tag is rendered first thus modifying context before other blocks are rendered. A tag in an extending template is rendered after parent tags, allowing you to override template context in child templates. The tag has to be the first tag, immediately after the :tag:`extends` tag. You have to define a empty context block tag at the very start of your base template. Example usage, in your base template:: {% contextblock %}{% endcontextblock %}<html> <body> <head> <title>{{ title }}</title> </head> <body> <h1>{{ title }}</h1> <p><a href="{{ homepage }}">{{ title }}</a></p> </body> </body> </html> In your extending template:: {% extends "base.html" %} {% contextblock %} {% load future i18n %} {% setcontext as title %}{% blocktrans %}{{ username }}'s blog{% endblocktrans %}{% endsetcontext %} {% url "homepage" as homepage %} {% endcontextblock %} """ nodelist = parser.parse(('endcontextblock', )) if hasattr(base, 'TokenType'): TOKEN_VAR = base.TokenType.VAR else: TOKEN_VAR = base.TOKEN_VAR # Make sure we call block.super at least once # (and in ContextBlockNode.super we make sure it is called only once) block_super_token = base.Token(TOKEN_VAR, 'block.super') if hasattr(token, 'source'): block_super_token.source = token.source filter_expression = parser.compile_filter(block_super_token.contents) var_node = base.VariableNode(filter_expression) # To push it through the normal logic first parser.extend_nodelist(nodelist, var_node, block_super_token) # But we want it at the very beginning var_node = nodelist.pop() nodelist.insert(0, var_node) parser.delete_first_token() return ContextBlockNode(None, nodelist)
def test_import_gizmo_dependency_syntax_error(self): token = base.Token(token_type='TOKEN_TEXT', contents='token') self.assertRaises(TemplateSyntaxError, gizmos_templatetags.import_gizmo_dependency, parser='', token=token)
def test_import_gizmo_dependency(self, mock_tgid): token = base.Token(token_type='TOKEN_TEXT', contents='test_tag_name test_gizmo_name') gizmos_templatetags.import_gizmo_dependency(parser='', token=token) # Check Result mock_tgid.assert_called_with('test_gizmo_name')
def test_gizmo_syntax_error(self): token = base.Token(token_type='TOKEN_TEXT', contents='token') # Check Error self.assertRaises(TemplateSyntaxError, gizmos_templatetags.gizmo, parser='', token=token)
def test_token_smart_split(self): # Regression test for #7027 token = template_base.Token( template_base.TOKEN_BLOCK, 'sometag _("Page not found") value|yesno:_("yes,no")') split = token.split_contents() self.assertEqual( split, ["sometag", '_("Page not found")', 'value|yesno:_("yes,no")'])
def test_gizmo_dependencies_not_valid(self): token = base.Token(token_type='TOKEN_TEXT', contents='token css1') self.assertRaises(TemplateSyntaxError, gizmos_templatetags.gizmo_dependencies, parser='', token=token)
def test_gizmo_dependencies(self, mock_tgdn): token = base.Token(token_type='TOKEN_TEXT', contents='token "css"') gizmos_templatetags.gizmo_dependencies(parser='', token=token) # Check Result mock_tgdn.assert_called_with('css')