Magnolia 6.1 reached end of life on March 31, 2021. This branch is no longer supported, see End-of-life policy.
In this beginner tutorial, you will create a simple web page based on a custom-built light module called hello-magnolia
. Your light module will provide a page template and a custom quotation
component for a web page. You will learn how to get content from a repository and display it on the page. You will develop the page using Magnolia CLI, an npm package which facilitates light development with Magnolia.
Magnolia CLI is available in several release versions. This tutorial has been checked against Magnolia CLI version 3.1.0
.
Are you planning a 'headless' project? Start with the My first content app tutorial.
For development and testing you need a running instance of Magnolia. In this tutorial, we suppose that it is installed in a directory called magnolia
.
In this section, you create the hello-magnolia
module.
light-modules
folder of your Magnolia installation:Run the following command:
mgnl create-light-module hello-magnolia
The created structure:
This is a typical Magnolia module structure.
é
, à
, ç
, ä
, ö
, ü
or special characters such as slashes /
, \
and so on.
Magnolia continuously scans the file system folder defined by the magnolia.resources.dir
property. In this case, it looks inside the light-modules folder, and registers your module(s) automatically. It detects new and modified templates, dialogs and (web)resources.
Open the Resource files app from the App launcher and check that the hello-magnolia
module structure has been registered:
The resource is loaded from the file system.
icon in the Origin column indicates that theYou can create a working page template instantly with a CLI command called create-page
, but we'll make the first template by hand for educational reasons.
In the steps that follow, you create:
Create a new css
folder in /hello-magnolia/webresources/
.
Copy the CSS below and save it as /hello-magnolia/webresources/css/hello-style.css
. We will reference this stylesheet in a FreeMarker template script.
After saving the file, it becomes a resource available as http://localhost:8080/magnoliaAuthor/.resources/hello-magnolia/webresources/css/hello-style.css.
A template script defines the output, typically HTML, and is interpreted by a page renderer. The script below uses a templating language called FreeMarker.
/hello-magnolia/templates/pages/
folder.In there, create a file called hello.ftl
and save it with the following content:
A template definition gives the template a name and makes it available to the system. It also tells the system which script renders the content.
We use YAML to create template definitions.
Copy the template definition code below.
Save it to a new file /hello-magnolia/templates/pages/hello.yaml
.
Copy the following code to a file named hello.yaml
in /hello-magnolia/dialogs/pages/
:
A dialog defined like this allows content authors to add and edit the values of page properties such as windowTitle
and title
. The content of these properties is stored in the Magnolia JCR repository.
Now you can use the page template to create a page in the Author instance. You can then publish the page to the Public instance.
hello
. Start this by clicking Add page from the Action bar on the right-hand side.hello
template as the page template. Click Next:Hello Magnolia
into the Title and Window title fields. Congratulations! You've created your first Magnolia page built on a page template.
Now let's enhance the content of the page by adding an editable custom-built quotation
component to the area just below the "Hello Magnolia works!" heading.
Areas and components help modularize a project and structure pages. Once areas and components are defined, you can reuse them in many page templates.
Let's create an area with one component to add quotations. At the end, the whole page should look like this:
What is ...
Areas are defined in the page template definition.
The main
area for the quotation component is defined and created when you execute the create-component
command. You'll create the area in the next section .
Make sure you are at the root of your module (/magnolia/light-modules/hello-magnolia
). Use the following CLI command to create the quotation
component and make it available in the main
area of the page template:
mgnl create-component quotation -a pages/hello@main
The command creates the following files:
/hello-magnolia/dialogs/components/quotation.yaml
/hello-magnolia/templates/components/quotation.yaml
/hello-magnolia/templates/components/quotation.ftl
The command also modifies the /hello-magnolia/templates/pages/hello.yaml
file, in which it creates the main
area and adds the availability of the quotation
component by appending the following piece of code to it:
areas: main: availableComponents: quotation: id: hello-magnolia:components/quotation
main
area on the pageWith @main
, the mgnl create-component
command also adds [@cms.area name="main"/]
to end of the hello.ftl
page template script.
We want the main
area just below the page heading. For this, edit the /hello-magnolia/templates/pages/hello.ftl
template script and add a <div/>
element with the area just below the <h1/>
element:
The CLI command mgnl create-component
creates a dialog definition with field types which you do not need. Open the dialog definition file /hello-magnolia/dialogs/components/quotation.yaml
for editing, and edit it to appear as follows:
form: label: quotation tabs: - name: tabMain label: Main fields: - name: quotation fieldType: richText label: Quotation - name: citedPerson fieldType: text label: Cited person actions: commit: class: info.magnolia.ui.admincentral.dialog.action.SaveDialogActionDefinition cancel: class: info.magnolia.ui.admincentral.dialog.action.CancelDialogActionDefinition
Be careful with the type of whitespace in this code. All the whitespace to the left of the text should be formed using the SPACE character, not the TAB character. For more details on this topic, see the 6.1. Indentation Spaces section in YAML v1.2 specification.
The key content elements in the above dialogue definition – citedPerson
and quotation
– are stored in the JCR repository and rendered in the page by the following code, the component's script.
Replace the quotation.ftl
template script with the following code:
The final structure of your hello-magnolia
module in the light-modules
folder should look like this:
hello-magnolia/ ├── decorations/ ├── dialogs/ │ ├── components/ │ │ └── quotation.yaml │ └── pages/ │ └── hello.yaml ├── i18n/ │ └── hello-magnolia-messages_en.properties ├── README.md ├── includes ├── templates/ │ ├── components/ │ │ ├── quotation.ftl │ │ └── quotation.yaml │ └── pages/ │ ├── hello.ftl │ └── hello.yaml └── webresources/ └── css/ └── hello-style.css
Now, a content author can add a quotation component to the hello
page with the text and the author of a quotation.
hello
page in the Pages app.Finally, access the web page on the Public instance: http://localhost:8080/magnoliaPublic/hello.html.
Other editors, with appropriate permissions configured in the Security app, can now also change the content of the quotation component on the Author instance.
Congratulations.
You now know the basics of Magnolia templating. With these techniques, you can build websites where non-technical authors can manage content using dialogs.