Certain presentation, like images and style sheets are not associated with any other component, so that one cannot create a view. To solve this problem, resources were developed, which are presentation components that do not require any context. This chapter will demonstrate how resources are created and registered with BlueBream.
The first goal is to register a simple plain-text file called resource.txt as a browser resource. The first step is to create this file anywhere you wish on the filesystem, and adding the following content. If you are working on the official tutorial, you can create the file at: src/tc/main/resource.txt:
Hello, I am a BlueBream Resource Component!
Now just register the resource in a ZCML configuration file using the browser resource directive. If you are working on the official tutorial, you can add these lines to src/tc/main/configure.zcml:
1 2 3 4
<browser:resource name="resource.txt" file="resource.txt" layer="default" />
Once you hook up the configuration file to the main configuration path and restart BlueBream, you should be able to access the resource now via a Browser at: http://localhost:8080/@@/resource.txt. The @@/ in the URL tells the traversal mechanism that the following object is a resource.
If you have an image resource, you might want to use different configuration. Create a simple image called img.png and register it as follows:
1 2 3 4
<browser:resource name="img.png" image="img.png" permission="zope.ManageContent" />
If you have many resource files to register, it can be very tedious to write a single directive for every resource. For this purpose the browser:resourceDirectory directive is provided, with which you can simply declare an entire directory, including its content as resources. Thereby the filenames of the files are reused as the names for the resource available. Assuming you put your two previous resources in a directory called resource, then you can use the following:
1 2 3 4
<browser:resourceDirectory name="resource" directory="resource" />
The image will then be publically available from the following URL: http://localhost:8080/@@/resources/img.png
The directory resource object uses a simple resource type recognition. It looks at the filename extensions to discover the type. For page templates, currently the extensions pt, zpt and html are registered and for an image gif, png and jpg. All other extensions are converted to file resources. Note that it is not necessary to have a list of all image types, since only Browser-displayable images must be recognized.
In BlueBream, there is a resource directory registered named static. If you are working the tutorial, you can see it at: src/tc/main/configure.zcml. At the beginning of the file, you can see the registration like this:
1 2 3 4
<browser:resourceDirectory name="static" directory="static" />
Two resource files (style.css and logo.png) will be already available inside src/tc/main/static directory.blog comments powered by Disqus