Message Boards

Need Help with Freemarker WCM Templates

Klaus Bachmaier, modified 7 Years ago.

Need Help with Freemarker WCM Templates

Regular Member Posts: 223 Join Date: 9/30/13 Recent Posts
What I'm trying to do is to include the categories of a journal article in my WCM Freemarker template. I've googled around a bit, and already found quite some promising solutions. Unfortunately none of them work on my dev. machine, as all the classes used by these example are not found by my server.

For instance when I use this code:

<#assign journalArticleResourceLocalServiceUtil = serviceLocator.findService("com.liferay.portlet.journal.service.JournalArticleLocalService") />


I get this:
Expression serviceLocator is undefined on line 18, column 51 in 20155#20195#23618.

When I try:

<#assign journalArticleResourceService = portal.getClass().forName("com.liferay.portlet.journal.service.JournalArticleResourceLocalServiceUtil").getMethod("getService", null).invoke(null, null)/>


I get :
Expression portal.getClass().forName is undefined on line 16, column 42 in 20155#20195#23618.

When I try:
<#assign journalArticleResourceLocalServiceUtil = staticUtil["com.liferay.portlet.journal.service.JournalArticleResourceLocalServiceUtil"]>

I get a message that staticUtil is undefined.

Everything I try with the Freemarker objects like "serviceLocator", "portal" or "staticUtil" won't work. What am I missing? Do I have to initialize or to include something?
thumbnail
Christoph Rabel, modified 7 Years ago.

RE: Need Help with Freemarker WCM Templates

Liferay Legend Posts: 1554 Join Date: 9/24/09 Recent Posts
Yes, access to serviceLocator is forbidden by default.

Please add the following property to portal-ext.properties:
freemarker.engine.restricted.variables=

I am not a 100% sure, but I think you can use staticUtil only in ADTs, you can't use it in a Journalarticle template.
thumbnail
Olaf Kock, modified 7 Years ago.

RE: Need Help with Freemarker WCM Templates

