Creating website templates in Jahia tutorial

November 14, 2023

In this tutorial, you will learn how to build a website from scratch using Jahia. It is assumed that you have an instance of Jahia up and running. If not, see Jahia with Docker - Instructions to run Jahia in a Docker image.

You will build a site with a carousel that looks similar to this:

final-in-preview.png

Learning the basics

Basics of content definitions and rendering

A content object is a small structured piece of content that you add to a page for display in a browser or mobile device. Object properties compose the single content element. For example, an article content object is composed of a title, an abstract and body, as well as optional metadata such as author, publishing date, description, and tags. All content objects stored in Jahia have an associated definition and view. For example, a content object of type jnt:article could have the following definition:

  • A Title property
  • An Intro property
  • Paragraphs and subnodes (there can more than one)

As you can see, a type definition can include properties or subnodes. Each content object is stored as a node. Subnodes are simply content objects stored under a parent node, which creates a tree structure. If you’re wondering what the jnt: prefix stands for, it is a namespace that means Jahia Node Type. You can and should define your own namespaces (but let’s not get into that now).

After defining a type, you can create a view to render the content object into HTML, as shown in the following example:

<h2>${currentNode.properties.title.string}</h2>
 <div class="intro">
  ${currentNode.properties.intro.string} 
 </div>
<c:forEach items="${currentNode.nodes}" var="paragraph">
 <template:module node="${paragraph}" view="default"/> <!-- this will render the paragraph sub-node -->
</c:forEach>

Views are JSP tags that use custom HTML tags to output content object properties. Alternatively, you can access content objects in a “headless” way using our GraphQL API.

Jahia includes many predefined content object types, known as node types, and associated views. Modules can define new types and views. In this tutorial, you do not need to define any new types or views. Instead, you use existing types that are available in modules from the public Jahia App Store. If you’re interested in learning more about node types and views, see Where to go from here at the end of this tutorial.

Content pages and templates

Content pages are similar to HTML pages, meaning that for each content page there is a full HTML page that is rendered. Pages can be organized in a tree structure, just the same as for content objects. Pages are actually content objects with a jnt:page node type.

The mechanism for rendering a page is a bit different than for regular content objects. Pages use layout templates, and a template is also a content object that has its own view. As templates instances all share the same view, this makes it easy to define common HTML headers, scripts, CSS, and other resources as shared dependencies between them.

Templates can then be packaged in a Jahia module that is called a template set. You will now learn how to build your first template set, set up layout templates, create default content, package everything up and finally how to create a site using this template set.

Installing the Bootstrap 4 modules

First you install the modules that you will use as building blocks for your template set. You use the following modules that are freely available on Jahia’s App Store:

  • Popper.js
    Embeds the Javascript library to calculate the positioning of HTML elements
  • Bootstrap 4 Core
    Embeds the Bootstrap 4 CSS and Javascript libraries
  • Bootstrap 4 Components
    Uses the Bootstrap 4 Core module and offers a library of Jahia components that wrap Bootstrap 4 components

To install the modules:

  1. Access Jahia at http://localhost:8080 and log in using the root/root credentials.
  2. Select Page Composer from the login menu.
    home-page.png
  3. At the bottom of the navigation menu, select Administration>Server>Modules and Extensions>Modules.
  4. Select the Available modules tab to connect to the Jahia Public App Store.
    available-modules.png
  5. In the Search module by keyword field, enter Popper and click the download arrow under Download.
  6. Search for bootstrap4 and download the Bootstrap4 Core and Bootstrap4 Components modules.

Creating a template set module using the Jahia Studio

To create a template set module:

  1. In Jahia, navigate to Developer Tools.
    developer-tools.png
  2. Select Studio. Studio opens.
  3. In the top toolbar, select New>New module.
  4. In the Create module dialog, change the module type to templatesSet.
  5. In Module name, name your module template-tutorial.
    new-module.png
  6. Click OK to create the module. This typically takes 5-10 minutes as Jahia generates a new module, downloads all its dependencies and compiles it. Dependencies are only downloaded for the first module and creation time depends on your connection speed.
  7. Once the module is generated, in the left panel, navigate to and select the following file: src/main/resources/jnt_template/html/template.template-tutorial.jsp. This is the “view” for your pages. Note the <template:area path="pagecontent” /> tag in the <div> tags. This is where the actual layout template will be used. You don’t need to modify this file, just have a look at the path of the area because you will see how it relates to page templates.
  8. Next, you must declare that your template set will use the newly installed Bootstrap 4 module and its dependencies. Select the Dependencies tab
    in the left panel. Move the Bootstrap 4 Core, Bootstrap 4 Components and popper.js modules from the Module to the Dependency section using the Click to add a dependency down arrow. When you are finished, the Dependencies tab should look like this:
    new-module.png

Creating the base page template

Page templates can inherit objects from other templates. You can use the tabs in the left menu navigation to switch to other objects.

