Message Boards

Widget template: Get image path

Alejandro González Jiménez, modified 3 Years ago.

Widget template: Get image path

New Member Posts: 7 Join Date: 6/24/20 Recent Posts

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!

thumbnail
Christoph Rabel, modified 3 Years ago.

RE: Widget template: Get image path

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

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-

Alejandro González Jiménez, modified 3 Years ago.

RE: RE: Widget template: Get image path

New Member Posts: 7 Join Date: 6/24/20 Recent Posts

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="">