Git Product home page Git Product logo

sharptal's Introduction

SharpTAL

SharpTAL is an HTML/XML template engine for .NET platform, that you can use in any application running on .NET 4.0.

The template engine compiles HTML/XML templates into .NET assemblies.

It contains implementation of the ZPT language (Zope Page Templates). ZPT is a system which can generate HTML, XML or plain text output. ZPT is formed by the TAL (Template Attribute Language), TALES (TAL Expression Syntax) and the METAL (Macro Expansion TAL).

Getting the code

Binaries are provided as a NuGet package (https://nuget.org/packages/SharpTAL).

The project is hosted in a GitHub repository

Please report any issues to the issue tracker.

Introduction

Using a set of simple language constructs, you control the document flow, element repetition and text replacement.

The basic TAL (Template Attribute Language) example:

<html>
  <body>
    <h1>Hello, ${"world"}!</h1>
    <table>
      <tr tal:repeat='row new string[] { "red", "green", "blue" }'>
        <td tal:repeat='col new string[] { "rectangle", "triangle", "circle" }'>
           ${row} ${col}
        </td>
      </tr>
    </table>
  </body>
</html>

The ${...} notation is short-hand for text insertion. The C# expression inside the braces is evaluated and the result included in the output. By default, the string is escaped before insertion. To avoid this, use the structure: prefix:

<div>${structure: ...}</div>

The macro language (known as the macro expansion language or METAL) provides a means of filling in portions of a generic template.

The macro template (saved as main.html file):

<html metal:define-macro="main">
  <head>
    <title>Example ${document.title}</title>
  </head>
  <body>
    <h1>${document.title}</h1>
    <div id="content">
      <metal:tag metal:define-slot="content" />
    </div>
  </body>
</html>

Template that imports and uses the macro, filling in the content slot:

<metal:tag metal:import="main.html" use-macro='macros["main"]'>
  <p metal:fill-slot="content">${structure: document.body}<p/>
</metal:tag>

In the example, the statement metal:import is used to import a template from the file system using a path relative to the calling template.

Sample code that shows how easy the library is to use:

var globals = new Dictionary<string, object>
{
    { "movies", new List<string> { "alien", "star wars", "star trek" } }
};

const string body = @"<!DOCTYPE html>
<html tal:define='textInfo new System.Globalization.CultureInfo(""en-US"", false).TextInfo'>
    Favorite sci-fi movies:
    <div tal:repeat='movie movies'>${textInfo.ToTitleCase(movie)}</div>
</html>";

var template = new Template(body);

var result = template.Render(globals);

Console.WriteLine(result);

Here's the console output:

<!DOCTYPE html>
<html>
   Favorite sci-fi movies:
   <div>Alien</div><div>Star Wars</div><div>Star Trek</div>
</html>

License

This software is made available under Apache Licence Version 2.0.

sharptal's People

Contributors

borisshevchenko avatar eugeneai avatar lck avatar petteriaimonen avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.