Beispiel #1
0
    def display(self, value=None, **params):
        """Display the widget in a Kid template.

        Returns an elementtree node instance. If you need serialized output
        in a string, call 'render' instead.

        Probably you will not need to override or extend if inhertiting from
        Widget.

        @params:

        value   : The value to display in the widget.
        **params: Extra parameters specific to the widget. All keyword params
                  supplied will pass through the update_params method which
                  will have a last chance to modify them before reaching the
                  template.

        """
        if not getattr(self, 'template_c', False):
            warnings.warn("Widget instance '%r' has no template defined" %
                          self)
            return None

        # logic for managing the params attribute
        for param in self.__class__.params:
            if param in params:
                param_value = params[param]
                if callable(param_value):
                    param_value = param_value()

            else:
                # if the param hasn't been overridden (passed as a keyword
                # argument inside **params) put the corresponding instance
                # value inside params.
                param_value = getattr(self, param, None)

            # make sure we don't pass a reference to mutables
            params[param] = copy_if_mutable(param_value)

        if not params.get('name'):
            params['name'] = self.name
        if 'assume_encoding' not in params:
            params['assume_encoding'] = config.get('kid.assume_encoding',
                                                   'utf-8')
        params['value'] = to_unicode(self.adjust_value(value, **params))
        self.update_params(params)
        # update_data has been deprecated
        self.update_data(params)

        try:
            transform = view.engines['kid'].transform

        except (KeyError, AttributeError):
            # this can happen if you render a widget before application startup
            # when view.load_engines() has not yet been called
            raise RuntimeError("Trying to render a widget,"
                               " but Kid templating engine is not yet loaded.")

        return transform(params, self.template_c)
Beispiel #2
0
    def display(self, value=None, **params):
        """Display the widget in a Kid template.

        Returns an elementtree node instance. If you need serialized output
        in a string, call 'render' instead.

        Probably you will not need to override or extend if inhertiting from
        Widget.

        @params:

        value   : The value to display in the widget.
        **params: Extra parameters specific to the widget. All keyword params
                  supplied will pass through the update_params method which
                  will have a last chance to modify them before reaching the
                  template.

        """
        if not getattr(self, 'template_c', False):
            warnings.warn("Widget instance '%r' has no template defined" % self)
            return None

        # logic for managing the params attribute
        for param in self.__class__.params:
            if param in params:
                param_value = params[param]
                if callable(param_value):
                    param_value = param_value()

            else:
                # if the param hasn't been overridden (passed as a keyword
                # argument inside **params) put the corresponding instance
                # value inside params.
                param_value = getattr(self, param, None)

            # make sure we don't pass a reference to mutables
            params[param] = copy_if_mutable(param_value)

        if not params.get('name'):
            params['name'] = self.name
        if 'assume_encoding' not in params:
            params['assume_encoding'] = config.get('kid.assume_encoding', 'utf-8')
        params['value'] = to_unicode(self.adjust_value(value, **params))
        self.update_params(params)
        # update_data has been deprecated
        self.update_data(params)

        try:
            transform = view.engines['kid'].transform

        except (KeyError, AttributeError):
            # this can happen if you render a widget before application startup
            # when view.load_engines() has not yet been called
            raise RuntimeError("Trying to render a widget,"
                " but Kid templating engine is not yet loaded.")

        return transform(params, self.template_c)
Beispiel #3
0
    def display(self, value=None, **params):
        """
        Display the widget in a Kid template. Returns an elementtree node 
        instance. If you need serialized output in a string call 'render'
        instead.
        Probably you will not need to override or extend if inhertitting from
        Widget.

        @params:

        value    : The value to display in the widget.
        **params : Extra parameters specific to the widget. All keyword params
                   supplied will pass through the update_params method which will 
                   have a last chance to modify them before reaching the
                   template. 
        """

        if not getattr(self, "template_c", False):
            warnings.warn("Widget instance '%r' has no template defined" % self)
            return None
        # logic for managing the params attribute
        for param in self.__class__.params:
            if param in params:
                param_value = params[param]
                if callable(param_value):
                    param_value = param_value()
            else:
                # if the param hasn't been overridden (passed as a keyword
                # argument inside **params) put the corresponding instance
                # value inside params.
                param_value = getattr(self, param, None)
            # make sure we don't pass a reference to mutables
            params[param] = copy_if_mutable(param_value)
        params["name"] = self.name
        params["value"] = to_unicode(self.adjust_value(value, **params))
        self.update_params(params)
        # update_data has been deprecated
        self.update_data(params)
        return view.engines.get("kid").transform(params, self.template_c)
Beispiel #4
0
    def display(self, value=None, **params):
        """Display the widget in a Kid template.

        Returns an elementtree node instance. If you need serialized output
        in a string, call 'render' instead.

        Probably you will not need to override or extend if inheriting from
        Widget.

        @params:

        value   : The value to display in the widget.
        **params: Extra parameters specific to the widget. All keyword params
                  supplied will pass through the update_params method which
                  will have a last chance to modify them before reaching the
                  template.

        """
        if not getattr(self, 'template_c', False):
            warnings.warn('kid' in view.engines
                and "Widget instance %r has no template defined" % self
                or "Trying to render a widget, but the Kid"
                    " templating engine is not installed or not yet loaded.")
            return None

        # logic for managing the params attribute
        for param in self.__class__.params:
            if param in params:
                param_value = params[param]
                if callable(param_value):
                    param_value = param_value()

            else:
                # if the param hasn't been overridden (passed as a keyword
                # argument inside **params) put the corresponding instance
                # value inside params.
                param_value = getattr(self, param, None)

            # make sure we don't pass a reference to mutables
            params[param] = copy_if_mutable(param_value)

        if not params.get('name'):
            params['name'] = self.name

        if value is None:
            value = self.default
            if callable(value):
                value = value()
        params['value'] = to_unicode(self.adjust_value(value, **params))

        self.update_params(params)

        try:
            transform = view.engines['kid'].transform
        except (KeyError, AttributeError):
            # this can happen if you render a widget before application startup
            # when view.load_engines() has not yet been called
            raise RuntimeError("Trying to render a widget, but the Kid"
                " templating engine is not installed or not yet loaded.")

        # If the page template is Genshi, we keep track of the nesting level,
        # because Genshi cannot display Kid's ElementTree elements directly.
        if request_available() and request.tg_template_enginename == 'genshi':
            display_level = getattr(request, 'tg_widgets_display_level', 0)
            request.tg_widgets_display_level = display_level + 1
        else:
            display_level = None

        try:
            output = transform(params, self.template_c)
            if display_level == 0:
                # On the topmost level, we create a Genshi markup stream
                # from Kid's ElementTree element to make Genshi really happy.
                # This automatism makes wrapping widget output with ET(...)
                # calls in Genshi page templates unnecessary.
                output = view.genshi_et(output)
        finally:
            if display_level is not None:
                request.tg_widgets_display_level = display_level

        return output