Ask Questions and Find Answers
Important:
Ask is now read-only. You can review any existing questions and answers, but not add anything new.
But - don't panic! While ask is no more, we've replaced it with discuss - the new Liferay Discussion Forum! Read more here here or just visit the site here:
discuss.liferay.com
Liferay7 WebContent template: retrieve document title (FTL)
I have a WebContent structure with a "Documents & Media" field.
I finally figured out that the download URL is no available through getUrl() or getFriendlyUrl() but strangely enough through getData().
However I can't find a way to get the name of the document in FreeMarker.
The code generated by the template editor:
Neither getTitle() nor getTitle(locale) work (as TemplateNode does not have those methods). getName() only returns the name of the field, not the name of the document. The "attributes" map only contains the language-id
So how can I display the (localized) name of the document?
Surely I am not the first one to try something so obvious.
I finally figured out that the download URL is no available through getUrl() or getFriendlyUrl() but strangely enough through getData().
However I can't find a way to get the name of the document in FreeMarker.
The code generated by the template editor:
${languageUtil.format(locale, "download-x", "Dokument")})is pretty much useless as it displays a constant label, not the name of the document. Neither getTitle() nor getTitle(locale) work (as TemplateNode does not have those methods). getName() only returns the name of the field, not the name of the document. The "attributes" map only contains the language-id
So how can I display the (localized) name of the document?
Surely I am not the first one to try something so obvious.
Massimo Bevilacqua, modified 8 Years ago.
Regular Member
Posts: 210
Join Date: 12/27/16
Recent Posts
Hi Thomas,
I don't know if there is a better way to do it, but one solution could be this:
the ${media.getData()} is stored in this way
So you can retrieve file using Group id and uuid and then you will have all the file information
In order to use ServiceLocator you need to add these lines to your portal-ext.properties
or you can delete these values from:
Control panel -> Configuration -> System setting ->Foundation -> Freemarker engine
A server start is required
If you want you can just take the doc-title name from
in this way
but in this way if the file name has a space inside, Liferay adds a "+", my file.mp3 become my+file.mp3
Maybe in the future you will need others information so I think it is better to retrive the file instead only the name
Hope it helps
Massimo
I don't know if there is a better way to do it, but one solution could be this:
the ${media.getData()} is stored in this way
/documents/[doc-group-id]/0/[doc-title]/[doc-uuid]So you can retrieve file using Group id and uuid and then you will have all the file information
<#assign counter = 0 >
<#list "${media.getData()}"?split("/") as x>
<#if counter == 2>
<#assign groupId = x?number >
<!--#if-->
<#if counter == 5>
<#assign uuId = x >
<!--#if-->
<#assign counter = counter+1 >
<!--#list-->
<#assign dlFileEntryService = serviceLocator.findService('com.liferay.document.library.kernel.service.DLFileEntryService') />
<#assign file = dlFileEntryService.getFileEntryByUuidAndGroupId(uuId,groupId) >
<h1>${file.fileName}</h1>
In order to use ServiceLocator you need to add these lines to your portal-ext.properties
freemarker.engine.restricted.classes=
freemarker.engine.restricted.packages=
freemarker.engine.restricted.variables=
or you can delete these values from:
Control panel -> Configuration -> System setting ->Foundation -> Freemarker engine
A server start is required
If you want you can just take the doc-title name from
/documents/[doc-group-id]/0/[doc-title]/[doc-uuid]in this way
<#if counter == 4>
<#assign name = x >
<!--#if-->but in this way if the file name has a space inside, Liferay adds a "+", my file.mp3 become my+file.mp3
Maybe in the future you will need others information so I think it is better to retrive the file instead only the name
Hope it helps
Massimo
Thanks, that worked.