views:

21

answers:

1

I have a base.mako template with a if statement to include or not jQuery

<head>
% if getattr(c, 'includeJQuery', False):
    <script type="text/javascript" src="jquery.js"></script>
% endif
...

Several templates inherit from base.mako, someone needs jQuery, someone don't.

At the moment I have to set the attribute in the controller before calling render

c.includeJQuery = True
return render('/jQueryTemplate.mako')

but I think this should go directly in child template (i.e. jQueryTemplate.mako)

I tried adding it before inherit

<% c.includeJQuery = True %>
<%inherit file="/base.mako"/>\ 

but it does not work.

Any tips?

Thanks for your support

+1  A: 

Well, since with the line

<script type="text/javascript" src="jquery.js"></script>

I also need to add some other js I put a jQueryScript %def in child template

##jQueryTemplate.mako
<%def name="jQueryScript()">
    <script>
    </script>
</%def>

then in base I check if exists and add all accordingly

#base.mako
%if hasattr(next, 'jQueryScript'):
    <script type="text/javascript" src="/js/jquery-1.4.2.min.js"></script>
    ${next.jQueryScript()}
%endif

so I don't need to set nothing in the controller.

neurino