Developer forum

Forum » Templates » Did 9.4.14 change something in the Razor template engine?

Did 9.4.14 change something in the Razor template engine?

Jacob Storgaard Jensen
Reply

Hi,

I just upgraded to 9.4.14 in a DEV solution and i get some weird razor behaviour...

Consider this code:

foreach (LoopItem item in GetLoop("Item.Something"))
{
    <a href="@item.GetString("Item.Something.URL")" title="@item.GetString("Item.Something.Text")">@item.GetString("Item.Something.Text")</a>
}

This gives me this output:

<a href=""="" title=""="">The expected text</a>


Ealier there was some issues when using @GetString etc. inside double quotes where I used single quotes, but I haven't seen this behaviour for years...

Anyone else getting this behaviour?


Replies

 
Nicolai Pedersen
Reply
This post has been marked as an answer

Nope, nothing changed.

But you cannot do it like this in Layout template - only in module templates.

So do like this:

foreach (LoopItem item in GetLoop("Item.Something"))
{
    var url = item.GetString("Item.Something.URL")
    <a href="@url" title="@item.GetString("Item.Something.Text")">@item.GetString("Item.Something.Text")</a>
}

And do the same for the other attributes.

Basically - always use variables in attribute values and not method calls

BR Nicolai

Votes for this answer: 1
 
Jacob Storgaard Jensen
Reply

Thanks for the clarification! :-)

I've been using variables all the time, but when I suddenly end up making a small, quick and apparently dirty "include file" to a Layout (Master) template, I end up doing it wrong. For fuck sakes, pardon my french :-D

#beeninfrontofthescreentoolong

 
Gaëtan Di Caro
Reply

Nicolai is right in saying that you shouldn't use methods in html attributes, but for the record I have a workaround : you just need to surround the attribute value in simple quotes and not double quotes.

Instead of :

<a href="@item.GetString("Item.Something.URL")" title="@item.GetString("Item.Something.Text")">@item.GetString("Item.Something.Text")</a>

Write :

<a href='@item.GetString("Item.Something.URL")' title='@item.GetString("Item.Something.Text")'>@item.GetString("Item.Something.Text")</a>
 
Jacob Storgaard Jensen
Reply

True, been doing that. For some reason I had subconsciously been aware that I couldn't do it in layout templates, and reviewing my older templates I did do exactly the "single quotes trick". One of my occasional #brainfarts :-D

 

You must be logged in to post in the forum