Hi Dominic, You are very close and just a gotchya for the Razor syntax really.
Where you use content_@i the Razor engine doesn't know if this supposed to be HTML/text/email address so parses it as a string rather than switching back to the code parser. The way to fix this is to change it to <div id="content_@(i)"> Using brackets () after the @ tells the parser anything inside this needs to be parsed as code.
<ulclass="clearfix">@{int i = 0;}@foreach (var item in @Model.Children)
{
<liid="content_@(i)"><divclass="homepage-content-top clearfix">@item.Name
</div></li>
i ++;
}
</ul>
Increment CSS class?
Hi all,
I'm brand new to Umbraco, and from a PHP background.
I'm trying to loop through some images, and increment a class on each li.
In short, I need content_@i to increment by 1 on each iteration. I can't get the syntax correct!
Best thing is to join up your element ID variable like so:
Hi Dominic,
You are very close and just a gotchya for the Razor syntax really.
Where you use content_@i the Razor engine doesn't know if this supposed to be HTML/text/email address so parses it as a string rather than switching back to the code parser. The way to fix this is to change it to <div id="content_@(i)"> Using brackets () after the @ tells the parser anything inside this needs to be parsed as code.
Yep or you can do as Barry suggested and build your string up in a var first and then use that in your HTML
Nice one Warren - your solution is more elegant didnt know about that razor bracket syntax!
Works, thanks!
This is a good little guide to Razor syntax
http://haacked.com/archive/2011/01/06/razor-syntax-quick-reference.aspx
Ah nice thanks.
if Model.HomepageSlide returns a DynamicNodeList, you can do this:
@foreach(var item in Model.HomePageSlide)
{
<li id="[email protected]">@item.Name</li>
}
If it doesn't, this is equiv to Warren's example but slightly shorter:
@{
var i = 0;
foreach(var item in Model.HomePageSlide)
{
<li id="content_@(i++)">@item.Name</li>
}
The @(i++) returns i, and then increments it for the next loop pass.
is working on a reply...