Liferay Legend Posts: 6396 Join Date: 9/23/08 Recent Posts
Correct answer - note that this inherently implies trust to all your template authors that they don't abuse the ability to have access to all *LocalServices - that's the full Liferay API with no permission checks (which is why it's disabled by default)
Klaus Bachmaier, modified 7 Years ago.

RE: Need Help with Freemarker WCM Templates

Regular Member Posts: 223 Join Date: 9/30/13 Recent Posts
Thanks for your immediate answers.

I' ve added this to my portal-ext.properties, and restarted the Server:

freemarker.engine.restricted.classes=
freemarker.engine.restricted.variables=

But I still get the same Error when using this:

<#assign journalArticleResourceLocalServiceUtil = serviceLocator.findService("com.liferay.portlet.journal.service.JournalArticleLocalService") /> 


Expression serviceLocator is undefined on line 18, column 51 in 20155#20195#23618.
thumbnail
Christoph Rabel, modified 7 Years ago.

RE: Need Help with Freemarker WCM Templates

Liferay Legend Posts: 1554 Join Date: 9/24/09 Recent Posts
Liferay Version?

I just tried it, it works for me with Liferay 6.2, I have these in the portal-ext.properties. Not sure if one of them makes a difference (it actually shouldn't, but who knows).
freemarker.engine.restricted.classes=
freemarker.engine.restricted.packages=
freemarker.engine.restricted.variables=

velocity.engine.restricted.classes=
velocity.engine.restricted.packages=
velocity.engine.restricted.variables=

Just to be paranoid: Could you check that these fields are really set? Control Panel -> Configuration -> Server Administration -> Portal Properties.
Klaus Bachmaier, modified 7 Years ago.

RE: Need Help with Freemarker WCM Templates

Regular Member Posts: 223 Join Date: 9/30/13 Recent Posts
Thanks for your support Christoph, now suddenly it seems to work, at least I don't get any Errors when I use :

<#assign JournalArticleLocalService = serviceLocator.findService("com.liferay.portlet.journal.service.JournalArticleLocalService")>


...and there are a couple of new service variables available beneath the template editor area, so the properties seem to take effect.

Unfortunately I still have no Idea on how to use the service now. For example I tried

<#assign articleResourcePK = JournalArticleLocalService.getArticleResourcePrimKey(groupId, .vars['reserved-article-id'].data)  /> 
<#assign categoryList=assetCategoryLocalServiceUtil.getCategories("com.liferay.portlet.journal.model.JournalArticle",articleResourcePK) >


...and get this Error:

Expression JournalArticleLocalService.getArticleResourcePrimKey is undefined on line 22, column 30 in 20155#20195#23618.


It doesn't matter which method of JournalArticleLocalService I use, the method is always "undefined".

So I'm still stuck and can't my list of categories for the article.
Klaus Bachmaier, modified 7 Years ago.

RE: Need Help with Freemarker WCM Templates

Regular Member Posts: 223 Join Date: 9/30/13 Recent Posts
Ok I found this thread, and I'm fine now. Everything works:
https://web.liferay.com/de/community/forums/-/message_boards/message/41897445
trj lee, modified 7 Years ago.

RE: Need Help with Freemarker WCM Templates

New Member Posts: 2 Join Date: 1/14/16 Recent Posts
Christoph Rabel:
Liferay Version?

I just tried it, it works for me with Liferay 6.2, I have these in the portal-ext.properties. Not sure if one of them makes a difference (it actually shouldn't, but who knows).
freemarker.engine.restricted.classes=
freemarker.engine.restricted.packages=
freemarker.engine.restricted.variables=

velocity.engine.restricted.classes=
velocity.engine.restricted.packages=
velocity.engine.restricted.variables=

Just to be paranoid: Could you check that these fields are really set? Control Panel -> Configuration -> Server Administration -> Portal Properties.

Hi Christoph Rabel, i use Liferay 7, i had add
freemarker.engine.restricted.classes=
freemarker.engine.restricted.packages=
freemarker.engine.restricted.variables=

in the portal-ext.properties but it is not work. This error:
The following has evaluated to null or missing:
==> serviceLocator  [in template "20115#20159#21341" at line 1, column 34]
----
Tip: If the failing expression is known to be legally refer to something that's sometimes null or missing, either specify a default value like myOptionalVar!myDefault, or use &lt;#if myOptionalVar??&gt;when-present&lt;#else&gt;when-missing<!--#if-->. (These only cover the last step of the expression; to cover the whole expression, use parenthesis: (myOptionalVar.foo)!myDefault, (myOptionalVar.foo)??

Can you help me?
thumbnail
Christoph Rabel, modified 7 Years ago.

RE: Need Help with Freemarker WCM Templates

Liferay Legend Posts: 1554 Join Date: 9/24/09 Recent Posts
In Liferay 7 the configuration was moved from portal.properties to the new configuration store.

Control Panel -> Configuration -> System Settings -> Foundation -> FreeMarker Engine -> Restricted Variables.

There you can remove serviceLocator from the list. You probably want to restart afterwards otherwise the setting won't work immediately.
Please BE CAREFUL: There's a bug, you need to enter something valid like "java.lang.String" into the allowedClasses field or otherwise Liferay 7 won't start up after saving that form! That field must not contain an empty string!
trj lee, modified 7 Years ago.

RE: Need Help with Freemarker WCM Templates

New Member Posts: 2 Join Date: 1/14/16 Recent Posts
Thanks Christoph Rabel,I will try it. Otherwise, I find a way. That is create 1 file com.liferay.portal.template.freemarker.configuration.FreeMarkerEngineConfiguration.cfg copy into address: (site)\bundles\osgi\configs\ . In file i write
restrictedVariables=
It worked. Do you think it correct way?
thumbnail
Vincent Groß, modified 6 Years ago.

RE: Need Help with Freemarker WCM Templates

New Member Posts: 8 Join Date: 8/13/13 Recent Posts
I know this thread is a year old now, but when I search for 'liferay dxp serviceLocator ' this is the top result.
Sad to see that there is no real solution provided to that problem.
You can either use the control-panel, remove the restriction and hope your portal will work after that (if not, be faced with hours of additional work, I guess) or create that freemarkerengine config file as mentioned above.

Sadly though, that config part doesn't work for me!
Is there a new or better solution to this?
Do I have to do something for my OSGI configs to work?
thumbnail
Christoph Rabel, modified 6 Years ago.

RE: Need Help with Freemarker WCM Templates

Liferay Legend Posts: 1554 Join Date: 9/24/09 Recent Posts
The issue is fixed in GA4.
In any case, both given solutions work. You can either configure this in the frontend (but in GA3 you have to be a bit careful) or using the module configuration by file.

Could you explain what you are doing?
thumbnail
Vincent G., modified 6 Years ago.

RE: Need Help with Freemarker WCM Templates

New Member Posts: 8 Join Date: 8/13/13 Recent Posts
Christoph Rabel:
The issue is fixed in GA4.
In any case, both given solutions work. You can either configure this in the frontend (but in GA3 you have to be a bit careful) or using the module configuration by file.

Could you explain what you are doing?


Sure. I have created a workspace and added a theme in folder themes/ that I created using the Theme Generator.
I then created the file 'com.liferay.portal.template.freemarker.configuration.FreeMarkerEngineConfiguration.cfg' as mentioned above inside <portal_home>/osgi/configs with the following content: restrictedVariables=

Did I do something wrong?

EDIT: I am using liferay-dxp-digital-enterprise-tomcat-7.0-sp1-20161027112321352
thumbnail
Christoph Rabel, modified 6 Years ago.

RE: Need Help with Freemarker WCM Templates

Liferay Legend Posts: 1554 Join Date: 9/24/09 Recent Posts
Hmm, that should work.
Did you restart after making that configuration?
thumbnail
Vincent G., modified 6 Years ago.

RE: Need Help with Freemarker WCM Templates

New Member Posts: 8 Join Date: 8/13/13 Recent Posts
Christoph Rabel:
Hmm, that should work.
Did you restart after making that configuration?


I guess that was the problem.
Now, when I start up though, I am faced with a new Exception:


14:24:35,880 INFO  [Start Level: Equinox Container: e0d6a5d3-5662-0017-1f26-f84065a78e69][BundleStartStopLogger:35] STARTED com.liferay.util.taglib_2.5.1 [544]
06-Jul-2017 14:24:35.889 SEVERE [localhost-startStop-1] org.apache.catalina.core.StandardContext.listenerStart Exception sending context initialized event to listener instance of class com.liferay.portal.spring.context.PortalContextLoaderListener
 java.lang.RuntimeException: java.nio.file.FileSystemNotFoundException: D:\liferay-portal-dxp\osgi\marketplace\Liferay Collaboration.lpkg
	at com.liferay.portal.spring.context.PortalContextLoaderListener.contextInitialized(PortalContextLoaderListener.java:256)
	at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4812)
	at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5255)
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147)


