Wow what a complicated mess. I don't care for C# so have attempted to use VB with Razor and have found it to be the most frustrating and ass backward "language" implementation I Have ever seen.
The old ASP days with <% amd %> made perfect sense. This @ crap is more than silly and the documentation is laughable at best. I have spent the better part of two days trying to write a SIMPLE razor macro for my site. How in the world is this progress?
Sorry to hear about your frustration with razor. Having used both razor and xslt I now prefer razor and I know a few experienced Umbraco developers feel the same way. I haven't used VB with Razor so not sure if there is a particular problem with it or not. Could you let us know what you were struggling with and maybe someone will be able to help.
I have visited both of those links and 3 dozen others. I can certainly see the power in Razor but am absolutely dumbfounded at the clunky semantics that are more than a little ambiguous with regard to the @ delimeter. COBOL was easier to read and write.
It would be SO MUCH easier to wrap ALL code in <% %> tags. Trying to mix heavily marked up HTML with code is a disaster when some elements need an @ and others don't. Adding a single pair of div tags split around a few if-then blocks and you end up having to move a dozen @ delimters to get things working again, if at all. I just don't get it.
I agree it can be a little confusing an you can end up with a messy code depending what you are doing, I really liked the way the Spark view engine handled this.
Found this (http://stackoverflow.com/questions/1451319/asp-net-mvc-view-engine-comparison) great comparison on StackOverflow.
Can you show the code for your pair of divs example?
Would be interesting to see if it can be refactored.
I guess my big question is WHY would anybody dream up such a messy and hard to use syntax when there is cleary a better way, both from a readability standpoint and a coding standpoint?
@Inherits umbraco.MacroEngines.DynamicNodeContext
@Code
Dim nodeSelection = Model.Children.items
Dim odd as boolean = true
If Not IsNothing(nodeSelection) then
@<div class="foo">
@For each item in nodeSelection
@<div>
@item.name
</div>
next
</div>
End If
End Code
The follow code DOES NOT COMPILE
Error occured
The "Code" block was not terminated. All "Code" statements must be terminated with a matching "End Code".
@Inherits umbraco.MacroEngines.DynamicNodeContext
@Code
Dim nodeSelection = Model.Children.items
Dim odd as boolean = true
If Not IsNothing(nodeSelection) then
@<div class="foo">
@<div class="foofoo">
@For each item in nodeSelection
@<div>
@item.name
</div>
next
</div>
</div>
End If
End Code
Hi, I love razor, it has made my productivity much higher than when I used xslt. I, and most others I guess, do use c# syntax though.
One difference I noticed between vbhtml and cshtml is in vbhtml you always need to escape the first html tag with @ (c# understands < starts a html block, but in vb.net it could be a xml literal, and you need a @ afaiu)
In your sample above you should remove the @ before <div class="foofoo"> since you already are in a html block.
Are you using Visual Studio (or WebMatrix) for your macros? I'm asking because when I started learning Razor I used the Umbraco code editor, but that was a bit painful sometimes, and one reason was because of the block scopes. VS and WM has great intellisense and syntax highlighting help there.
(Another note: I find it nice to separate pure code from view logic, so I put code blocks first and create whatever view variables I need, then I only use for and if's in the html part:
I love Razor, it's a lot more intuïtive and readable than the clunky <%= ... %> syntax. But then, I'm a C# developer. I understand your frustration, @<div clas="foo"> doesn't make sense to me either.
Maybe it's time for you to switch from VB to C#. I once started programming in VB too, then I switched to C# and never looked back.
When I started learning Razor, I also had difficulties with it. Then I started to understand that Razor just needs to know when you're in - my case - C# code. So when you go from html to code, just prepend your code with '@'. For example:
<p>this is html markup, <a href="@product.url">link to my product"</a></p>
In the above example, you can see that I somehow have to my clear to Razor that 'produc.url' is not html, but that it's a variable that needs to be processed, hence I prepend my variable with '@'. The beauty of Razor, is that is knows that only 'produc.url' needs to be processed. With .aspx , I would have to fully enclose the 'product.url' variable, like so: <%= product.url %>.
When you're switching from code to html, but there is no html tag to make it clear to Razor that it's html, you can prepend your text with @: For example:
<ul> @foreach (var product in products) { @:this is no code <li>@product.name</li> @:this is no code } </ul>
I prefer @: above <text>....</text>, because it doesn't need a closing statement.
I also found out that editing Razor in Visual Studio is a great help, because the Intellisense shows me when the code is interpreted as C# code or as static html markup.
First I would like to say your ability to upset and annoy contributors and users of Umbraco and its' forum isnt without some level of praise! I say this because I have struggled for months with a part of Umbraco thats not even FREE and that's Courier 2. Since the day of it's release it's been a total mess and with every incremental release over TWO YEARS the product has become more and more unstable and unusable....In the end I had to get vocal too! here....http://our.umbraco.org/forum/umbraco-pro/courier/41759-LOBBY-THE-UMBRACO-TEAM!!!-COURIER-IS-LETTING-UMBRACO-DOWN!!
Anyway, here's my ten pence worth on Razor....
1. It's Microsoft's baby and therefore it's here to stay, the alternative is MVC client side ala Angular.JS! Which the UI in U7 is actually based on!
2. There were a few templating engines initially on offer when MVC first came to be and the MS team seemed to settle on Razor presumably due to it's ability retain a closeness to C#/VB syntax.
3. I totally agree going from XSLT to Razor is a massive culture shock and an uphill struggle, but you can draw many positives and negatives from both engines! That's even with considerable C# experience!
4. As a designer/developer going from Classic ASP to ASP.net WebForms seemed initially like I was losing all control of my client side code and in many respects I did, but the page lifecycle and event model was (in my view) well worth that loss. In time WebForms controls improved to the point (along with third party controls) that I didnt care anyway!
5. Remember Umbraco's implementation of Razor is an abstraction away from the other 2 parts of MVC, the model and the controller...you need not concern yourself with these unless you need to write anything akin to the old user controls for incorporating information thats not part of your Umbraco tree. If you do find yourself in that boat you may find that Razor actually makes more sense because you can then write an extremely focused view which may only incorporate a bunch of @paramaters and HTML. So in some respects Umbraco views can "potentially" become far more complicated and code heavy than even MS originally intended!
6. Stick with it and learn it...overall I believe it is easier to read through than XSLT. I've found XSLT to be quite abstract at times, due to it's recursive nature. Also if you ever delve in the Umbraco APIs you don't have to continually put on different code hats, but stick with C# over VB!!!
My thoughts on Razor
Just my two cents...
Wow what a complicated mess. I don't care for C# so have attempted to use VB with Razor and have found it to be the most frustrating and ass backward "language" implementation I Have ever seen.
The old ASP days with <% amd %> made perfect sense. This @ crap is more than silly and the documentation is laughable at best. I have spent the better part of two days trying to write a SIMPLE razor macro for my site. How in the world is this progress?
Back to XSLT for me... this is crazy.
Sorry to hear about your frustration with razor. Having used both razor and xslt I now prefer razor and I know a few experienced Umbraco developers feel the same way. I haven't used VB with Razor so not sure if there is a particular problem with it or not. Could you let us know what you were struggling with and maybe someone will be able to help.
You have probably already read Scott Gu's introduction to razor (http://weblogs.asp.net/scottgu/archive/2010/07/02/introducing-razor.aspx), it is a good resrouce. The core team have put together some great videos as well that might help - http://umbraco.com/help-and-support/video-tutorials/umbraco-fundamentals/razor.aspx.
Hopefully we can help if you let us know what parts you were struggling with.
I have visited both of those links and 3 dozen others. I can certainly see the power in Razor but am absolutely dumbfounded at the clunky semantics that are more than a little ambiguous with regard to the @ delimeter. COBOL was easier to read and write.
It would be SO MUCH easier to wrap ALL code in <% %> tags. Trying to mix heavily marked up HTML with code is a disaster when some elements need an @ and others don't. Adding a single pair of div tags split around a few if-then blocks and you end up having to move a dozen @ delimters to get things working again, if at all. I just don't get it.
I agree it can be a little confusing an you can end up with a messy code depending what you are doing, I really liked the way the Spark view engine handled this.
Found this (http://stackoverflow.com/questions/1451319/asp-net-mvc-view-engine-comparison) great comparison on StackOverflow.
Can you show the code for your pair of divs example?
Would be interesting to see if it can be refactored.
I guess my big question is WHY would anybody dream up such a messy and hard to use syntax when there is cleary a better way, both from a readability standpoint and a coding standpoint?
The follow code DOES NOT COMPILE
Error occured
The "Code" block was not terminated. All "Code" statements must be terminated with a matching "End Code".
Hi, I love razor, it has made my productivity much higher than when I used xslt. I, and most others I guess, do use c# syntax though.
One difference I noticed between vbhtml and cshtml is in vbhtml you always need to escape the first html tag with @ (c# understands < starts a html block, but in vb.net it could be a xml literal, and you need a @ afaiu)
In your sample above you should remove the @ before <div class="foofoo"> since you already are in a html block.
A very good article about vbhtml : http://www.asp.net/web-pages/tutorials/basics/asp-net-web-pages-visual-basic
Are you using Visual Studio (or WebMatrix) for your macros? I'm asking because when I started learning Razor I used the Umbraco code editor, but that was a bit painful sometimes, and one reason was because of the block scopes. VS and WM has great intellisense and syntax highlighting help there.
(Another note: I find it nice to separate pure code from view logic, so I put code blocks first and create whatever view variables I need, then I only use for and if's in the html part:
)
HTH
Jonas
Hi William,
I love Razor, it's a lot more intuïtive and readable than the clunky <%= ... %> syntax. But then, I'm a C# developer. I understand your frustration, @<div clas="foo"> doesn't make sense to me either.
Maybe it's time for you to switch from VB to C#. I once started programming in VB too, then I switched to C# and never looked back.
When I started learning Razor, I also had difficulties with it. Then I started to understand that Razor just needs to know when you're in - my case - C# code. So when you go from html to code, just prepend your code with '@'. For example:
<p>this is html markup, <a href="@product.url">link to my product"</a></p>
In the above example, you can see that I somehow have to my clear to Razor that 'produc.url' is not html, but that it's a variable that needs to be processed, hence I prepend my variable with '@'. The beauty of Razor, is that is knows that only 'produc.url' needs to be processed. With .aspx , I would have to fully enclose the 'product.url' variable, like so: <%= product.url %>.
When you're switching from code to html, but there is no html tag to make it clear to Razor that it's html, you can prepend your text with @: For example:
<ul>
@foreach (var product in products)
{
@:this is no code
<li>@product.name</li>
@:this is no code
}
</ul>
Hi William
First I would like to say your ability to upset and annoy contributors and users of Umbraco and its' forum isnt without some level of praise! I say this because I have struggled for months with a part of Umbraco thats not even FREE and that's Courier 2. Since the day of it's release it's been a total mess and with every incremental release over TWO YEARS the product has become more and more unstable and unusable....In the end I had to get vocal too! here....http://our.umbraco.org/forum/umbraco-pro/courier/41759-LOBBY-THE-UMBRACO-TEAM!!!-COURIER-IS-LETTING-UMBRACO-DOWN!!
Anyway, here's my ten pence worth on Razor....
1. It's Microsoft's baby and therefore it's here to stay, the alternative is MVC client side ala Angular.JS! Which the UI in U7 is actually based on!
2. There were a few templating engines initially on offer when MVC first came to be and the MS team seemed to settle on Razor presumably due to it's ability retain a closeness to C#/VB syntax.
3. I totally agree going from XSLT to Razor is a massive culture shock and an uphill struggle, but you can draw many positives and negatives from both engines! That's even with considerable C# experience!
4. As a designer/developer going from Classic ASP to ASP.net WebForms seemed initially like I was losing all control of my client side code and in many respects I did, but the page lifecycle and event model was (in my view) well worth that loss. In time WebForms controls improved to the point (along with third party controls) that I didnt care anyway!
5. Remember Umbraco's implementation of Razor is an abstraction away from the other 2 parts of MVC, the model and the controller...you need not concern yourself with these unless you need to write anything akin to the old user controls for incorporating information thats not part of your Umbraco tree. If you do find yourself in that boat you may find that Razor actually makes more sense because you can then write an extremely focused view which may only incorporate a bunch of @paramaters and HTML. So in some respects Umbraco views can "potentially" become far more complicated and code heavy than even MS originally intended!
6. Stick with it and learn it...overall I believe it is easier to read through than XSLT. I've found XSLT to be quite abstract at times, due to it's recursive nature. Also if you ever delve in the Umbraco APIs you don't have to continually put on different code hats, but stick with C# over VB!!!
Martin
is working on a reply...