Developer forum

Forum » Templates » RE: Reference Page item File-field to image src

RE: Reference Page item File-field to image src

Dmitrij Jazel
Reply

Hej Guys,

Recently I created fresh site, and created a Page type item.

I added Type "File" field with name Image.

Strange thing is happening, whenever get the value from that field, and I just define: <text>@GetString("Item.Image")</text> it shows correct src that I want to use.

But the issue happens when I am trying to use something like this:

<img src="@GetString("Item.Image")" alt="test" />

For some reason dw changes src and generates this:

<img src="/Files/Templates/Designs/newsite2015//Files/Images/banner/test.jpg" alt="test" /> 

When <text>@GetString("Item.Image")</text> generates "/Files/Images/banner/test.jpg" as needed.

As soon as I put it inside image src it attempts to add design folder of the website.

Does anyone has any suggestions, ideas why this can happen?


Replies

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply
This post has been marked as an answer

Hi Dmitrij,

Take a look here: http://developer.dynamicweb.com/forum/templates/file-item-property-in-image-tag.aspx

The double quotes on the src attribute and in the call to GetString mess things up. Either use single quotes for the src attribute or put the value in a variable first:

@{
  var imagePath = GetString("Item.Image");
  <img src="@imagePath" alt="test" />
}

Hope this helps,

Imar

Votes for this answer: 1
 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

Oh. looking at this more closely, I see it's a different issue. The solution might still be the same though.

Imar

 
Dmitrij Jazel
Reply

Hej Imar,

Well I just used that just to save some lines :)

my code so far

string src = GetString("Item.Image");
if(!string.IsNullOrEmpty(src)){
    <li>
      <img src="@src" alt="test" />
    </li>                    
}

But there is no effect, it just does exactly the same.

 
Mikkel Ricky
Reply

The issue here is that the layout template parser prepends the layout template folder to all relative url references in a layout template. However, this should not be done when the attribute value is a template tag (i.e. starts with @ or <!--@) and this bug has been fixed in Dynamicweb 8.5.1. Which version of Dynamicweb are you using?

See this post for a possible workaround: http://developer.dynamicweb.com/forum/templates.aspx?ThreadID=37356

Best regards,
Mikkel 

 
Dmitrij Jazel
Reply

Hehe, well that was unexpected...

But it worked...

<text>@("<")img src='@src'  alt="test" /@(">")</text>

Thanks Mikkel :)

 

You must be logged in to post in the forum