Does this have to do with the configuration I made? Seems to have to do with marketplace although I did not modify it.
thumbnail
Christoph Rabel, modified 6 Years ago.

RE: Need Help with Freemarker WCM Templates

Liferay Legend Posts: 1554 Join Date: 9/24/09 Recent Posts
Is the file "D:\liferay-portal-dxp\osgi\marketplace\Liferay Collaboration.lpkg" there?
It's in the Liferay Zip file, so it should be there.

Did you, per chance, apply a Liferay patch(provided you are using EE)? You absolutely need to delete the osgi/state folder after patching. It might also help you here. Stop the server, delete the folder, start it again. It will take some extra time. Deleting the state folder is perfectly fine, it contains only "temporary" files.
thumbnail
Vincent G., modified 6 Years ago.

RE: Need Help with Freemarker WCM Templates

New Member Posts: 8 Join Date: 8/13/13 Recent Posts
Yes, the file ist there.
And yes, I patched my instance recently.

I deleted the folder (which seemed to help), started up and get an error:
freemarker.core.InvalidReferenceException: The following has evaluated to null or missing:_==&gt; serviceLocator.findService("com.liferay.portal.service.LayoutLocalService")

I looked into Config > Foundation > Freemarker and saw that nothing changed. Still, my config lies in <lr-portal>/osgi/configs and is somehow registered in the database aswell. If I look into *.configuration, I see a table 'com.liferay.portal.template.freemarker.configuration.FreeMarkerEngineConfiguration' with value 'felix.fileinstall.filename="file:/D:/liferay-portal-dxp/osgi/configs/com.liferay.portal.template.freemarker.configuration.FreeMarkerEngineConfiguration.cfg"
restrictedVariables=""
service.pid="com.liferay.portal.template.freemarker.configuration.FreeMarkerEngineConfiguration"'

Is there any mistake I made?
thumbnail
Vincent G., modified 6 Years ago.

RE: Need Help with Freemarker WCM Templates

New Member Posts: 8 Join Date: 8/13/13 Recent Posts
To save time, I have simply done it via configuration in portal.
Next Error I'm getting is:

10:30:54,310 ERROR [http-nio-8080-exec-1][error_jsp:670] javax.servlet.ServletException: javax.servlet.jsp.JspException: com.liferay.portal.kernel.template.TemplateException: Unsupported template manager ftl
javax.servlet.ServletException: javax.servlet.jsp.JspException: com.liferay.portal.kernel.template.TemplateException: Unsupported template manager ftl
	at org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:905)
	at org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:838)
	at com.liferay.taglib.servlet.PageContextWrapper.handlePageException(PageContextWrapper.java:164)
	at org.apache.jsp.html.common.themes.portal_jsp._jspService(portal_jsp.java:721)
