Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for HTML templating in Xamarin
Alexandre Nédélec
Alexandre Nédélec

Posted on • Originally published attechwatching.dev on

     

HTML templating in Xamarin

There are often situations where you need to do some HTML templating and having a powerful HTML templating engine like Razor can be really helpful. What is nice is that you don't need to be in an ASP.NET context to use Razor templates, in fact you can even use them in a Xamarin Application.

Razor templates

There is already a complete article about Razor HTML templates in Xamarin in theMicrosoft documentation so if you want an in-depth explanation I suggest you to read it. In this article, it is explained how to add a Razor template file (.cshtml file) to a Xamarin project by using theText Templating section of theNew file dialog. However if you try to add a new item in a Xamarin project you won't see aText Templating section.

That's because at the time of writing of this article, it only exists in Visual Studio for Mac (probably because it comes from Xamarin Studio). So how to add a Razor template file in Visual Studio (Windows) ? There is a little tip to do that (I found it in an old post onStackOverflow): you have to manually add a.cshtml file to your project and set the custom tool toRazorTemplatePreprocessor in the properties of the file (this will generate the code-behind file).

Then you can generate an html string from your Razor template and your data in your Xamarin project.

Code to generate the html string:

var people = new Character[]{    new Character() { FirstName = "Ellana", LastName = "Caldin", Job = "Marchombre"},    new Character() { FirstName = "Edwin", LastName = "Til'Illan", Job = "General"}};var template = new RazorTemplate() { Model = people};var page = template.GenerateString();
Enter fullscreen modeExit fullscreen mode

Razor template:

@model IEnumerable<PdfGeneration.Models.Character><html><body>    <ul>    @foreach (var person in @Model)    {        <li>@person.FirstName @person.LastName - @person.Job</li>    }    </ul></body></html>
Enter fullscreen modeExit fullscreen mode

This code produces the following html:

<html><body>    <ul>        <li>Ellana Caldin - Marchombre</li>        <li>Edwin Til&#39;Illan - General</li>    </ul></body></html>
Enter fullscreen modeExit fullscreen mode

Handlebars.Net, an alternative to Razor templates

You can also do HTML templating by usingHandlebars.Net which is a .NET Handlebars engine. It allows to build semantic templates in a .NET application. It uses the same syntax thanhandlebars.js for the templates and try to mimic the JS API. Nothing better than a piece of code to illustrate that:

This produces the following html:

<ul>    <li>Ellana Caldin - Marchombre</li>    <li>Edwin Til'Illan - General</li></ul>
Enter fullscreen modeExit fullscreen mode

The code aboveTestingHandlebars.csx is a C# script so if you havedotnet-script installed you can run it just by copy/pasting the following command in your terminal:dotnet script http://tinyurl.com/y5vvv2nm.

Using Handlebars.Net is pretty simple and quite powerful; and it runs fine on Xamarin too 👌.


So now you have 2 solutions to do HTML templating in Xamarin, choose the one you like the best.

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

I am a software developer living in Bordeaux. I am a .NET and Azure enthusiast, primarly coding in C# but I like to discover other languages and technologies too.
  • Location
    Bordeaux
  • Work
    .NET & Azure developer
  • Joined

Trending onDEV CommunityHot

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp