示例#1
0
文件: base.py 项目: zzgvh/djangoembed
    def render_oembed(self, oembed_resource, original_url, template_dir=None, context=None):
        """
        Render the oembed resource and return as a string.
        
        Template directory will always fall back to 'oembed/[type].html', but
        a custom template dir can be passed in using the kwargs.
        
        Templates are given two context variables:
        - response: an OEmbedResource
        - original_url: the url that was passed to the consumer
        """
        if not context.get("request", False):
            provided_context = context or Context()
            context = RequestContext(mock_request())
            context.update(provided_context)

        # templates are named for the resources they display, i.e. video.html
        template_name = "%s.html" % oembed_resource.type

        # set up template finder to fall back to the link template
        templates = [os.path.join("oembed", template_name), "oembed/link.html"]

        # if there's a custom template dir, look there first
        if template_dir:
            templates.insert(0, os.path.join("oembed", template_dir, template_name))

        template = select_template(templates)

        context.push()
        context["response"] = oembed_resource
        context["original_url"] = original_url
        rendered = template.render(context)
        context.pop()

        return rendered.strip()  # rendering template may add whitespace
示例#2
0
    def render_oembed(self,
                      oembed_resource,
                      original_url,
                      template_dir=None,
                      context=None):
        """
        Render the oembed resource and return as a string.
        
        Template directory will always fall back to 'oembed/[type].html', but
        a custom template dir can be passed in using the kwargs.
        
        Templates are given two context variables:
        - response: an OEmbedResource
        - original_url: the url that was passed to the consumer
        """
        provided_context = context or Context()
        context = RequestContext(context.get("request") or mock_request())
        context.update(provided_context)

        # templates are named for the resources they display, i.e. video.html
        template_name = '%s.html' % oembed_resource.type

        # set up template finder to fall back to the link template
        templates = [os.path.join('oembed', template_name), 'oembed/link.html']

        # if there's a custom template dir, look there first
        if template_dir:
            templates.insert(
                0, os.path.join('oembed', template_dir, template_name))

        template = select_template(templates)

        context.push()
        context['response'] = oembed_resource
        context['original_url'] = original_url
        rendered = template.render(context)
        context.pop()

        return rendered.strip()  # rendering template may add whitespace
示例#3
0
 def render_html(self, obj, context=None):
     """
     Generate the 'html' attribute of an oembed resource using a template.
     Sort of a corollary to the parser's render_oembed method.  By default,
     the current mapping will be passed in as the context.
     
     OEmbed templates are stored in:
     
     oembed/provider/[app_label]_[model].html
     
     -- or --
     
     oembed/provider/media_video.html
     """        
     provided_context = context or Context()
     context = RequestContext(mock_request())
     context.update(provided_context)
     
     context.push()
     context[self._meta.context_varname] = obj
     rendered = render_to_string(self._meta.template_name, context)
     context.pop()
     return rendered
    def render_html(self, obj, context=None):
        """
        Generate the 'html' attribute of an oembed resource using a template.
        Sort of a corollary to the parser's render_oembed method.  By default,
        the current mapping will be passed in as the context.
        
        OEmbed templates are stored in:
        
        oembed/provider/[app_label]_[model].html
        
        -- or --
        
        oembed/provider/media_video.html
        """
        provided_context = context or Context()
        context = RequestContext(mock_request())
        context.update(provided_context)

        context.push()
        context[self._meta.context_varname] = obj
        rendered = render_to_string(self._meta.template_name, context)
        context.pop()
        return rendered