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
RE: Freemarker webcontent template img element without height width attribu
When I add IMG element inside freemarker template (web content template) it appears without attributes width and height. Are there any possible ways to automatically get them there?
I use Liferay 7.0
I use Liferay 7.0
I don't understand what you want to accomplish?
Why don't you just write the width/height directly into the image tag? Or do you want to insert the width/height of the image? If so, WHY?
Why don't you just write the width/height directly into the image tag? Or do you want to insert the width/height of the image? If so, WHY?
I can't define exactly width and height (hardcode) in the template because when people add web content and select image they can choose different images with different sizes.
Yes, I want to insert width height into img attributes width height kinda dynamically. It is necessary to prevent page jumping while images loading. Communicative Layout Shift metric
Yes, I want to insert width height into img attributes width height kinda dynamically. It is necessary to prevent page jumping while images loading. Communicative Layout Shift metric
Nikita Laitinen:
What do you write in your template?
When I add IMG element inside freemarker template (web content template) it appears without attributes width and height. Are there any possible ways to automatically get them there?
I use Liferay 7.0
<#if image.getData()?? && image.getData() != "">
<img data-fileentryid="${image.getAttribute(" fileEntryId")}" alt="${image.getAttribute(" alt")}" src="${image.getData()}">
<!--#if-->
This code automaticcly is added when I click on the image variable in fields section of template editing mode
That code is just an example, you can freely edit it.
We often insert the image as a background image into a fixed width/height container. That way the image is automatically truncated. But please note:
There is no absolute, always correct way to do this. If an image with the wrong aspect ration is used, something of the image will be truncated. Often that's acceptable, sometimes it simply isn't. The best way to fix this is to force the editors to upload images with the right aspect ratio. e.g.:
And then you set a width/height in the css class (here card__image--small).
We often insert the image as a background image into a fixed width/height container. That way the image is automatically truncated. But please note:
There is no absolute, always correct way to do this. If an image with the wrong aspect ration is used, something of the image will be truncated. Often that's acceptable, sometimes it simply isn't. The best way to fix this is to force the editors to upload images with the right aspect ratio. e.g.:
<div data-cmh="small-card-img" class="card__image card__image--small" style="background-image: url('${image.getData()}')"></div>
And then you set a width/height in the css class (here card__image--small).
Yes, it is one of the approaches
Thank you!
Thank you!
Copyright © 2025 Liferay, Inc
• Privacy Policy
Powered by Liferay™