Hi, I’m working on Liferay DXP 7.3 with a custom theme deployed. I need to include scripts and CSS in the head tag of the theme dynamically, as the source changes frequently. What are the possible ways to do this?
2 Answers
2Looking at portal_normal.ftl, there is an extension point already, which you can utilize and calculate whatever you need:
<@liferay_util["include"] page=top_head_include />
It points to here, which points to here. And there you find the best maintainable way to extend it:
<liferay-util:dynamic-include key=“/html/common/themes/top_head.jsp#pre” />
A DynamicInclude. Encapsulate your logic there and calculate whatever you want to add to the head section without modifying any existing or other custom file. It’ll all be well encapsulated in that single component.
Thank you, Olaf. It works perfectly.
Extending on Olafs answer: We added an OSGI config with a String[] field and the snippets are then added to the page. We found it quite useful since it allowed us to add/update verification codes, tracking codes and other stuff on the fly. Since you have a custom theme, another approach would be to implement a template context contributor. It allows you to inject custom variables into freemarker. https://liferay-studio.github.io/themes/samples/template-context-contributor/ But for your usecase the DynamicInclude extension point sounds just fine.
– Christoph_Rabel