shpaml + pyramid + mako

12 views
Skip to first unread message

Erez

unread,
Feb 13, 2011, 11:02:30 AM2/13/11
to shpaml
I'm starting a project on pyramid and I wanted to use shpaml to
automatically convert all of my templates to html on runtime.

It's a bit slower than static compilation, of course, but
1) shpaml's operation is potentially very fast (if it isn't already)
2) a good webserver will cache its pages anyway
3) It's so much easier to work with

My solution is undoubtedly a bit hackish, but I figured I'd share it
and maybe it will inspire someone to post a better solution.

In my project's __init__.py, I added these lines:

import shpaml
from pyramid import mako_templating

class ShpamlRenderer(object):
def __init__(self, info):
self.makoRenderer = mako_templating.renderer_factory(info)

def __call__(self, value, system):
result = self.makoRenderer(value, system)
return shpaml.convert_text(result)

and in the main() function, I added:
....
config.add_renderer(name=".mako", factory=ShpamlRenderer)
....

Now all mako templates pass through the shpaml preprocessor!
Here's an example:

template.mako:
----------------------
html
body
%for row in range(4):
.link
a.pretty_link href="${row}" | link${row}
%endfor

---- turns into ----->>

<html>
<body>
<div class="link">
<a href="0" class="pretty_link">link0</a>
</div>
<div class="link">
<a href="1" class="pretty_link">link1</a>
</div>
<div class="link">
<a href="2" class="pretty_link">link2</a>
</div>
<div class="link">
<a href="3" class="pretty_link">link3</a>
</div>
</body>
</html>

Steve Howell

unread,
Feb 13, 2011, 11:39:59 AM2/13/11
to shp...@googlegroups.com
This is an interesting take on using shpaml. If I'm reading this correctly, you
first render through mako, which creates shpaml, then you convert the shpaml to
html.

I've mostly seen the reverse approach. For example, I convert shpaml to django,
then render the django. This makes sense in the django world, because it's
pretty easy for shpaml to detect parts of the document that are django and just
leave them alone. With mako, that's not so easy, as mako allows for more use of
inline Python blocks. So I think your approach makes sense.

Shpaml's pretty fast, because it doesn't do a whole heck of a lot. On my Mac I
see that it can process about 20,000 lines per second. (It's a 2.66 GHz Intel
Core 2 Duo.)

机械唯物主义linjunhalida

unread,
Feb 16, 2011, 8:16:35 PM2/16/11
to shpaml
hello, I found there is an issue:

when I use inheritance, I write base.mako:

html
body
wrap
${self.wrap()}

and view.mako:

<%inherit file="base.mako"/>

<%def name="wrap()">
p | sth
</%def>

I need make current indentation, and it drive me mad, any way to solve
this?

机械唯物主义 : linjunhalida

unread,
Feb 16, 2011, 8:57:23 PM2/16/11
to shpaml
sorry, thread broken, it is followed by:
http://groups.google.com/group/shpaml/browse_thread/thread/51ce1380b2f2882f

2011/2/17 机械唯物主义linjunhalida <linjun...@gmail.com>:

机械唯物主义linjunhalida

unread,
Feb 16, 2011, 9:21:51 PM2/16/11
to shpaml
and there is another issue:

when I need import some text in mako like:
<textarea name="body" rows="10" cols="60">
${page.data}
</textarea>

shpaml's grammar don't have block text quote...

so looks like text -> shpaml -> mako is the better solution?

Steve Howell

unread,
Feb 17, 2011, 12:46:57 AM2/17/11
to shp...@googlegroups.com

----- Original Message ----
> From: 机械唯物主义linjunhalida <linjun...@gmail.com>
> To: shpaml <shp...@googlegroups.com>
> Sent: Wed, February 16, 2011 6:21:51 PM
> Subject: Re: shpaml + pyramid + mako
>
> and there is another issue:
>
> when I need import some text in mako like:
> <textarea name="body" rows="10" cols="60">
> ${page.data}
> </textarea>
>
> shpaml's grammar don't have block text quote...
>
> so looks like text -> shpaml -> mako is the better solution?
>


It probably make sense to do it that way.

Later versions of shpaml support the "VERBATIM" keyword, which might solve your
problem:

import shpaml

test = '''
blockquote VERBATIM
Here we are with
some text that is indented, and
we want
sphaml to just let it pass through

p
Back to the normal mode of shpaml.

'''
print shpaml.convert_text(test)

---

<blockquote>
Here we are with
some text that is indented, and
we want
sphaml to just let it pass through
</blockquote>

<p>
Back to the normal mode of shpaml.
</p>

Erez

unread,
Feb 17, 2011, 9:54:08 AM2/17/11
to shpaml
Changing the order of evaluation **isn't** going to solve the first
issue, because shpaml won't run on base.mako.
(and it can't do so without parsing and understanding mako)

If ${self.wrap()} doesn't insert code in the right indentation,
perhaps prefixing it with a nop character can help?
(like: @ ${self.wrap()} )
Otherwise, perhaps a new shpaml construct could be introduced for such
situations.
For example:
html
body
wrap
!BLOCK
div
p | bla
!END
in which the contents of the block will be "indented" to reside in
wrap

I say "indented" because if BLOCK only pretends to indent, without
actually doing so (which is faster),
it could solve your (second) issue as well.

机械唯物主义 : linjunhalida

unread,
Feb 17, 2011, 10:43:19 AM2/17/11
to shp...@googlegroups.com
thanks, and I'm using static rending way to do this now.
and for the first question, I'm checking how to rending shpaml before
send file to mako....

2011/2/17 Erez <ere...@gmail.com>:

Reply all
Reply to author
Forward
0 new messages