def node_render(context): if is_pattern_library_context(context): tag_overridden = False result = '' # Load pattern's config current_template_name = parser.origin.template_name pattern_config = get_pattern_config(current_template_name) # Extract values for lookup from the token bits = token.split_contents() tag_name = bits[0] arguments = ' '.join(bits[1:]).strip() # Get config for a specific tag tag_config = pattern_config.get('tags', {}).get(tag_name, {}) if tag_config: # Get config for specific arguments tag_config = tag_config.get(arguments, {}) if 'raw' in tag_config: # Return raw data (it can be string or a structure), if defined result = tag_config['raw'] tag_overridden = True elif 'template_name' in tag_config: # Render pattern, if raw string is not defined template_name = tag_config['template_name'] request = context.get('request') result = render_pattern(request, template_name, allow_non_patterns=True) tag_overridden = True # TODO: Allow objects with the __str__ method # In some cases we must return an object that can # be rendered as a string `{{ result }}` # and allow users to access it's attributes `{{ result.url }}` if tag_overridden: if isinstance(original_node, SimpleNode): # If it's a SimpleNode try to use it's target_var target_var = original_node.target_var else: # If it's a custom tag, check for the target_var in tag's config target_var = tag_config.get('target_var') if target_var: # If a value for the target_var is supplied, # write result into the variable context[target_var] = result return '' # Render result instead of the tag return result else: logger.warning( 'No stub data defined for the "%s" tag in the "%s" template', tag_name, current_template_name) return original_node_render(context)
def get(self, request, pattern_template_name=None): # Get all pattern templates templates = get_pattern_templates() if pattern_template_name is None: # Just display the first pattern if a specific one isn't requested pattern_template_name = self.get_first_template(templates) if not is_pattern(pattern_template_name): raise Http404 template = get_template(pattern_template_name) pattern_config = get_pattern_config(pattern_template_name) context = self.get_context_data() context['pattern_templates'] = templates context['pattern_template_name'] = pattern_template_name context['pattern_source'] = escape(template.template.source) context['pattern_config'] = escape( get_pattern_config_str(pattern_template_name)) context['pattern_name'] = pattern_config.get('name', pattern_template_name) context['pattern_markdown'] = get_pattern_markdown( pattern_template_name) return self.render_to_response(context)