Modern CMS Ninjas need Razor sharp templates

I want to comment on some of the Ninja tricks (“very smart” methods) we sometimes use during the work of implementing a website.

Let me start by saying: Ninja tricks are cool when you first see them, but people will cut them selves if they cannot see the sharp ninja tricks

Nina tricks are amazing but are also double-edged swords, I love and hate them!

From Clumsy Ninja game for IOS

What is all the ninja fuss about?

I have implemented many websites using Dynamicweb CMS and other CMS systems. Mainly on .NET based systems.

It started back in the “good old” ASP days where content were a mess of code an content. Then came ASP.NET based period where code and content were seperated.

Today the trend (in the part of web where I work) is returning to ASP sort of coding style with Microsoft Razor MVC based patterns.

For example to write a H2 containing a name you could see this:

Example ASP Razor
Write out a name <h2>< %=username%></h2> <h2>@username</h2>

This example does not prove that Razor and ASP is same-same, however it is perhaps more the pattern which is the same: The mixing of code and markup.

This example will not cause problem to most front-end developers, even if they have not coded any code. They may even be able to remember the syntax.

But what about this example, which is taken from the official Dynamicweb Forum posted by a guy from Dynamicweb, helping a guy out of a problem he cannot figure out:

(Razor templates – connect to database )

<ul>

@{

   using (var command = Dynamicweb.Database.CreateConnection().CreateCommand())
   {
      command.CommandText = "select top 10 * from EcomProducts where ProductName like '%'+@ProductName+'%'";
       command.Parameters.Add(new System.Data.SqlClient.SqlParameter()
       {
           ParameterName = "ProductName",
           SqlDbType = System.Data.SqlDbType.NVarChar,
           Value = System.Web.HttpContext.Current.Request.Params["name"] ?? ""
       });
       using (var reader = command.ExecuteReader())
       {
           while (reader.Read())
           {
               var product = new Dynamicweb.eCommerce.Products.Product(reader);
               <li>@product.Name</li>
           }
       }
   }

}

</ul>

– most fronted developers working with “conventional” Dynamicweb HTML or XSLT templates will recognize only the UL and LI tags! The rest are ninja tricks.

I Love Ninja Tricks!

There are so many nice things you can do with a Razor sharp ! I love it!

You can utilize so many nice things embedded in the classes of the Microsoft .NET framework.

You can turn your templates into complete .NET apps! Do this and that, get things from here, there and everywhere while you swing your Razor C-Sharp sword!

You have a lethal tool in your hands as you build your .CSHTML templates: “Die challenges! Die!”.

I hate Ninja Tricks!

When I return to a website where I have used Ninja Tricks some months ago, I sometimes wonder: “What th f**k have I been thinking here?” and I need to spend sometimes hours to figure out what me as a Ninja or some other Ninja have been using of tricks or hacks to get the task done.

When something breaks because of some Ninja Tricks which was clever, but not that clever.

When the quick Ninja Trick fixed the problem, but did not turn out very easy to build on at a later date.

We need Ninja Tricks…?

Well first of all, not all smart solutions are Ninja Tricks – they are simply what was needed to do the job!

Some Ninja Tricks are simply something new which at first seems like Ninja Tricks, but they were not – they were simply just a new (at first complex) pattern being introduced.

For instance jQuery will have some weird patterns which at first sight seems hard to get, but will make sense when you get up to speed.

Trying to explain my 8 years old daughter what I was trying to say made me compare with driving a car.

At first there are a lot of things you need to simply understand and remember. It is not a Ninja Trick to be able to use the brakes. At first, perhaps, it can be hard to get – like some Ninja Tricks.

Driving the car calls for a certain level of skills and understanding. That is simply the level of difficulty that you need to master if you want to drive a car!

  • Those “drive a car tricks” most people understand and learn to master.

However the relative simple tricks required to be a good driver does not change all the time!

How much can we require from web developers?

Well, you may argue that you simply should use the level of Razor Ninja Tricks which you master to avoid the dark side of Ninja Tricks!

Keep with the IF tests and the output and perhaps a small set of content manipulation (like UpperCase methods) and you will get no problems.

Also you could know your limitations and ask (or pay) someone with better skills of Razor Ninja tricks to help you.

Like you as a backend developer may not be the one designing the site.

The good old skills of a great developer

You can also try to use the know simple easy to use skills which a developer should know.

For instance I (try to) always write small comments for every fix or change I make in the code, like:

/ SHO 19/03/2014 22.25: I wrote this sht! */

Conclusion

We have been given some great tools with the addition of Razor templates in our CMS!

Think before you swing the C# Razor sword! Remember to let a colleague (backend) developer validate your code.

And Dynamicweb: Try to also document and teach the partners how you think we should use not-too-complex Razor Ninja Code!

Thank you for the Razor blade sharp options given to us! May the force be with us all!

US

One thought on “Modern CMS Ninjas need Razor sharp templates

  1. The so called Ninja Tricks should be placed in it’s own Class/Namespace, and then you can use this in your razor template by just writing “@using YourNameSpace” – No reason to have a ex. make a call to the database in your razor script. I would recommend splitting it a bit up, so that you only have the most simple in your razor script, like just outputting your template field, creating lists etc..

Leave a Reply