Jonno_FTW
Jonno_FTW

Reputation: 8809

Python: Accessing Renderer Global in Pyramid

I want to access GMAPS_API_KEY renderer global from my mako template in pyramid. I keep getting the error that GMAPS_API_KEY is undefined:

 ...
 File "views/header.html", line 10, in render_body
    <script src="https://maps.googleapis.com/maps/api/js?key=${GMAPS_API_KEY}&sensor=true"></script>
  File "/scratch/temp/venvs/py2.7.10/lib/python2.7/site-packages/mako/runtime.py", line 226, in __str__
    raise NameError("Undefined")
NameError: Undefined

I have 3 files (I've omitted a lot of stuff):

header.html:

 <script src="https://maps.googleapis.com/maps/api/js?key=${GMAPS_API_KEY}&sensor=true"></script> <!-- ERROR HERE -->

map.mak:

<%include file="header.html"/>

webapp.py

from pyramid.events import subscriber
from pyramid.events import BeforeRender

@subscriber(BeforeRender)
def add_global(event):
    event['GMAPS_API_KEY'] = get_gmaps_key()

def show_map(request):
    return render_to_response('views/map.mak', {},request=request)

config = Configurator()
config.include('pyramid_mako')
config.add_route('map', '/')
config.add_view(show_map, route_name='map')
app = config.make_wsgi_app()
host, port = '127.0.0.1', 8080
server = make_server(host, port, app)
server.serve_forever()

Upvotes: 0

Views: 117

Answers (1)

X-Istence
X-Istence

Reputation: 16667

@subscriber requires that you call config.scan() on the package that contains the line @subscriber, or you need to replace your decorator with a call to config.add_subscriber() as seen in the documentation for the Pyramid configurator: http://docs.pylonsproject.org/projects/pyramid//en/latest/api/config.html#pyramid.config.Configurator.add_subscriber

Upvotes: 1

Related Questions