Create Web Content With Structure and Template in Liferay

Web content in Liferay can be created with structure and template. It’s more powerful to create complex web content which is highly configurable.
In This article, I will show you how to create web content with the help of Structure and Template.
I assume that you have basic knowledge of how to create simple web content and how to configure it into out of the box portlet – Web Content Display portlet.

If you are new to web content in liferay, I would suggest to get detail about simple web content from my previous blog – 
Create Web Content in Liferay

Before we start, let me explain what is structure,template and content in liferay.


Structure :- As its name, Structure defines the data structure of the web content.These data structure will contain the data types (like String, image, url, boolean etc.).


Template:- Template provide the template where the data structure will be arranged.


Content:- Its the textual content that will be fit in respective structure and structures will be arranged in template to form of complete Web Content.
Now let us do some practical work. In my previous post Create Web Content in Liferay I have created community Nilang. So I will use the same community and going to create separate Web Content (with structure and template)

 
I would recommend looking at index page ‘A Complete Liferay Guide‘ to browse all topics about liferay.

First do log-in and go to Control Panel and select Nilang community from drop-down in Content section of control panel.
Click on Web Content in content section. You may observe that the web content named “Simple Web Content” that we had created in previous post will be shown.

We will divide Web Content creation activity in three steps.

1) Creating Structure

Click on Structure link just besides (Web Content) as per below screenshot.
Web content with structure and template in liferay - Add Structure to create web content in Liferay
Give the basic details of Structure like name and descriptions. Also make sure to tick mark the Autogenerated ID checkbox. (So that the Id of the structure will generate automatically) as per below screenshot.
Web content with structure and template in liferay - Creating new Structure for creating web content
Now scroll down and you will find the section called XML Schema Definition where we can define various data type. Click on Add button and add 4 rows and give the values as per below screenshot.
Web content with structure and template in liferay - Adding elements of Structure
Note:- I have tick-mark Repeatable checkbox for Body component. This will allow to insert multiple component while creating web content. I will show this later in this blog.

Click on Save button to create Structure.

Explanation:-

  • The first component(Text box) represents the name of the element
  • Second component(Drop down) represents the type of the element (like text, text box,link etc)
  • Third component(Drop down) represent whether this element is searchable or not
  • Fourth component(select box) represent whether this element is repeatable or not.
We had created 
  • Header element as Text (Label)
  • Logo element as Image
  • Body element as Text Area(Where we will put HTML code
  • Footer element as again Text (Label)
Click on Save button. You will see the structure entry as per below screenshot.
p-2B-3-

Now second step

2) Creating Template

Click on Template link which is just besides the Structure link. Click on Add Template button as shown in below screenshot.Web content with structure and template in liferay - Creating new Template for Web content in Liferay
Give template name and description. Also tick mark to Autogenerated ID check-box as shown in below screenshot.
Web content with structure and template in liferay - Adding new Template in Liferay

Now scroll down to see more options. 
Template can be written different language like Freemarker, Velocity, XSL etc. Still we can user plain HTML, Javascript and CSS while creating Template.

First we need to attach Structure for this template. Click on Select button in Structure section as shown in below screenshot. It will list down all the structure we created for Nilang community. Choose the structure that we created just now (Web Content 1) and click on Save in pop window.

Web content with structure and template in liferay - Selecting Structure while creating Template in Liferay

Select Language Type as VM(Velocity) and click on Launch Editor button as shown in below screenshot.
Web content with structure and template in liferay - Selecting template engine while creating template in Liferay

You will notice the Template Editor something like as below

Web content with structure and template in liferay - Template code editor

Clear all the content and type the following code in it.

and click on Update button.

Explanation:-

  • We are simply created one table with row and column. 
  • In first row, we are displaying logo image. we are getting the its value by $Logo.data expression. In this expression, Logo is the name of the component that we defined in Structure. 
  • Since this is Velocity template, all the component’s value will be available by its name. Component value we will give while creating Web Content.
  • Similar way we are accessing other component’s value (with $<<COMPONENT_NAME>>.data expression)
Note:- If we don’t attach Structure with Template, then these component value won’t be available.
 

Create web content with structure and template

Now our Template has been created. Next we will create Actual web content.
Click on Web Content from Content Section from control panel and then choose Web Content and click on Add Web Content as per below screenshot.
 Web content with structure and template in liferay - Adding new web content in Liferay
Give the name as ‘Web Content1’ and click on  Select button in Template section (right side just below the Structure section) as per below screenshot.
Web content with structure and template in liferay - Adding new web content in Liferay

Note:- If you are not able to see the Template (or Structure section), then you have to click on Advanced link just below the <<Back link. When these sections (Structure and Template) are opened, then the Advanced link will be converted to Basic link.

Since we already attached the Structure with Template, if we select Template, then automatically the attached Structure will be applied.

On clicking on Select button, it will show the pop up saying ‘Selecting a template will change the structure,available input fields, and available templates ? Do you want to proceed ?’. Click OK button and next new browser window will be open which will list down all template available as per below screenshot.

Change Template associate with Liferay web content

