Writing, including and customizing JavaScript for Plone add-ons


This part of the documentation is under construction. Important parts for adding JavaScript to addons can be found in the documentation here:

For JavaScript and CSS development, Plone 5 makes extensive use of tools like Bower, Grunt, RequireJS and Less for an optimized development process. The JavaScript and CSS resources are managed in the Plone 5 Resource Registry. The Resource Registry was completly rewritten in Plone 5 to support the new dependency based RequireJS approach. It also allows us to build Less and RequireJS bundles Through-The-Web for a low entry barrier.

JavaScript basic tips

Always use DOM ready event before executing your DOM manipulation.

Don’t include JavaScript inline in HTML code unless you are passing variables from Python to JavaScript.

Use JSLint with your code editor and ECMAStrict 5 strict mode to catch common JavaScript mistakes (like missing var).

For more JavaScript tips see brief introduction to good JavaScript practices and JSLint

Add a pattern or other javascript to your own bundle

  1. Write your JavaScript file or pattern

There are two options:

A good example for a new pattern can be found here: (Work in progress)

It uses the new Base Pattern that was added to patternslib recently. The way how this example registers the Pattern assumes that require.js is available and used. To make a pattern work without require.js please use the way like it is proposed by the patterslib documentation:

There is also a Yeoman patternslib generator that can be found here: It produces a boilerplate for creating a new pattern

  1. Add your pattern or JavaScript file as a resource (in registry.xml)

<records prefix="plone.resources/MY-PATTERN"
   <value key="js">++resource+MY-PATTERN-PATH/MY-PATTERN.js</value>

Make sure that your pattern is available over the provided resource directory path

  1. Create bundle resource (only if “define” is used under 1)

See Products.CMFPlone/Products/CMFPlone/static/plone.js for an example

  1. Add bundle resource (only if “define” is used under 1) (in registry.xml)

<records prefix="plone.resources/MY-BUNDLE-RESOURCE"
   <value key="js">++resource++MY-BUNDLE-RESOURCE-PATH/MY-BUNDLE-RESOURCE.js</value>
   <value key="css">
  1. Define bundle

<records prefix="plone.bundles/MY-BUNDLE"
 <value key="resources">
 <value key="enabled">True</value>
 <value key="jscompilation">++resource++MY-BUNDLE-PATH/MY-BUNDLE-compiled.min.js</value>
 <value key="csscompilation">++resource++MY-BUNDLE-PATH/MY-BUNDLE-compiled.min.css</value>
 <value key="depends">BUNDLE-DEPENDENCY</value>
  1. Compile bundle

First you need to install your addon in a fresh plone site. Then execute

bin/plone-compile-resources --site-id=Plone --bundle=MY-BUNDLE

Open questions for addons developers:

  • Do I need to create a bundle for every add on? I there a possibility to add a resource to an existing bundle? If yes, how is this done?

What is missing here?

  • How do I setup a dev environment for the JavaScript topic?

How to add a patternslib pattern to plone bundle in Products.CMFPlone

  1. Add resource

<records prefix="plone.resources/patternslib-patterns-autofocus"
    <value key="js">++plone++static/components/patternslib/src/pat/autofocus/autofocus.js</value>
  1. Add pattern to static/plone.js