thumbnail
Christoph Rabel, modified 6 Years ago.

RE: Need Help with Freemarker WCM Templates

Liferay Legend Posts: 1554 Join Date: 9/24/09 Recent Posts
Oh, my.

Now you are most likely hitting this issue:
Please BE CAREFUL: There's a bug, you need to enter something valid like "java.lang.String" into the allowedClasses field or otherwise Liferay 7 won't start up after saving that form! That field must not contain an empty string!

Upgrading to Patch 12 or SP2+ should fix that.

Here's a hint how to fix it manually:
https://web.liferay.com/community/forums/-/message_boards/message/83643369

P.S.:
No idea why the cfg file doesn't work. Everything looks correct.
thumbnail
Vincent G., modified 6 Years ago.

RE: Need Help with Freemarker WCM Templates

New Member Posts: 8 Join Date: 8/13/13 Recent Posts
I have updated the patching-tool, downloaded the most recent patch and installed it. I deleted state folder (even portal/work, tomcat/temp and tomcat/work) and restarted, but I still get the error I mentioned earlier.

Here's the output of the patching tool:
 D:\liferay-portal-dxp\patching-tool&gt;patching-tool.bat info
 Loading product and patch information...
 Product information:
   * installation type: binary
   * build number: 7010
   * service pack version:
     - available SP version: 3
     - installable SP version: 3
   * patching-tool version: 2.0.6
   * time: 2017-07-07 11:34Z
   * plugins: no plugins detected

Currently installed patches: de-22-7010
Available patches: de-7-7010, de-22-7010
Detailed patch list:
  [ -] de-7-7010 :: Currently not installed; Won't be installed: de-22 contains the fixes included in this one
  [*I] de-22-7010 :: Installed; Will be installed. :: Built for LIFERAY


I also did what was mentioned in the article under the link you posted and discovered, that the config file 'com.liferay.portal.template.freemarker.configuration.FreeMarkerEngineConfiguration.cfg' in portal/osgi/configs was modified and now contains the following:

