Skip to content

Commit e502f9f

Browse files
committed
tidy static-resources.md
1 parent c3799cd commit e502f9f

File tree

1 file changed

+49
-70
lines changed

1 file changed

+49
-70
lines changed

plone.app.dexterity/advanced/static-resources.md

Lines changed: 49 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,51 @@
11
---
22
myst:
33
html_meta:
4-
"description": ""
5-
"property=og:description": ""
6-
"property=og:title": ""
7-
"keywords": ""
4+
"description": "How to add images, stylesheets, JavaScripts, and other static assets with Plone content types"
5+
"property=og:description": "How to add images, stylesheets, JavaScripts, and other static assets with Plone content types"
6+
"property=og:title": "How to add images, stylesheets, JavaScripts, and other static assets with Plone content types"
7+
"keywords": "Plone, content types, static, resources"
88
---
99

1010
# Static resources
1111

12-
**Adding images and stylesheets**
12+
This chapter describes how to add images, stylesheets, JavaScripts, and other static assets.
13+
14+
Earlier in this manual, we have seen how to create views, and how to use file and image fields.
15+
These are all dynamic, however, and often we just want to ship with a static image, icon, CSS or JavaScript file.
16+
For this, we need to register static resources.
1317

14-
Earlier in this manual, we have seen how to create views, and how to use
15-
file and image fields. These are all dynamic, however, and often we just
16-
want to ship with a static image/icon, CSS or JavaScript file. For this,
17-
we need to register static resources.
1818

1919
## Registering a static resource directory
2020

21-
The easiest way to manage static resources is to create a
22-
`static` resource directory in your Dexterity project using the ZCML resourceDirectory directive.
21+
The easiest way to manage static resources is to create a `static` resource directory in your Dexterity project using the ZCML `resourceDirectory` directive.
2322

24-
Registration of the resource directory is done using the
25-
`<browser:resourceDirectory />` ZCML directive. This requires two
26-
attributes: `name` is the name that appears after the
27-
`++resource++` namespace; `directory` is a relative path to the
28-
directory containing resources.
23+
Registration of the resource directory is done using the `<browser:resourceDirectory />` ZCML directive.
24+
This requires two attributes.
25+
`name` is the name that appears after the `++resource++` namespace
26+
`directory` is a relative path to the directory containing resources.
2927

30-
It's conventional to use "static" for the directory name and the dotted name of your package for the resource name.
31-
You would use this zcml to register it:
28+
It's conventional to use `static` for the directory name, and the dotted name of your package for the resource name.
29+
You would use this ZCML to register it.
3230

3331
```xml
3432
<browser:resourceDirectory
3533
name="example.conference"
3634
directory="static" />
3735
```
3836

39-
Then, if a "static" resource directory in the `example.conference` package
40-
contains a file called `conference.css`, it will be accessible on a URL
41-
like `http://<server>/site/++resource++example.conference/conference.css.`
42-
The resource name is the same as the package name wherein the `resources`
43-
directory appears.
37+
Then, if a "static" resource directory in the `example.conference` package contains a file called {file}`conference.css`, it will be accessible on a URL, such as `https://<server>/site/++resource++example.conference/conference.css`.
38+
The resource name is the same as the package name wherein the `resources` directory appears.
39+
4440

4541
## Importing CSS and JavaScript files in templates
4642

47-
One common use of static resources is to add a static CSS or JavaScript
48-
file to a specific template. We can do this by filling the `style_slot`
49-
or `javascript_slot` in Plone’s `main_template` in our own view
50-
template and using an appropriate resource link.
43+
One common use of static resources is to add a static CSS or JavaScript file to a specific template.
44+
We can do this by filling the `style_slot` or `javascript_slot` in Plone's `main_template` in our own view template and using an appropriate resource link.
5145

52-
For example, we could add the following near the top of
53-
`presenter_templates/view.pt`:
46+
For example, we could add the following near the top of {file}`presenter_templates/view.pt`.
5447

