Rewriting URLs with XDV

321 views Asked by At

We are using static HTML files as theme for our Plone 4 site with collective.xdv.

The static HTML files themselves are openable in a web browser making the theming process easy for the theme authors.

However, theme files use a file system resource directores which are referred in HTML like

  <link rel="stylesheet" type="text/css" href="../css/jquery/accordion.css"/>

How it could be possible to rewrite these to be absolute URL when served through Plone, with a custom prefix? (Can it be done in rules.xml??

E.g. translate

  ../images/logo.gif

To

  http://portal_url/images/logo.gif
2

There are 2 answers

1
Rigel Di Scala On

Register the static directory as a resource. Keep the directories containing the rules and the media files separate.

To register a resource directory inside your package, named 'my.package', use the following in your configure.zcml

  <browser:resourceDirectory
     name="my.package.media" 
     directory="static"
     />

In your template, you will now be able to access a resource using '++resource++my.package.media/name-of-resource', i.e.

<link rel="stylesheet" type="text/css" href="++resource++my.package.media/css/jquery/accordion.css"/>

This should now work as intended even after a url rewrite.

Avoid using absolute paths when defining the locations of your XDV rules and templates. Instead, simply use Python :) For example, we have placed our template files (.html) and our rules files (.xml) in a directory called xdvstuff, inside our package:

python://my.package/xdvstuff/theme.html
python://my.package/xdvstuff/theme.xml
1
Christoph Böhner On

If you use the "absolute_prefix" setting

<registry>
    <record interface="collective.xdv.interfaces.ITransformSettings" field="absolute_prefix">
        <value>/++resource++example.sitetheme</value>
    </record>
</registry>

and manage your static files via the resource registries with the "applyPrefix=True" option you can use both relative and absolute urls inside your theme html file (preserving the possibility for theme authors to simply use the static directory independent from Plone) and use

<drop theme="/html/head/style" />
<append theme="/html/head" content="/html/head/style " />

to remove them from the theme template and pull all static resources back in from the resource registries (with the additional benefit of having them merged for production use). See collective.xdv for details.

Note: though Nginx is very fast at serving static files we got better overall performance from utilizing the resource registries for our theme`s static files in combination with the usual caching proxy (Varnish) in front.