Re: [bottlepy] include macro defs in simple template

94 views
Skip to first unread message

bender

unread,
Nov 6, 2012, 1:41:51 PM11/6/12
to bott...@googlegroups.com

Have you tried %Defs.mymacro() ?

On Nov 6, 2012 10:06 PM, "Michael P" <michael...@gmail.com> wrote:
I would like to include a bunch of macro defs in a simple template but keep getting server error 500 if I try including the macro defs from a separate file.  If I just paste the defs file into the beginning of the render template everything works fine.  What am I doing wrong? 


Here are simplified versions of the base, render and defs templates. (I have tried a bunch of variants of the include statement eg  %include Defs mymacro=mymacro and get the same outcome)

Base.tpl:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>{{Title}}</title>
</head>
<body>
%include
</body>
</html>

Render.tpl

%rebase Base Title=Title
%include Defs
%mymacro()
<div>
Hello World

Defs.tpl

%def mymacro():
<h1> Hello World <h2>
%end


--
You are member of the "bottlepy" group at google groups.
See http://groups.google.de/group/bottlepy for mailing list options.
See http://bottlepy.org/ for news and documentation.

Michael P

unread,
Nov 6, 2012, 3:54:38 PM11/6/12
to bott...@googlegroups.com, ben...@rodriges.org
Thanks for this suggestion.  I hadn't tried this.

Unfortunately prepending "Defs." to the mymacro call in "Render" still gives the 500 error -- albiet it tells me:

  NameError: name 'Defs' is not defined

instead of mymacro

Marcel Hellkamp

unread,
Nov 7, 2012, 4:48:13 AM11/7/12
to bott...@googlegroups.com
On 11/06/2012 07:05 PM, Michael P wrote:
> I would like to include a bunch of macro defs in a simple template but
> keep getting server error 500 if I try including the macro defs from a
> separate file. If I just paste the defs file into the beginning of
> the render template everything works fine. What am I doing wrong?

The namespace of an included template is stored in '_'. You cannot know
this, its an documented implementation detail. Looks like we have to
think for a better solution soon.

Example:

% include Defs
% defs = _
% mymacro = defs['mymacro']

bender

unread,
Nov 7, 2012, 4:51:07 AM11/7/12
to bott...@googlegroups.com
Wow, Marcel, this really need to be added into Recipes section on bottlepy.org
> --
> You are member of the "bottlepy" group at google groups.
> See http://groups.google.de/group/bottlepy for mailing list options.
> See http://bottlepy.org/ for news and documentation.



--
Не без уважения, Георгий (aka bender.rodriges) @ http://rodriges.org

Michael P

unread,
Nov 7, 2012, 3:59:43 PM11/7/12
to bott...@googlegroups.com
This works!  Thank you.  I'll add just one final (albeit obvious) detail.  For multiple macros, to be used do the following


% include Defs
% defs = _
% first_macro = defs['first_macro']
% second_macro = defs['second_macro']

Michael

PS Thank you Marcel for conceiving, writing, and supporting the elegant, concise, and empowering "Bottle".  Truly the Python framework which will launch a thousand websites!

pgoetz

unread,
Apr 5, 2013, 6:01:58 PM4/5/13
to bott...@googlegroups.com
I don't understand how this can work.  What if you have more than one %include statement -- where is the namespace of the second included template stored?

Michael Prisant

unread,
Apr 5, 2013, 6:28:01 PM4/5/13
to bott...@googlegroups.com
I was a little bit surprised to see this in my inbox -- I haven't worked with my bottle code in a little while.  So bear with me as I attempt to provide an answer

Defs refers to a template file Defs.tpl which is stored in the same views directory. In my case I collected several functions or macros in my Defs.tpl file.

I am not sure how to answer the question regarding multiple includes and potential namespace conflicts.

The posted snippet simplified example really captures the essence of this better than my actual code which is pretty lengthy.  In any case, my code works for me.  I really think the snippet should be sufficient to begin coding "toy" examples.

Hope this helps -- I remember being pretty frustrated with this issue.

Michael

PS  I haven't updated my Bottle version since coding this

--
--
You are member of the "bottlepy" group at google groups.
See http://groups.google.de/group/bottlepy for mailing list options.
See http://bottlepy.org/ for news and documentation.
 
---
You received this message because you are subscribed to the Google Groups "bottlepy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bottlepy+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



pgoetz

unread,
Apr 8, 2013, 11:52:32 AM4/8/13
to bott...@googlegroups.com
Thanks for responding.  My question was specifically about how one could have more than one %include (I checked the documentation again, and didn't see anything limiting the number of includes), given the design decision which maps the namespace to _.  I'm still confused/frustrated by some of the finer points of using bottle, and this is one of them.
Reply all
Reply to author
Forward
0 new messages