Search code examples
liferayliferay-6freemarker

Liferay ADT - Freemarker Call Function From A Local Form


I am currently struggling with Liferay ADT, using freemarker language. What i want to do is simple. I just want to change a boolean field in a web content structure from true to false and vice versa.

I have achieved that by utilizing this code :

<#function expire>    
<#assign isActiveNode = docXml.selectSingleNode("//dynamic-element[@name='isActive']/dynamic-content") />
    <#assign journalArticleLocalService = serviceLocator.findService("com.liferay.portlet.journal.service.JournalArticleLocalService")>
    <#assign expireArticle = isActiveNode.setText("false") />
    <#assign expireArticle = article.setContent(docXml.asXML()) />
    <#assign expireArticle = journalArticleLocalService.updateJournalArticle(article) />
</#function>

I know the code worked because i tried it without using function. But i want the whole activity to be triggered by a click of a button. So what i want to do is to call that function from a form or a button. I can't seem to be able to do this :

<form action="${expire}" method="post">
<input type="submit" value="Expire" class="btn btn-success"/>
</form>

Calling it from button onclick event wont help either.

It says :

Expecting a string, date or number here, Expression expire is instead a freemarker.core.Macro

So how do i call that "expire" function from a html tag?

Any thoughts would be greatly appreciated.


Solution

  • As expire is a function, you should call it like ${expire()}. However, your function has no return value (no <#return someValue>), so that will also cause an error. I'm not sure what do you expect to be inserted at that ${...}, but #return it at the end of the #function.

    Also it's pointless to re-assing to the expireArticle variable again and again; you just overwrite the old value. Also, for variables that you only need inside the function it's cleaner to use <#local someVar = someValue>.