restrictedVariables = [Ljava.lang.String;@b6008b6
allowedClasses = [Ljava.lang.String;@426f3cdf
localizedLookup = false
macroLibrary = [Ljava.lang.String;@13aa8072
resourceModificationCheck = 60
restrictedClasses = [Ljava.lang.String;@46b9133a
templateExceptionHandler = rethrow
thumbnail
Christoph Rabel, modified 6 Years ago.

RE: Need Help with Freemarker WCM Templates

Liferay Legend Posts: 1554 Join Date: 9/24/09 Recent Posts
No idea what the problem is.
I'd try to delete the cfg file (or maybe move it away). Maybe it works afterwards. If so, I would try to repeat the steps to remove the restricted variables again through the frontend.

And then I would open a ticket with Liferay. Creating the file should work and your server should start up without an issue.
thumbnail
Vincent G., modified 6 Years ago.

RE: Need Help with Freemarker WCM Templates

New Member Posts: 8 Join Date: 8/13/13 Recent Posts
I replaced the content of the file with
restrictedVariable=
, restarted and it finally worked! Thanks for your support, Christopher!
thumbnail
Yannis Sinadinos, modified 6 Years ago.

RE: Need Help with Freemarker WCM Templates

Junior Member Posts: 39 Join Date: 3/20/17 Recent Posts
Christoph Rabel:
No idea what the problem is.
I'd try to delete the cfg file (or maybe move it away). Maybe it works afterwards. If so, I would try to repeat the steps to remove the restricted variables again through the frontend.

And then I would open a ticket with Liferay. Creating the file should work and your server should start up without an issue.



Hi, is there a clear solution for this issue? I followed the same steps and didn't work for me...

The set "restrictedValiables = " to cfg file in ..bundle/osgi/config folder but it is not working.

I try to use staticUtil method

    &lt;#assign fileEntry = staticUtil["com.liferay.document.library.kernel.service.DLFileEntryLocalServiceUtil"]&gt;
    &lt;#assign file = fileEntry.getFileEntryByUuidAndGroupId(uuId,groupId)&gt;


and i get the following error:

No error description was specified for this error; low-level message: java.lang.ClassNotFoundException: com.liferay.document.library.kernel.service.DLFileEntryLocalServiceUtil cannot be found by com.liferay.portal.template.freemarker_2.0.24
----
FTL stack trace ("~" means nesting-related):
	- Failed at: #assign fileEntry = staticUtil["com.l...  [in template "20116#20152#45193" at line 26, column 5]
Masoud Salehisedeh, modified 5 Years ago.

RE: Need Help with Freemarker WCM Templates

New Member Posts: 4 Join Date: 9/20/18 Recent Posts

I am trying to display content of a journal from within a journal. I went to "Control Panel -> Configuration -> System Settings -> Template Engines -> FreeMarker Engine -> Restricted Variables" and pretty much removed all variables and classes.  I only have java.lang.Class and java.lang.Thread in restricted classes and still get the error. my code : 

<#assign journalArticleService = serviceLocator.findService("com.liferay.journal.service.JournalArticleLocalService") /> 
<#assign paragraphArticle = journalArticleService.fetchLatestArticle(author_map.getLong("classPK")) /> 
<#if paragraphArticle??> ${journalArticleService.getArticleContent(paragraphArticle, paragraphArticle.getDDMTemplateKey(),  "", languageUtil.getLanguageId(locale), themeDisplay)}</#if> 

 

What Am I Missing?

Michael El Khoury, modified 4 Years ago.

RE: Need Help with Freemarker WCM Templates

New Member Posts: 9 Join Date: 9/18/19 Recent Posts
Masoud, what error you're getting exacly? i might be able to help
Lee Jordan, modified 4 Years ago.

RE: Need Help with Freemarker WCM Templates

Expert Posts: 449 Join Date: 5/26/15 Recent Posts
Christoph Rabel:

In Liferay 7 the configuration was moved from portal.properties to the new configuration store.

Control Panel -> Configuration -> System Settings -> Foundation -> FreeMarker Engine -> Restricted Variables.



7.2 Update add another step in the navigation ...

Control Panel -> Configuration -> System Settings -> Platform -> Template Engines -> FreeMarker Engine -> Restricted Variables - Repeatable Fields

And I found this file
https://github.com/liferay/liferay-portal/blob/master/modules/apps/portal-template/portal-template-freemarker/src/main/java/com/liferay/portal/template/freemarker/configuration/FreeMarkerEngineConfiguration.java
With:
@Meta.AD(
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;deflt = "httpUtilUnsafe|objectUtil|serviceLocator|staticFieldGetter|staticUtil|utilLocator",
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;name = "restricted-variables", required = false
&nbsp;&nbsp; &nbsp;)
thumbnail
Joshua Turner, modified 5 Years ago.

RE: Need Help with Freemarker WCM Templates

New Member Posts: 2 Join Date: 8/11/15 Recent Posts

This is friggin crazy.

Lee Jordan, modified 4 Years ago.

RE: Need Help with Freemarker WCM Templates

Expert Posts: 449 Join Date: 5/26/15 Recent Posts
Just adding to this, slightly different need. Not having that navigation tree in the product menu is a huge barrier to an upgrade. Our users will have lost the ability to navigate their sites. Often the pages are "hidden from navigation". So the Sitemap portlet isn't working out as a replacement for the lost tree.

I too have spent too much time trying to get a list of site pages from Liferay. I could swear but I won't. I looked at it before Christmas and have picked it back up and still no hope in sight for accessing these variables.

Caused by: freemarker.core.InvalidReferenceException: The following has evaluated to null or missing:_==&gt; serviceLocator.findService("com.liferay.portal.service.LayoutLocalServiceUtil") &nbsp;[in template "mercury-theme_SERVLET_CONTEXT_/templates/init_custom.ftl" at line 55, column 26]__

Miserable.  I'm a front end developer and can't access template variables? Absolutely miserable experience and a waste of time tying to put on a band aid to something that should never have been removed in the first place. As a front end dev I don't have the knowledge to go fix your product for me to restore a feature that should never have been removed.
Lee Jordan, modified 3 Years ago.

RE: Need Help with Freemarker WCM Templates

Expert Posts: 449 Join Date: 5/26/15 Recent Posts
How can I sell this proposition to my technical lead? I need to open up production to vulnerabilities and possibly have the server not restart ... in order to complete a template??
thumbnail
Christoph Rabel, modified 3 Years ago.

RE: Need Help with Freemarker WCM Templates

Liferay Legend Posts: 1554 Join Date: 9/24/09 Recent Posts
We use serviceLocator quite often, BUT there is a cleaner approach. You can add your own services to templates and call them from there:
https://help.liferay.com/hc/en-us/articles/360018170931-Template-Context-Contributor
Please note that it only works "as expected" from 7.0.0 DXP SP7 and up:
https://issues.liferay.com/browse/LPS-76631