code reuse in rendering

21 views
Skip to first unread message

Terrence Brannon

unread,
Apr 23, 2015, 9:00:03 AM4/23/15
to nagare...@googlegroups.com
If HeaderOnly derives from AddParser then how can HeaderOnly use the
rendering method of AddParser prior to doing its own rendering?

I want the line `super(HeaderOnly, self).render(h, comp, args)` to work.

class AddParser(object):

    def __init__(self):
        self.name = ''
        self.parser_type = ''

    @property
    def factory(self):
        if self.parser_type == 'HeaderOnly':
            return HeaderOnly(self)

    def build_from(self, o):
        self.name = o.name
        self.parser_type = o.parser_type

@presentation.render_for(AddParser)
def render(self, h, comp, *args):

    h << "Parser Name: " << self.name

class HeaderOnly(AddParser):

    def __init__(self, o):
        self.build_from(o)

@presentation.render_for(HeaderOnly)
def render(self, h, comp, *args):

    super(HeaderOnly, self).render(h, comp, args)

    return h.root

Sylvain Prat

unread,
Apr 23, 2015, 9:58:40 AM4/23/15
to nagare...@googlegroups.com
Hi Terrence,

You can use the next_method feature from peak rules :

@presentation.render_for(HeaderOnly)
def render(next_method, self, h, comp, *args):
    # custom rendering code here
    next_method(self, h, comp, *args)  # default rendering
    # custom rendering code here
    return h.root

Cheers,
Sylvain

--
You received this message because you are subscribed to the Google Groups "Nagare users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nagare-users...@googlegroups.com.
To post to this group, send email to nagare...@googlegroups.com.
Visit this group at http://groups.google.com/group/nagare-users.
For more options, visit https://groups.google.com/d/optout.



--
Sylvain PRAT
+33 06 78 71 51 21

Bertrand Croq

unread,
Apr 23, 2015, 10:03:05 AM4/23/15
to nagare...@googlegroups.com

You may find other solutions in this blog entry: http://www.nagare.org/trac/blog/html-structuration-inheritance-aggregation

Bertrand CROQ
02 23 21 21 50
Chef de Projet

Net-ng
6, rue de Jouanet
35700 RENNES
http://www.net-ng.com

Terrence Brannon

unread,
Apr 23, 2015, 1:47:52 PM4/23/15
to nagare...@googlegroups.com, bertra...@net-ng.com


On Thursday, April 23, 2015 at 7:03:05 AM UTC-7, Bertrand Croq wrote:

You may find other solutions in this blog entry: http://www.nagare.org/trac/blog/html-structuration-inheritance-aggregation


I'm sorry, but I dont quite understand this code

class EntryTemplate(object):
    def __init__(self, entry):
        self.entry = entry
@render_for(EntryTemplate)
def EntryTemplate(self, h, *args):
    """
    Same code that we used in ``render_BaseEntry``
    but using ``self.entry`` instead of ``self``.
    """
    with h.div(class_='entry'):
        with h.div(class_='title'):
            h << self.entry.render(h, model='title')
        with h.div(class_='properties'):
            h << self.entry.render(h, model='properties')
    return h.root

1. Why does the decorated rendering method have the same name as the
class? 
2. Does the name of the decorated rendering method matter?
3. How does the slot `entry` in the plain Python object become a
component?  

Bertrand Croq

unread,
Apr 24, 2015, 4:17:15 AM4/24/15
to nagare...@googlegroups.com
Le jeudi 23 avril 2015, 10:47:52 Terrence Brannon a écrit :
>
> On Thursday, April 23, 2015 at 7:03:05 AM UTC-7, Bertrand Croq wrote:
> >
> > You may find other solutions in this blog entry:
> > http://www.nagare.org/trac/blog/html-structuration-inheritance-aggregation
> >
>
> I'm sorry, but I dont quite understand this code
>
> class EntryTemplate(object):
> def __init__(self, entry):
> self.entry = entry
> @render_for(EntryTemplate)
> def EntryTemplate(self, h, *args):
> """
> Same code that we used in ``render_BaseEntry``
> but using ``self.entry`` instead of ``self``.
> """
> with h.div(class_='entry'):
> with h.div(class_='title'):
> h << self.entry.render(h, model='title')
> with h.div(class_='properties'):
> h << self.entry.render(h, model='properties')
> return h.root
>
> 1. Why does the decorated rendering method have the same name as the
> class?

It is a typo. The rendering method should be named "render_entry_template".


> 2. Does the name of the decorated rendering method matter?

The name doesn't really matter but it is a good idea to give them a name that indicated what it renders. Personnaly, I now use this kind of convention:

@render_for(MyClass, 'this_model')
def render_my_class__this_model(...)


> 3. How does the slot `entry` in the plain Python object become a
> component?

In the "render_SimpleEntry_header" method you can see that EntryTemplate receives a Component instance because we use "EntryTemplate(comp)" instead of "EntryTemplate(self)"


Regards,
--
Bertrand CROQ
02 23 21 21 50

Yann G

unread,
Apr 24, 2015, 5:45:43 AM4/24/15
to nagare...@googlegroups.com, bertra...@net-ng.com
Hi all,


Le vendredi 24 avril 2015 10:17:15 UTC+2, Bertrand Croq a écrit :
Le jeudi 23 avril 2015, 10:47:52 Terrence Brannon a écrit :
>
> On Thursday, April 23, 2015 at 7:03:05 AM UTC-7, Bertrand Croq wrote:
> >
> > You may find other solutions in this blog entry:
> > http://www.nagare.org/trac/blog/html-structuration-inheritance-aggregation

A warning though:

Creating ``Component`` instances in views is discouraged now since callbacks on the components created this way won't be retained.
I don't think we should keep the blog post as is, i.e. with this practice.
@nagare-users: what do you think?

 

Terrence Brannon

unread,
Apr 24, 2015, 2:39:46 PM4/24/15
to nagare...@googlegroups.com
Well, reuse has been achieved. Relevant code follows:

class ValueTyped(object):
    def __init__(self):
        super(ValueTyped, self).__init__()
        self.value_type = ''

    def valuetyped_render(self, h, e):
        id_type = {
            '25': 'Timestamp with Jiffies',
            '26': 'Timestamp',
            '27': 'nums',
            '28': 'alphanums',
            '29': 'Any',
            '30': 'Single Word',
            '31': 'Time',
            '32': 'Date'
        }

        with h.select.action(e.value_type):
            with h.optgroup(label='xxx'):
                for id,type in id_type.iteritems():
                    h << h.option(type, value=type)


        with h.div:
            h << "Value Type: "
            self.target.valuetyped_render(h, self)
Reply all
Reply to author
Forward
0 new messages