So far my first steps into Umbraco are going reasonably well..but every now and then I will bump into something I don't know how to do yet.
I am building a list of techniques, that are build up from a parent node, that is added as a parameter to a macro. This macro uses razor to build up the list, however...our designer found it funny to build it conditionally. Each 4th item gets a different style compared to the others.
Right now I have this working, but I am sure this can be done more ellegantly.
The below script is how I am doing this right now. As you can see I have tried to do it differently, but using a classextender, which I would set to " space" or nothing depending on the teller. I then wanted to concatenate that to the "class=technieken" part for the first three, and not for the last one.
Well I hope so. ;) I do % 4 != 0 so every first 3 will always get the classextender set to " space" just every 4th one, which would result in the modulus being 0, gets the other class.
I changed the script to what you see below, but get an error message.
Error loading Razor Script GetTechnieken.cshtml c:\inetpub\wwwroot\ecologics\Umbraco.Website\macroScripts\GetTechnieken.cshtml(16): error CS0201: Only assignment, call, increment, decrement, and new object expressions can be used as a statement
What if I want the class attribute itself to by dynamic? For example in an li list only the last li needs the class="last". With xslt I could use xsl:attribute inside an if statement. How can I do that with razor? Can I just do this?
You can do it with First() and Last() in 4,7.1. For now you can first count the results and do a for loop instead of a foreach loop. So when i == myCount you can add the "last" class.
Hy Sebastiaan i know i'm posting terrible questions :) could you have a look at my post please ? Need some help for solving and integrating it into a customer project.
dynamic class allocation with razor?
Hey there,
So far my first steps into Umbraco are going reasonably well..but every now and then I will bump into something I don't know how to do yet.
I am building a list of techniques, that are build up from a parent node, that is added as a parameter to a macro. This macro uses razor to build up the list, however...our designer found it funny to build it conditionally. Each 4th item gets a different style compared to the others.
Right now I have this working, but I am sure this can be done more ellegantly.
The below script is how I am doing this right now. As you can see I have tried to do it differently, but using a classextender, which I would set to " space" or nothing depending on the teller. I then wanted to concatenate that to the "class=technieken" part for the first three, and not for the last one.
I tried using
<div onClick="window.location.href='@c.Url';" class="technieken" + classextender>
<div onClick="window.location.href='@c.Url';" class="technieken"@classextender>
None of those tries worked so I am now stuck with repeating the code which is silly. How do I concat that classextender in razor? Thanks people.
Actually, you're almost there:
<div onClick="window.location.href='@c.Url';" class="technieken@classextender">
The last quotation mark was moved to the end. :-)
By the way, doing "% 4 != 0" is not the same as "> 3", but I'm guessing you know what you're doing there. =)
Well I hope so. ;) I do % 4 != 0 so every first 3 will always get the classextender set to " space" just every 4th one, which would result in the modulus being 0, gets the other class.
I changed the script to what you see below, but get an error message.
Error loading Razor Script GetTechnieken.cshtml
c:\inetpub\wwwroot\ecologics\Umbraco.Website\macroScripts\GetTechnieken.cshtml(16): error CS0201: Only assignment, call, increment, decrement, and new object expressions can be used as a statement
*slaps his forehead*
That was just stupid, that needed to be
classextender = "space";
Not
classextender == "space";
Let's just say it is early. But now this is fixed.
Oh, I rewrote it a tiny bit:
See, I knew you knew what you were doing. :)
Oh actually, you also need to put a space in:
If that doesn't work, try to output @classextender elsewhere and see what happens. Maybe if it's empty your need to do @classextender.ToString()?
Ah, excellent, we're posting at the same time. No extra space needed then, as you really want to concat them. Great, glad it works!
What if I want the class attribute itself to by dynamic? For example in an li list only the last li needs the class="last". With xslt I could use xsl:attribute inside an if statement. How can I do that with razor? Can I just do this?
Jeroen
You can do it with First() and Last() in 4,7.1. For now you can first count the results and do a for loop instead of a foreach loop. So when i == myCount you can add the "last" class.
Hy Sebastiaan i know i'm posting terrible questions :) could you have a look at my post please ? Need some help for solving and integrating it into a customer project.
Here is the Link Post
is working on a reply...