Example #1
0
    def render(self, info, format="html", fragment=False, template=None):
        """
        info == dict of variables to stick into the template namespace
        format == output format if applicable
        fragment == special rules about rendering part of a page
        template == dotted.path.to.template (without .ext)
        """

        vars = info

        # check to see if we were passed a function get extra vars
        if callable(self.get_extra_vars):
            vars.update(self.get_extra_vars())

        self.breve_opts.update(self.get_config(vars))
        template_path, template_filename, args = self.load_template(template)
        # self.breve_opts.update ( args )

        template_root = self.breve_opts['root']
        format = args.get('format', format)

        if template_root and template_path.startswith(template_root):
            # this feels mildly brittle
            template_path = template_path[len(template_root) + 1:]

        if format not in self.tag_defs:
            # this seems weak (concerns about path). Should perhaps
            # find a better way, but getting only a string for format
            # makes it difficult to do too much
            self.tag_defs[format] = __import__(format, {}, {})

        self.breve_opts['doctype'] = self.breve_opts.get(
            'doctype', self.tag_defs[format].doctype)
        template_obj = Template(tags=self.tag_defs[format].tags,
                                xmlns=self.tag_defs[format].xmlns,
                                **self.breve_opts)

        if fragment:
            return template_obj.render_partial(os.path.join(
                template_path, template_filename),
                                               vars=vars,
                                               **self.breve_opts)

        return template_obj.render(os.path.join(template_path,
                                                template_filename),
                                   vars=vars,
                                   **self.breve_opts)
Example #2
0
    def render ( self, info, format = "html", fragment = False, template = None ):
        """
        info == dict of variables to stick into the template namespace
        format == output format if applicable
        fragment == special rules about rendering part of a page
        template == dotted.path.to.template (without .ext)
        """

        vars = info

        # check to see if we were passed a function get extra vars
        if callable ( self.get_extra_vars ):
            vars.update ( self.get_extra_vars ( ) )
            
        self.breve_opts.update ( self.get_config ( vars ) )
        template_path, template_filename, args = self.load_template ( template )
        # self.breve_opts.update ( args )

        template_root = self.breve_opts [ 'root' ]
        format = args.get ( 'format', format )

        if template_root and template_path.startswith ( template_root ):
            # this feels mildly brittle
            template_path = template_path [ len ( template_root ) + 1: ]

        if format not in self.tag_defs:
            # this seems weak (concerns about path). Should perhaps
            # find a better way, but getting only a string for format
            # makes it difficult to do too much
            self.tag_defs [ format ] = __import__ ( format, { }, { } )

        self.breve_opts [ 'doctype' ] = self.breve_opts.get ( 'doctype', self.tag_defs [ format ].doctype )
        template_obj = Template ( tags = self.tag_defs [ format ].tags,
                                  xmlns = self.tag_defs [ format ].xmlns,
                                  **self.breve_opts )

        if fragment:
            return template_obj.render_partial ( os.path.join ( template_path, template_filename ),
                                                 vars = vars, **self.breve_opts )
        
        return template_obj.render ( os.path.join ( template_path, template_filename ),
                                     vars = vars, **self.breve_opts )