55-
```html
48+
```xml
5649
<head>
5750
<metal:block fill-slot="style_slot">
5851
<link rel="stylesheet" type="text/css"
@@ -63,35 +56,24 @@ For example, we could add the following near the top of
6356
</head>
6457
```
6558

66-
:::{note}
67-
Always create the resource URL relative to the navigation root as shown
68-
here, so that the URL is the same for all content objects using this
69-
view. This allows for efficient resource caching.
70-
:::
71-
72-
## Registering resources with Plone’s resource registries
73-
74-
Sometimes it is more appropriate to register a stylesheet with Plone’s
75-
`portal_css` registry (or a JavaScript file with
76-
`portal_javascripts`), rather than add the registration on a
77-
per-template basis. This ensures that the resource is available
78-
site-wide.
79-
80-
:::{note}
81-
It may seem wasteful to include a resource that is not be used on all
82-
pages in the global registry. Remember, however, that `portal_css` and
83-
`portal_javascripts` will merge and compress resources, and set caching
84-
headers such that browsers and caching proxies can cache resources well.
85-
It is often more effective to have one slightly larger file that caches
86-
well, than to have a variable number of files that may need to be loaded
87-
at different times.
88-
:::
89-
90-
To add a static resource file, you can use the GenericSetup
91-
`cssregistry.xml` or `jsregistry.xml` import steps in the
92-
`profiles/default` directory. For example, an import step to add the
93-
`conference.css` file site-wide may involve a `cssregistry.xml` file
94-
that looks like this:
59+
```{note}
60+
Always create the resource URL relative to the navigation root as shown here, so that the URL is the same for all content objects using this view.
61+
This allows for efficient resource caching.
62+
```
63+
64+
65+
## Registering resources with Plone's resource registries
66+
67+
Sometimes it is more appropriate to register a stylesheet with Plone's `portal_css` registry (or a JavaScript file with `portal_javascripts`), rather than add the registration on a per-template basis.
68+
This ensures that the resource is available site-wide.
69+
70+
```{note}
71+
It may seem wasteful to include a resource that is not be used on all pages in the global registry.
72+
Remember, however, that `portal_css` and `portal_javascripts` will merge and compress resources, and set caching headers such that browsers and caching proxies can cache resources well.
73+
It is often more effective to have one slightly larger file that caches well, than to have a variable number of files that may need to be loaded at different times.
74+
```
75+
76+
To add a static resource file, you can use the GenericSetup {file}`cssregistry.xml` or {file}`jsregistry.xml` import steps in the `profiles/default` directory. For example, an import step to add the {file}`conference.css` file site-wide may involve a {file}`cssregistry.xml` file such as the following.
9577

9678
```xml
9779
<?xml version="1.0"?>
@@ -103,8 +85,7 @@ that looks like this:
10385
</object>
10486
```
10587

106-
Similarly, a JavaScript resource could be imported with a
107-
`jsregistry.xml` like:
88+
Similarly, a JavaScript resource could be imported with a {file}`jsregistry.xml` file such as the following.
10889

10990
```xml
11091
<?xml version="1.0"?>
@@ -115,26 +96,24 @@ Similarly, a JavaScript resource could be imported with a
11596
</object>
11697
```
11798

99+
118100
## Image resources
119101

120-
Images can be added to resource directories just like any other type of
121-
resource. To use the image in a view, you can construct an `<img />` tag
122-
like this:
102+
Images can be added to resource directories just like any other type of resource.
103+
To use the image in a view, you can construct an `<img />` tag as follows.
123104

124-
```html
105+
```xml
125106
<img style="float: left; margin-right: 2px; margin-top: 2px"
126107
tal:define="navroot context/@@plone_portal_state/navigation_root_url"
127108
tal:attributes="src string:${navroot}/++resource++example.conference/program.gif"
128109
/>
129110
```
130111

112+
131113
## Content type icons
132114

133-
Finally, to use an image resource as the icon for a content type, simply
134-
list it in the FTI under the `content_icon` property. For example, in
135-
`profiles/default/types/example.conference.presenter.xml`, we can use
136-
the following line, presuming we have a `presenter.gif` in the `example.conference` resource
137-
directory:
115+
Finally, to use an image resource as the icon for a content type, simply list it in the FTI under the `content_icon` property.
116+
For example, in {file}`profiles/default/types/example.conference.presenter.xml`, we can use the following line, presuming we have a {file}`presenter.gif` in the `example.conference` resource directory.
138117

139118
```xml
140119
<property name="content_icon">++resource++example.conference/presenter.gif</property>

0 commit comments

Comments
 (0)