Click on Test Template1 which we had created. You will observe that, now instead of single WYSIWYG editor, the Web Content is divided in four sections.
  • Header:- (We have given type as Text in structure. So whatever value we will give in text box, it will be shown as label while displaying this  Web Content .)
  • Body:- (As WYSIWYG editor, because we have given type as Text Area (HTML) in Structure. So we can type HTML code and it will render when actual Web Content get displayed).
  • Logo:- (We had given type as Image in Structure, so that it will ask to upload Image which will be render while actual Web Content display)
  • Footer:- (We have given type as Text in Structure. So whatever value we will give in text box, it will be shown as label while displaying this  Web Content .)

Now its time to give actual value in these components. You are free to give any value you want. For explanation, I am giving the following values.

  • Header :- “My Own Company”
  • Body:- anything you want.
  • Logo:- Upload any logo size image.
  • Footer:- “Copyrighted by me :)”
You can give more options while creating Web Content. For this, just click on Edit button in Structure section as shown in below screenshot.
Editing Structure elements while creating Liferay web content

You will observe following options for each component we defined in Structure.

  • Value
  • Edit Option button from where you will find more options like tool tip and etc.
  • Variable Name (defined in Structure)
  • Close button (To remove it from Structure).
Once you done with editing, you can click on Save button from Structure section. If you cancel all your changes then simply click on Stop Editing button just besides Save button as per following screenshot.
structure and template in liferay - structure

Note:-  If you want, you can choose different Structure from Choose link.

Click on Publish button. Now its time to see our hard work done so far. To display this web content, we will create new page called Web Content 1 in Nilang community.
 
Now place the Web Content Display portlet and configure it to show the Web Content 1 that we just now created. To know how to configure the Web Content to Web Content Display portlet please refer my previous blog Create Web Content in Liferay
 
After successfully configuration, it will looks like as per below screenshot.
structure and template in liferay - Liferay web content with Structure and Template

And all this done.  Try some more complex components in Structure and do some hands on for creating Web Content with the help of Structure and Template.


I would recommend looking at index page ‘A Complete Liferay Guide‘ to browse all topics about liferay.

Share This Post

14 Comments - Write a Comment

  1. Hi Nilang,
    Similar to create web content with the help of Structure and Template. I would like to create printable document based on entered data for example we are getting data while entering data in Liferay’s Forms or creating Documents. Actually I want to generate a letter (printable) based on predefined template to the person once attached document with meta data is approved.
    How is it possible? Any ides?
    Thanks in Advance.

    Reply
    1. Hi Umesh,
      Sorry for late response.
      As per my understanding, you need to create custom portlet showing the list of document. For eachdocument,you need to define either expando (custom attribute – liferay) and then render the predefined template. Hope this help to achieve what you want.

      Regards
      Nilang

      Reply
  2. Dear Sir:

    I created a web content article with structure and template.
    To preview the web content article in Web Content Editing Page is OK ,
    but it didn’t show anything except the title of the article in “Web Content Display” and “Asset Publisher” portlets

    Could someone help to fix it ?

    Best Regards,

    Jasper Tseng

    Reply
  3. Dear Nilang,
    Nice article, it is simple to understand… I had a query on how the summary of the web content with “read more” link which takes the user to the asset publisher to read the entire article… basically I am working on an internal news letter, which will have a summary & read more link for the user to read the entire article

    Regards,

    Reply
    1. Hi Mehtab,

      Welcome to Techblog and thanks for appreciation. In this scenario, you can place access publisher portlet on some page. When any user click on ‘Read More’ button, you need to pass articleId of current web content along with the url of asset publisher. You also can open this link in model popup so that user can see full article in model pop up.

      Feel free to ask questions / give suggestion / feedback
      Join this blog to get updated.

      Regards
      Nilang

      Reply
    1. Hi Andrea,

      Welcome to Tech blog. when you define structure with selection list, you can define it in template. when you actually creates web content, there will be one check box called localize for each attribute of structure. You can select this checkbox for attribute you want localize.

      Hope this solve your query. Feel free to ask questions / give feedback

      Regards
      Nilang

      Reply
  4. Hi Nilang! Nice article here. Please I’d like to how to design a template like yours, but with a read more link to display the rest of the content on a content display page, like what you have in an asset publisher.

    Thanks in advance!

    Reply
    1. Hi Mattae,

      Welcome to Tech blog and thanks for appreciation. Sorry but I didn’t get you exactly. Do you want me to explain how to design template ? Please clarify so that I can help you out.

      Regards
      Nilang

      Reply
  5. Hi, I have seen your blogs and the explanation is really great, full and clear. I like this article but I want to do something more complicated. I would like to know if in the structure I can show data from a Data Base. For example, in your structure you have a Header element, but imagine I have a ‘Header’ Table with all the headers I use. Now instead of a text box I would like to show Header element as drop down list with the options of my ‘Header’ table. Is that posible here? or is better to do it in the blog portlet?
    The reason I would like to use liferay web content or blog, is because they already provide some functionalities I would like to use like publish, approval workflow and more.

    Reply
    1. Hi ,

      First of welcome to Techblog and thx for appreciation. The scenario you mentioned is really interested. So far I have never pulled data from DB and shown in Content. So first of all I will look into this and then will be able to give you suggestion.

      Regards
      Nilang

      Reply
    2. It will be great if you can look into this and give some suggestions. In the meantime I will continue reading all your liferay post that are very interesting.

      Reply

Post Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.