Presentation with templating

31 views
Skip to first unread message

Bahman Movaqar

unread,
May 19, 2013, 12:34:37 AM5/19/13
to nagare...@googlegroups.com
After looking over the tutorial and samples, it appears to me that presentation is done by creating HTML with Python or using meld. I don't know meld but going through PresentationTier, it looks to me like templating is done at page-level (please correct me if I'm wrong). This, IMHO, contrasts with the clever component-model of Nagare.

Is there any way to define a component along with its XHTML snippet without knowing/caring about the whole page design?

For example consider the following pseudo-code:

# component1.py
class Component1:
       def __init__(self):
            self.value1 = 10

@presentation.render_for(Component1)
def render(comp, h, *args):
       #  set value for 'placeholder1'
       template.get("placeholder1").set(comp.value1)

# component1.xhtml

<div id="placeholder1"></div>


And then package component1.py and component1.xhtml together as one re-usable unit.

Is it possible?  Am I getting something wrong?

TIA,
--
Bahman

Alain Poirier

unread,
May 19, 2013, 5:13:12 PM5/19/13
to nagare...@googlegroups.com
Le 19 mai 2013 à 06:34, Bahman Movaqar <b.mo...@gmail.com> a écrit :

> After looking over the tutorial and samples, it appears to me that presentation is done by creating HTML with Python or using meld. I don't know meld but going through PresentationTier, it looks to me like templating is done at page-level (please correct me if I'm wrong). This, IMHO, contrasts with the clever component-model of Nagare.
>
> Is there any way to define a component along with its XHTML snippet without knowing/caring about the whole page design?

Yes, it's possible at the component level. In fact, it's just how we use meld templating.

For example, here is now the `counter` component with a meld view:

# --
# File: counter.py
import os
from nagare import presentation

class Counter(object):
def __init__(self):
self.val = 0

def increase(self):
self.val += 1

def decrease(self):
self.val -= 1


@presentation.render_for(Counter)
def render(counter, h, comp, *args):
# 1. Parse the template into a DOM
filename = os.path.join(os.path.dirname(__file__), 'counter.xml')
template = h.parse_html(filename, xhtml=True)

# 2. Dynamize the DOM
template.findmeld('value').fill(counter.val)
template.findmeld('decrease').action(counter.decrease)
template.findmeld('increase').action(counter.increase)

# 3. Return the DOM
return template

# --
# File: counter.xml
<div xmlns:meld="http://www.plope.com/software/meld3">
<div meld:id='value'>0</div>
<a meld:id='decrease'>--</a> | <a meld:id='increase'>++</a>
</div>


Exactly like the pseudo-code you wanted:

> For example consider the following pseudo-code:
>
> # component1.py
> class Component1:
> def __init__(self):
> self.value1 = 10
>
> @presentation.render_for(Component1)
> def render(comp, h, *args):
> # set value for 'placeholder1'
> template.get("placeholder1").set(comp.value1)
>
> # component1.xhtml
> <div id="placeholder1"></div>
>
> And then package component1.py and component1.xhtml together as one re-usable unit.
>
> Is it possible? Am I getting something wrong?
>
> TIA,
> --
> Bahman

Best regards,
Alain

Bahman Movaqar

unread,
May 19, 2013, 11:28:32 PM5/19/13
to nagare...@googlegroups.com
Excellent! Thanks.

--
Bahman



--
You received this message because you are subscribed to a topic in the Google Groups "Nagare users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/nagare-users/di-W4-9N7yk/unsubscribe?hl=en.
To unsubscribe from this group and all its topics, 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?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.





--
Bahman Movaqar (http://BahmanM.com)
ERP Evaluation, Implementation and Deployment Consultant

Bahman Movaqar

unread,
May 19, 2013, 11:32:25 PM5/19/13
to nagare...@googlegroups.com
And is the packaging possible? What if I need to re-use such a component from a project in another project? Can I make that component a normal python package (along with its template) so I can easily and without knowing the details plug it into my web page?

TIA,
--
Bahman

apoirier

unread,
May 31, 2013, 12:19:43 PM5/31/13
to nagare...@googlegroups.com
Le lundi 20 mai 2013 05:32:25 UTC+2, Bahman Movaqar a écrit :
And is the packaging possible? What if I need to re-use such a component from a project in another project? Can I make that component a normal python package (along with its template) so I can easily and without knowing the details plug it into my web page?

Yes, an "application" package can be of any granularity, from a single component to a full featured application.

For example, at this end of the tutorial you can make a package:

  python setup.py sdist

The package 'tutorial-0.0.1.tar.gz' is created in the 'dist' directory.

Someone else can then install it:

  easy_install .../tutorial-0.0.1.tar.gz   (or simply 'easy_install tutorial' if it's uploaded on PyPi)

and then, in his code, he can uses your 'Counter' component:

    from tutorial.app import Counter

    ...
    counter = component.Component(Counter())
    ...
 
(of course, for a real component/application sent to PyPi, it's better to create a package namespace. i.e 'bahmancops.counter' instead of 'tutorial')

Best regards,
Alain
Reply all
Reply to author
Forward
0 new messages