Accessing Image URL from templates

Posted by Community Admin on 04-Aug-2018 23:31

Accessing Image URL from templates

All Replies

Posted by Community Admin on 07-Feb-2012 00:00

All,

We are currently using sitefinity 4.4 and looking to take advantage of the feature which allows developers to append “size=max width or height”  to the end of a query string. Then the server would resize the image based up on the size parameter.

ex

URL http://domain.com/libraries/library_name/image_name.ext?size=640

Resizes to a  maximum width or height of 640.

Our current problem is we want to add the size parameter to the query string within widget templates so we need access to the url of the following fields.

<img src="<%# Eval("PrimaryImageUrl") %>" />

  <sf:AssetsField runat="server" DataFieldName="HeroImage" />

We were hoping for something like

 <%# ( (ContentLink[]) Eval("ThumbnailImage") )[0].URL %>

 

Is this or anything similar possible?


Posted by Community Admin on 08-Feb-2012 00:00

Anthony, this should certainly be possible. The approach I use is to place a helper method in the code-behind of the template. This requires mapping an external widget template, but gives you more control over the rendering of the content properties.

What I usually do is do define a method like this:

protected string RenderImage(object contentItem)
    var item = contentItem as MyContentItem;

This allows me to pass the entire content item from the template:
<img src="<%# RenderImage(Container.DataItem) %>" />

In the method above, MyContentItem can be your custom object, or one of the built-in content items like News, or BlogPost, etc.

Adding a breakpoint in that method allows you to inspect all the properties to extract the value you might need, construct a string response, and return it to the template.

For an example of using a helper method to retrieve an image, take a look at this article: Retrieving Data From Dynamic Modules Using the Module Builder API

Hope this is helpful!

This thread is closed