Skip to content

Instantly share code, notes, and snippets.

@minskmaz
Created February 12, 2019 05:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save minskmaz/7299838cb296dd4395905602a1b55eb0 to your computer and use it in GitHub Desktop.
Save minskmaz/7299838cb296dd4395905602a1b55eb0 to your computer and use it in GitHub Desktop.
Zope Component getUtility call from Jinja2 template (whoa!)
# 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