Importing a Mako file and using its variables/functions

122 views
Skip to first unread message

EJelome

unread,
Sep 20, 2012, 9:38:42 PM9/20/12
to blogofil...@googlegroups.com
Hello everyone,

I'm having problems when using the import method to use a function declared from another mako file. For example:

I'll create a helper file which contains possible list of functions to be used (in WordPress, this is the functions.php):
## filename: helper.mako
<%
def say_hi(name):
    return 'Hi', name
%>
Then I would just want to simply use it to my current mako file (e.g., site.mako):
<%! import helper %>
${helper.say_hi('Blogofile')}

But I'm getting this error:
ImportError: No module named helper

They are in the same directory so I don't know why it can't use the function. I also tried the example given in the Mako docs:

<%namespace name="func" module="helper" />
${func.say_hi('Blogofile')}
... but it still doesn't work. Also tried changing the extension of .mako to .py and changed the code thereof to pure python code, however, still receiving the same error.

Can anyone shed a light for this? It will be a hassle if I'll put the same code on different mako files that needs them.
 

EJelome

unread,
Sep 22, 2012, 1:06:28 PM9/22/12
to blogofil...@googlegroups.com
Finally, it seems that using variables and/or def (functions) outside the <%def></%def> blocks are not recognizable to other templates (which causes the error). 

For example:

This one causes an error:
## filename: helper.mako
<%
def say_hi(name):
    return 'Hi', name
%>
## filename: site.mako
<%namespace name="ej" file="helper.mako" />
${ej.say_hi(name='Blogofile')} ## Causes an error

And this one works:
## filename: helper.mako 
<%def name="say_hi(name)">
    <% return 'Hi', name %>
</%def>
## filename: site.mako
<%namespace name="ej" file="helper.mako" />
${ej.say_hi(name='Blogofile')} ## Displays: Hi, Blogofile

Doug Latornell

unread,
Sep 22, 2012, 2:04:00 PM9/22/12
to blogofil...@googlegroups.com, EJelome
On Sat, Sep 22, 2012 at 10:06 AM, EJelome <eje...@gmail.com> wrote:
Finally, it seems that using variables and/or def (functions) outside the <%def></%def> blocks are not recognizable to other templates (which causes the error). 

That sounds right. Mako is a template library that happens to allow Python code to be embedded. Many template libraries (e.g. Django, Jinja2) don't allow that. I'm not surprised that you can't access Python functions embedded in Mako templates outside of the template they are embedded in.

I tend to think of embedding Python in a template as a technique of last resort. In web apps that means doing the calculations in view/controller functions/classes (terminology and methodology vary from one framework to another) and putting resulting Python data objects (strings, lists, dicts, etc.) into the context that the template engine uses during rendering. In blogofile that probably translates to doing the calculations in controllers and/or filters. Out of the box blogofile and blogofile_blog provide controllers, filters and templates to do a collection of bloggish and other static site things, but for specialized purposes that go beyond that you probably need to implement your own. Have a look at the blogofile.com source [1] for some examples of custom controllers and filters, and at Mike Bayer's blogofile hacks post [2] for another example of a custom filter.

[1] https://github.com/EnigmaCurry/blogofile.com
[2] http://techspot.zzzeek.org/2010/12/06/my-blogofile-hacks/

Doug
 

For example:

This one causes an error:
## filename: helper.mako
<%
def say_hi(name):
    return 'Hi', name
%>
## filename: site.mako
<%namespace name="ej" file="helper.mako" />
${ej.say_hi(name='Blogofile')} ## Causes an error

And this one works:
## filename: helper.mako 
<%def name="say_hi(name)">
    <% return 'Hi', name %>
</%def>
## filename: site.mako
<%namespace name="ej" file="helper.mako" />
${ej.say_hi(name='Blogofile')} ## Displays: Hi, Blogofile

--
You received this message because you are subscribed to the Google Groups "blogofile-discuss" group.
To view this discussion on the web visit https://groups.google.com/d/msg/blogofile-discuss/-/ra1WGbYQOrAJ.

To post to this group, send email to blogofil...@googlegroups.com.
To unsubscribe from this group, send email to blogofile-disc...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/blogofile-discuss?hl=en.

EJelome

unread,
Sep 22, 2012, 10:54:03 PM9/22/12
to blogofil...@googlegroups.com, EJelome, d...@douglatornell.ca
Thanks Doug!

I'll keep that in mind. I will hack in the controllers and filters once I finish the templates to have a better understanding with them. Then I can probably separate the necessary controllers from the views (if my understanding is correct, views are the ones to be displayed, while controller are the logic, like functions---correct me if I'm wrong).

Kind regards,
EJelome
Reply all
Reply to author
Forward
0 new messages