April 28, 2022
Theme in Liferay is very important component for any project you will be working. Liferay 7 is coming with many changes and you will need to know the new ways of development in Liferay. This blog will help you understand how you can build theme in Liferay 7 using theme generator. Please note, using plug-in SDK approach to develop custom theme in Liferay 7 is still valid approach.
While writing this blog I am taking assumption that you already know Liferay and what is theme in Liferay. This blog is divided in three blocks,
Install theme generator:
Theme generator has few inline dependencies. To make theme generator work, you need to follow following steps to resolve its dependencies,
node -v
You should get following as output
V4.4.5
Note, to avoid version conflict, it is batter to install Long Term Support (LTS) version of Node.js which is 4.4.5 at the time of writing this blog.
Now that you have successfully install Node Package manager (NPM) in your machine, you are good to proceed with further steps.
Tip: In windows you will not be able to create file with name ‘.npmrc’ from user interface. You can use following way to create file in windows,
copy NUL .npmrc
prefix=c:/Users/[username]/.npm-packages
[username] should be replace with your user’s home directory name (ex. If you are logged-in using ‘EnProwess’ user. This path should look like c:/Users/EnProwess/.npm-packages).
You may be thinking why you need to set prefix property under this file then here is the answer, prefix property will inform NPM installer to install all belonging packages under the path you set as value to this property.
NPM_PACKAGES=/Users/[username]/.npm-packages
Tip: this should be same as prefix value
NODE_PATH=${NPM_PACKAGES}/node_modules
PATH=${NPM_PACKAGES}
Tip: please check your c:/Users/[username]/.npm-packages folder. If batch files are created under /bin folder then you should set PATH as ${NPM_PACKAGES}/bin otherwise keep as I mentioned above.
npm install -g yo gulp
Tips: if you are wondering what is Yeoman and gulp then don’t get confuse. Yoeman is scaffolding tool and gulp is streaming build system (assume like ant or mevan).
Congratulations! You’ve completed installing the Themes Generator’s dependencies. You are good to start with running following command to generate theme
npm install -g generator-liferay-theme
Installing Sass on Windows:
There is additional step you need to perform before you build theme. Liferay 7 support Sass in theme. Sass is an extension of CSS3 which adds nested rules, variables, mixins, selector inheritance, and more. Sass generates well formatted CSS and makes your stylesheets easier to organize and maintain.
Now that you know you will need Sass, lets understand how you can setup Sass. There are two options either you use node-sass or Ruby based Sass. By default, theme generator creates theme projects to use node-sass but you still have option to reconfigure it to use Ruby based Sass and Compass.
If you are thinking why someone will go with ruby based Sass if default is node-sass. The simple reason could be the dependencies node-sass needs to make it work. You need to install node-gyp to make node-sass work. Node-gyp requires python and Visual studio installed on machine. Sometime people does not want to install so many dependencies.
You need to make a simple decision to go with either node-sass or Ruby based sass.
It you have made your mind to go with node-sass, please install node-gyp by following simple steps given in Install node-gyp.
If you want to use Ruby based sass install ruby by downloading Ruby Installer. Install Ruby using installer you just download and run following simple command,
gem install sass compass
Well, you have reach to final stage now. Follow the following simple steps to use theme generator and build your theme
Run following Yeoman command,
yo liferay-theme
Welcome to the interactive commands to generate theme. You will be asked following questions,
You provided required information to Yoeman. Now it will start the default generator to installing additional required software and creating your theme project.
With this you are done with building new theme for Liferay 7. Please note that if you opted to use Ruby based Sass earlier you must follow following steps,
With this your theme is set to support compass, now you must install the Ruby Sass middleware and save it as a dependency for your theme.
npm i –save gulp-ruby-sass
The ‘–save’ flag adds Ruby Sass to the list of dependencies in your theme’s ‘package.json’ file. Your theme is ready to use.
gulp build
Deploy Theme:
If you have followed above steps without any issue, you got theme ready. Now you can deploy theme using following gulp command,
gulp deploy
Additional Detail,
You may notice that the folder structure created using theme generator is different then what you used to see in plugin-sdk approach. Since we are talking about building and deploying theme, lets also understand the approach develop custom theme.
I hope this blog will help you understand new changes in Liferay 7. Thanks for reading through this blog. In my next blog I will share detail around Theme Gulp Task and Themelets
Blog By,
Jayesh Prajapati