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
Widget template: Get image path
Hi!
I´m developing a widget template for a structure which has a image field.
I´m trying to get the path of the image inside my freemarker template using this code
<#list entries as entry>
<#assign entry = entry />
<#assign assetRenderer = entry.getAssetRenderer() />
<#assign className = assetRenderer.getClassName() />
<#assign docXml = saxReaderUtil.read(entry.getAssetRenderer().getArticle().getContent()) />
<#assign rootElement = docXml.getRootElement() />
<#list rootElement.elements() as dynamicElement>
<#if dynamicElement.attributeValue("name") == "IMAGE">
<#assign imagePath = dynamicElement.element("dynamic-content").getText() />
</#if>
</#list>
</#list>
"IMAGE" is the name of the image field in the structure.
Then in the HTML section of the template
<img class="img-responsive" src="${imagePath}">
But when the page is rendered the src attribute of the img contains the whole object
I was wondering if there is way to get the "name" property of the element.
My LR version is Liferay Community Edition Portal 7.3.4 CE GA5
Thanks!
Check this blog post, it has a lot of code/info on the topic. Also in the comments.
https://liferay.dev/blogs/-/blogs/oh-no-my-urls-disappeared-and-how-to-get-them-back-
Thanks!
Finally i ended up using "jsonFactoryUtil.createJSONObject" to get the json fields
<#assign imageJSONString = dynamicElement.element("dynamic-content").getText()/>
<#assign imageUUID = jsonFactoryUtil.createJSONObject(imageJSONString).getString("uuid")>
<#assign siteId = themeDisplay.getSiteGroupId()>
Then in the HTML section
<img class="img-responsive" src="/documents/${siteId}/${imageUUID}" alt="">
Powered by Liferay™