Created
February 12, 2019 05:31
-
-
Save minskmaz/7299838cb296dd4395905602a1b55eb0 to your computer and use it in GitHub Desktop.
Zope Component getUtility call from Jinja2 template (whoa!)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Zope Component Utilities are perfect for injecting up-to-date state/config from far away regions of your app | |
# To do so just don't __call__() the utility when you register it - register it as a method only but call it from your template | |
# Make sure to pass in your interface and utility function (zope.component.getUtility) to jinja - then profit! | |
'mytld.com':{ | |
'config':{ | |
'tld':'mytld.com', | |
'iface':IFWUtils, | |
'utilfunc':getUtility | |
} | |
} | |
### | |
{% with | |
tld=config.get('tld'), | |
IFWUtils=config.get('iface'), | |
getUtility=config.get('utilfunc') %} | |
{% for url in getUtility(IFWUtils, 'get_scripts_by_host')(tld) %} | |
<script src="{{ url }}" type="text/javascript"></script> | |
{% endfor %} | |
{% endwith %} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment