old blog,

Integrating hurry.resource into BFG

Izhar Firdaus Izhar Firdaus Follow Support Jun 19, 2010 · 2 mins read
Integrating hurry.resource into BFG
Creative Commons License
This work is licensed under a Creative Commons Attribution 4.0 International License.
Back in PyCon APAC last week I was introduced to hurry.resource by its author, which is also one of the founder behind Grok, Martijn Faassen.

So, what does it do?.

Read Here

Lets go to the fun part

Setting Up

Assuming you are using one of the default BFG templates

First, enable ZCA. (or just do this in run.py):
...
config = Configurator(root_factory=get_root, settings=settings)
config.hook_zca()
config.begin()
...


Do this little modification on the get_root function in model.py:
from repoze.bfg.interfaces import IRequest

def get_root(request):
request.registry.registerUtility(request,provided=IRequest)
return root


Add some component codes, name the file resource.py
from hurry.resource.interfaces import ICurrentNeededInclusions
from hurry.resource.interfaces import ILibraryUrl
from zope.interface import implements,implementer
from zope.component import getUtility,adapter
from hurry.resource import NeededInclusions
from repoze.bfg.interfaces import IRequest
from hurry.resource.core import Library

class NeededInclusion(object):
implements(ICurrentNeededInclusions)

def __call__(self):
request = getUtility(IRequest)
if not hasattr(request,'needed'):
request.needed = NeededInclusions()
return request.needed

@implementer(ILibraryUrl)
@adapter(Library)
def LibraryUrl(library):
request = getUtility(IRequest)
return "%s/hurry/%s" % (request.application_url,library.name)


and hook them into ZCA by adding these into configure.zcml
<utility factory=".resource.NeededInclusion"/>
<adapter factory=".resource.LibraryUrl"/>


Adding into template
In your main template head section, add:
<tal:cond condition="getattr(request,'needed',None)">
<tal:res replace="structure request.needed.render()"/>
</tal:cond>


Using it

Lets take hurry.jquery for example:

First, register the static files, add these into configure.zcml
<static
name="hurry/jquery"
path="hurry.jquery:jquery-build"
/>


If you want to include Jquery js into your current view, just do this in the view code:
from hurry.jquery import jquery
def my_view(request):
jquery.need()
return {'project':'helloworld'}


If you did the stuff above correctly, with luck , your view should have this in place of the tal block.

<script type="text/javascript" 
src="http://localhost:6543/hurry/jquery/jquery-1.4.2.js"></script>


Custom Resource Libraries

You can create your own custom resource libraries and share/reuse it. To create your own resource, read the documentation of hurry.resource

Considering hurry.resource is also used in Grok and probably several other ZCA based frameworks, your resource library can also be reused on these frameworks with little to no modifications.

Credits to Martijn for this awesome component.

Happy hacking :D
Written by Izhar Firdaus Follow Support
I'm a system architect, data engineer and developer advocate with passion in Free / Open Source software, entrepreneurship, community building, education and martial art. I take enjoyment in bridging and bringing together different FOSS technologies to help businesses and organizations utilize IT infrastructure to aid and optimize their business and organizational process.

"They'll realize that they're different."

Quote from JDrama CHANGE. Episode 5Keita Asakura I'm sorry for being arrogant. I used to be a grade school teacher. Last ye...

In old blog, Jun 03, 2010

« Previous Post

Unifi 10

Kuala Lumpur, MYWashington, USTokyo, JPPerth, AU- Izhar Firdaus -

In old blog, Sep 20, 2010

Next Post »