Notice the base template with two page templates below it. Anything defined in the base template is inherited by all the templates defined below it.

You will now set up the components in the base template:

  1. Select the Templates tab templates-tab.png and open the templates folder. Select the base template.
  2. Click the arrow at the right of the PAGECONTENT box to enable the zone, which is known as a template area.
  3. Click Any content and select Bootstrap 4>Navbar. Click OK.
  4. Select Advanced settings and select Wrap the content of the navbar in a container to center it on a page and Add the login button. Select Brand and enter ACME in the Brand text field. Click Save.
    bootstrap-navbar.png
  5. You will now add a Bootstrap 4 Layout and Grid component to adjust the layout to be center in the middle of the page. Click Any content and select the Bootstrap 4>Layout and Grid component and click OK.
  6. Select the Create Container checkbox but don’t modify any of its settings. This setting centers the layout of the container on the page. If you’re interested in learning more about Bootstrap Containers and Layout, you can read the official documentation.
  7. Select the Create Rows and Columns checkbox. In Select the type of grid that you want to create, choose Predefined grid. In Select a predefined grid, select the container-fluid option of 12.
    Note: Bootstrap Grid options specify the number of columns in the grid and the relative width of columns. For example, 4 / 4 / 4 specifies three columns of equal width.
    create-row-and-columns.png
  8. Click Save. A center-aligned, grey box named LAYOUT-AND-GRID-MAIN displays in the pagecontent area.
  9. Activate the LAYOUT-AND-GRID-MAIN box by clicking the down arrow button to the right of the box’s title.
  10. Click Any content and add the Bootstrap 4>Breadcrumb component and click OK.
  11. Select the Advanced settings checkbox and remove the float-left class and click Save.
  12. You will now add a template area. You add content into an area using subtemplates or page instances. The initial PAGECONTENT area that you activated earlier in this tutorial is also a template area. To add the area, click Any content button in the LAYOUT-AND-GRID-MAIN box again, and this time select the Layout Components>Area component (make sure you don’t use the Absolute Area as this area is quite different in the way it works). Click OK.
  13. In the Add: Area dialog, enter pagecontent in the System name field. You can ignore all the other component settings. Click Save.

Your base template should look like this:

base-template.png

Creating the home and simple page templates

Now that the base template is set up, you will use the basic layout defined by the template to populate the home and simple page templates, and define the more precise layouts that these pages will use.

  1. Select the home page template in the left menu.
  2. Activate the PAGECONTENT  box by clicking the down arrow at the right of the box. Note that the box is centered because it inherits its position from the base template. The other components that you set up do not display in this view. You can see how they render by selecting the In-context view in the upper right.
  3. Make sure you are back in the Structure view. Click Any content and add a Bootstrap 4>Layout and Grid component. Click OK.
  4. Select the Create Rows and Columns checkbox. In Select the type of grid that you want to create, choose Predefined grid. In Select a predefined grid, select 12.
  5. In Classes set on this row block, enter mb-4 to add a margin at the bottom of the box. The margin will clearly separate this content from other content that displays below. If you are wondering what this value is you can read Bootstrap 4’s documentation about spacing. Don’t change any other settings and click Save.
  6. Click Any content again and add a Layout and Grid component again.
  7. This time, select the Create Rows and Columns checkbox and specify the 4 / 4 / 4 predefined type.
  8. Click Save.

You have created the home page template. The template should look like this:

home-template.png

Now you will set up the simple page template:

  1. Select the simple page template in the left menu.
  2. Click the down arrow at the right of the PAGECONTENT area box to activate or open it.
  3. Click Any content, select the Bootstrap 4>Layout and Grid component and click OK.
  4. Select the Create Rows and Columns checkbox and specify the 4 / 8 predefined type.
  5. Click Save.

This is all that’s needed for the simple page template. Your template should look like this:

simple-template.png

This completes the templates setup. Next, you define a prebuilt page with content that can be used to import content directly into a website upon creation.

Adding default content to the template set module home page

In a module, you define prebuilt content that will be imported as default content when a site is created. You will use this feature to populate a default home page with content that can then be modified by content editors. This feature also serves as a great way to illustrate how to use the home page template that you have defined.

To edit the default home page content, select the Content tab image8.png in the left menu and select template-tutorial>Home to view the content.

default-home-page.png

As you can see, a Home page already exists. Notice that it has the grid components that you just defined for the home page template.

To add content to the default Home page:

  1. Activate the LAYOUT-AND-GRID-MAIN area by clicking the down arrow at the top right of the box.
  2. Click Any content, select the Bootstrap 4>Carousel component and click OK.
  3. In the Add: Carousel dialog, accept the default settings and click Save. Have a look at the advanced settings if you’re curious but don’t activate them. A new Slide button appears.
  4. Click the Slide button to add a carousel slide.
  5. In the Title field, enter Welcome to Jahia.
  6. In the Slide section and in the image field, click the down arrow. In the Image selection dialog, navigate to the default directory and click the upload button
    .
  7. Upload two images in a resolution close to 1024x468. You can use the following two images:
    nyon-water-sunset
    https://drive.google.com/file/d/1CMbQjruV9TNLrpcwQanH7rlgY3hsP2p1/view?usp=sharing
    skies-above-kloten
    https://drive.google.com/file/d/1IoQgsrJVXSmhXAaPvoMLE8CBTxtz1r5D/view?usp=sharing
  8. Select one of the images and click Save.
  9. In Caption enter this placeholder text: Cras justo odio, dapibus ac facilisis in, egestas eget quam. Donec id elit non mi porta gravida at eget metus. Nullam id dolor id nibh ultricies vehicula ut id elit. Then click Save.
  10. Repeat the steps 4-9 to add another slide, selecting the second image and entering any slide caption and title you want.

Next step add content in the three boxes underneath the carousel:

  1. Activate the left LAYOUT-AND-GRID-1-MAIN box using the down arrow and click Any content.
  2. Select the Content:Basic>Rich text component and click OK.
  3. In the Rich text editor, enter Heading and select the Heading 2 paragraph format (the third dropdown from the left). Enter some text below tagged with the Normal paragraph format. It should look like this:
  4. Click Save.
  5. For the next two boxes, instead of adding content again, you will use copy and paste functionality. Select the rich-text box, right-click and select Copy.
    copy-component.png
  6. Now activate the LAYOUT-AND-GRID-1-SIDE next box and click Paste.
  7. Do this again for the LAYOUT-AND-GRID-1-EXTRA box, copying content from either of the first two boxes. Your home page content should now look like this:
    default-home-page-02.png
  8. If you click the In-context button on the upper-right you should see something that looks like this:
    in-context.png

Don’t worry if the carousel looks different, this is the Edit mode view of the carousel. The carousel renders differently in Preview or Live mode.

You have setup of your first template set. Congratulations, you are now ready to create as many sites as you like, reusing what you have set up. But before you do that, you must deploy the template set to make it available in the system.

Deploying and creating a site from the template set module

Deploying a template set means that you make the template set available for creating new web sites. In the case of this tutorial, you have created the template set in the Jahia Studio on the same server instance that we will be using, but this doesn’t need to be the case. The template set could have been created on a separate Jahia instance dedicated to development, or it could have been downloaded from a public or private module store.

To deploy your first template set module:

  1. Switch batch to the File system tab
    in the left navigation pane.
  2. Click Compile and deploy module
    .

Wait until the module compiles and deploys. Note: Compilation is required because your module could also contain some Java code. In this case, the compilation phase will not find any Java code to process.

To create your first site with your new template set:

  1. In Jahia, navigate to Administration>Projects and click Create.
  2. On the next page, accept the default values or enter your own name for the site and site key. Then, click Next.
  3. In Please choose a template set, select template-tutorial. If you don’t see your template set here, check the logs during deployment.
    create-project.png

    Note: For more information on Docker logs, see the Docker documentation. For a local install of Jahia, you can find logs in tomcat/logs for macOS or Linux or in the Windows Shell window for Windows.
  4. In Choose modules to be deployed, use the Add selected modules button
    to move the Bootstrap 4 Components, Bootstrap 4 Core, and popper.js modules to the Selected modules list.
  5. Click Next.
  6. Click Save. A new site is created that uses your new template set.
  7. Once the site is created, the Web Projects page displays. Click the Go to Edit Mode 
    in the Actions column.
    edit-mode.png

Your screen should now look like this:

final-in-edit.png

Click the Preview button at the top-right of Jahia to see what it will look like for end users.

final-in-preview.png

Next, add a new About us page, using the simple template you defined earlier.

To add an About us page:

  1. Navigate back to Page Composer, which should be open in another tab in your browser.
  2. Select the Home page, right-click and select New page.
  3. Enter About us in the Title and select the simple template under Template name. Click Save.
  4. Click the back button. You can now add any content you want to in the LAYOUT-AND-GRID-SIDE and LAYOUT-AND-GRID-MAIN boxes.
    about-us.png
  5. Click the Preview button and notice that the breadcrumb bar now displays (it won’t appear on the home page). You can click the Home link to go back to the home page.

Congratulations you have completed this tutorial successfully! Let’s recap and look at what you have achieved and learned.

What you have learned

You have done a lot in this tutorial, you have learned how to:

  • Understand what content objects, view, pages and templates are
  • Install Jahia modules from the public Jahia App Store
  • Create a template set Jahia module using the Jahia Studio
  • Create a base template that is inherited by other page templates
  • Create two page templates with different layouts
  • Create a default home page that will be imported upon site creation
  • Deploy a module in the Jahia Studio
  • Create a site and add a page using the template set that was built previously

Where to go from here