📜 ⬆️ ⬇️

We create the first application on NancyFX. Part Five. Super Simple View Engine

In a previous article, Create the first application on NancyFX. Part Four We continue to work with the modules, we continued to study the modules. In this article, we will examine the Super Simple View Engine graphics engine coming with Nancy out of the box.

So let's create a new module:

using Nancy; namespace NancyFxApplication { public class ViewModule : NancyModule { public ViewModule() : base("/views") { Get["/"] = param => View["View.html"]; } } } 

And we will add the View.html file to the root of our project, which will look like this:

 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Super Simple View Engine Rule!</title> </head> <body> <h1>Hello! I'm super Simple View Engine!</h1> </body> </html> 

Run the application and get the following result:
')


Nancy supports the following file allocation and file naming conventions for submissions:

You can specify the name of the file without the extension, Nancy simply selects the first available view file with the name passed to View.

Now that we’ve figured out the layout of the view files, let's take a closer look at the Super Simple View Engine.
Super Simple View Engine uses syntax similar to Razor syntax. You can find a project on GitHub by clicking on the Super Simple View Engine link. SSVE does not support nested collections. When displaying complex data types, it is much more efficient to use Razor.

So, now we use the fake repository from the previous article to get data. The repository code looks like this:

 using System.Collections.Generic; namespace NancyFxApplication { public static class DishesRepository { public static List<Dish> Dishes = new List<Dish> { new Dish { Id=1, Name = "Porridge", IsSpicy = false, Ingridientses = new List<Ingridients>{new Ingridients{Id = 1, Name = "Porridge"}}}, new Dish {Id = 2, Name = "Chili", IsSpicy = true}, new Dish {Id = 3, Name = "Icecream", IsSpicy = false}, new Dish {Id = 3, Name = "Taco", IsSpicy = false}, new Dish {Id = 3, Name = "Burito", IsSpicy = true} }; public static List<Dish> GetAllDishes() { return Dishes; } public static void AddDish(Dish dish) { Dishes.Add(dish); } } } 

Modify the ViewModule module:

 using System.Collections.Generic; using Nancy; namespace NancyFxApplication { public class ViewModule : NancyModule { public ViewModule() : base("views") { Get["/"] = param => { List<Dish> dishes = DishesRepository.GetAllDishes(); return View['View.html', dishes]; }; } } } 


And in turn, we modify the view.

 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Super Simple View Engine Rule!</title> </head> <body> <h1>List of dishes:</h1> <ul> @Each <li>@Current.Name</li> @EndEach </ul> </body> </html> 


Run our application and get the following result.



The presentation engine provides the possibility of using @If operators, however, I would like to note that nested SSVE operators are not supported. It is also possible to add Partial View.

In conclusion, I would like to add once again that using SSVE only in cases when you need to display simple data, it is recommended to use other presentation engines to display complex types.

The next final article in the series will focus on testing applications for NancyFX. Waiting for feedback and comments.

Source: https://habr.com/ru/post/201490/


All Articles