Examples of passing dictionary

530 views
Skip to first unread message

Srikanth Bemineni

unread,
Mar 18, 2016, 8:25:07 PM3/18/16
to Mako Templates for Python

Hi ,

I have been struggling for good examples on how to pass dictionary to a def. None of the MAKO documentation shows how to pass dictionary values to defs or blocks

<%def name="form(action,method,formoptions)">
    <%
      attributeset = ""
      attributeset += 'action="' + action.strip() +'" '
      attributeset += 'method="' + method.strip() +'" '
      for (key,value) in formoptions.items():
          attributeset += key.strip() + '="' + value.strip() + '" '
    %>

    //some cool typography stuff
    <form ${attributeset}>
        ${caller.body()}
    </form>
</%def>


In other file.
<%namespace name="fh" file="form.mako"/>


<%fh:form action="something", method="POST" , formactions={????}>
</%fh:form>

what are different ways to pass a dictionary ?

Srikanth Bemineni

Jonathan Vanasco

unread,
Mar 19, 2016, 12:42:10 PM3/19/16
to Mako Templates for Python
http://docs.makotemplates.org/en/latest/namespaces.html#ways-to-call-namespaces

"When using tags, the values of the arguments are taken as literal strings by default. To embed Python expressions as arguments, use the embedded expression format"


If you need to access caller, you can all use the `call` format `<%call`

If you don't, I prefer the expression format `${fh.form(vars)}`

Srikanth Bemineni

unread,
Mar 19, 2016, 1:15:13 PM3/19/16
to Mako Templates for Python
I even tried the embedded expression format, but it still throws an error

mako.exceptions.SyntaxException: (SyntaxError) keyword can't be an expression (<unknown>, line 1) ("dict('class'='dfdf')") in file

<%fh:form action="something", method="POST" , formactions="${dict('class'="dfdf')}">
</%fh:form>

Srikanth 

Joel Rivera

unread,
Mar 19, 2016, 2:31:39 PM3/19/16
to mako-d...@googlegroups.com
"class" is a python keyword. You can't use it like this.

this is an invalid python expression:

dict(class="sample")

if you want to have a "class" key on a dictionary
you can use a literal dict notation:

{'class': 'sample'}

or use another name for the key:

dict(class_="sample")

As far as I know, you don't really need to do anything special to pass
a dictionary to a function. Is just another object that gets passed to
the function.
--
Rivera²

Srikanth Bemineni

unread,
Mar 19, 2016, 4:39:35 PM3/19/16
to Mako Templates for Python
Hi,

I am using the class as string, so it should not conflict with the python key word. The only way I was able to make it work is using the call

<%call expr="fh.form('hello','POST',{'class':'form-class'})">
</%call>

I tried multiple ways using the tag method, it doesn't work.

Srikanth 

On Friday, March 18, 2016 at 7:25:07 PM UTC-5, Srikanth Bemineni wrote:

Joel Rivera

unread,
Mar 19, 2016, 4:51:22 PM3/19/16
to mako-d...@googlegroups.com
Alright then... as an exercise open a python repl and do:

dict('class'=1)

or:

dict(class=1)

or:

{class: "test"}

(hint: none of those work)

You probably are not familiar with the concept of *keywords* on the
function parameters.

Anyway, I'm glad that the literal dictionary syntax worked (as
expected):

literal dictionary syntax:

{'class': 'form-class'}


As a general recommendation I would advice you to consider if the
syntax is valid python before considering mako.

Regards.
--
Rivera²

Srikanth Bemineni

unread,
Mar 19, 2016, 8:49:00 PM3/19/16
to Mako Templates for Python
Hi,

I think we are deviating from the main issue. Lets use any other dictionary {'cxx':'sdfdf'}

<%fh:form action="something", method="POST" , formactions="${dict('cxx'='sdfdf')}">
</%fh:form>

but the error remains the same 

mako.exceptions.SyntaxException: (SyntaxError) keyword can't be an expression (<unknown>, line 1) ("dict('cxx'='sdfdf')") in file

Srikanth

On Friday, March 18, 2016 at 7:25:07 PM UTC-5, Srikanth Bemineni wrote:

Joel Rivera

unread,
Mar 19, 2016, 9:29:23 PM3/19/16
to mako-d...@googlegroups.com
How about this:

dict(cxx='sdfdf')


El 19 de marzo de 2016 6:48:59 PM CST, Srikanth Bemineni <bemineni...@gmail.com> escribió:
Hi,

I think we are deviating from the main issue. Lets use any other dictionary {'cxx':'sdfdf'}

<%fh:form action="something", method="POST" , formactions="${dict('cxx'='sdfdf')}">
</%fh:form>

but the error remains the same 

mako.exceptions.SyntaxException: (SyntaxError) keyword can't be an expression (<unknown>, line 1) ("dict('cxx'='sdfdf')") in file

Srikanth
< br />
On Friday, March 18, 2016 at 7:25:07 PM UTC-5, Srikanth Bemineni wrote:

Hi ,

I have been struggling for good examples on how to pass dictionary to a def. None of the MAKO documentation shows how to pass dictionary values to defs or blocks

<%def name="form(action,method,formoptions)">
    <%
      attributeset = ""
      attributeset += 'action="' + action.strip() +'" '
      attributeset += 'method="' + method.strip() +'" '
      for (key,value) in formoptions.items():
          attributeset += key.strip() + '="' + value.strip() + '" '
    %>

    //some cool typography stuff
    <form ${attributeset}>        ${caller.body()}
    </form>
</%def>


In other file.
<%namespace name="fh" file="form.mako"/>


<%fh:form action="something", method="POST" , formactions={????}>
</%fh:form>

what are different ways to pass a dictionary ?

Srikanth Bemineni


--
Sent from my Android device with K-9 Mail. Please excuse my brevity.

Jonathan Vanasco

unread,
Mar 20, 2016, 2:25:59 AM3/20/16
to Mako Templates for Python


On Saturday, March 19, 2016 at 8:49:00 PM UTC-4, Srikanth Bemineni wrote:
I think we are deviating from the main issue. Lets use any other dictionary {'cxx':'sdfdf'}

<%fh:form action="something", method="POST" , formactions="${dict('cxx'='sdfdf')}">
</%fh:form>


This is not valid Python:

    dict('cxx'='sdfdf')

Python docs illustrate valid ways to create a dictionary here:


 

Srikanth Bemineni

unread,
Mar 20, 2016, 4:26:42 PM3/20/16
to Mako Templates for Python
Thanks for the input. I just went through all the scenarios, and I am listing her what works and which will not work. I am still little bit mystified by the syntax of Mako and python integration.


<%fh:form action="something", method="POST" , formactions="${dict('cxx'='sdfdf')}">
</%fh:form>

a = dict(one=1, two=2, three=3)
>>> b = {'one': 1, 'two': 2, 'three': 3}
>>> c = dict(zip(['one', 'two', 'three'], [1, 2, 3]))
>>> d = dict([('two', 2), ('one', 1), ('three', 3)])
>>> e = dict({'three': 3, 'one': 1, 'two': 2})


a -> works
b -> doesn't work
c-> works
d -> works
e -> doesn't work.

Anything things with flower brackets doesn't work. Is there a character in MAKO that nullifies the flower brackets, during expression evaluation.?

Srikanth Bemineni

Jonathan Vanasco

unread,
Mar 21, 2016, 12:09:08 PM3/21/16
to Mako Templates for Python
ah,. It turns out this is known bug that is not likely to be addressed (this would require a lot of rewriting, and you can just declare dict in a python block)



Reply all
Reply to author
Forward
0 new messages