Git Product home page Git Product logo

sfx.templates's Introduction

Lightweight template engine for C#

  • display model values
  • loop through any IEnumerable
  • if/else conditions on booleans or functions
  • limitless nesting.
  • custom functions for displaying values or conditions.
  • includes.
  • extend base templates.

How to use it:

Template.Parse("Hello {{.}}!").Render("Joe"); 

// returns -> Hello Joe!

Samples:

Model can be any object. You access properties with a dot:

{{ .name }}

You can chain properties. If any is null it simply returns null:

{{ .Customer.Address.PostalCode }}

If a property implenents IDictionary you can access it by key. Any of this syntax are valid:

{{ .Request.Cookies.sfxSession.Value }}
{{ .Request.Cookies["sfxSession"].Value }}
{{ .Request.Cookies['sfxSession'].Value }}

Loops

{{ foreach(item in .items) }} 
	{{.item}}
{{end}}

You can also read the loop index:

{{ foreach(item, i in .items) }} 
	{{.item}}{{.i}}
{{end}}

Nesting:

{{ foreach(item in .items) }} 
	{{ foreach(child in .item) }} 
		{{.child}}
	{{end}}
{{end}}

Conditions

Conditions can accept boolean values or functions:

{{ if .User.IsAdmin }} 
   you are admin
{{ else }}
   you are not admin
{{ end }}

{{ if isNotNull(.Name) }} 
   Name: {{ .Name }}
{{ end }}

Functions

You can define custom functions and call them to display values:

template.AddFunction("pow", (int a) => a * a);

And in the template:

{{ pow(.) }} 

You can also use them in condtions:

template.AddFunction("isvalid", (object a) => a != null);

And in the template:

{{ if isvalid(.value) }} 

Includes

{{ include header.html }}
	 <h1>Hello</h1>
{{ include  footer.html }}

Nested includes or includes inside extended templates are possible but please avoid them :)

Extend base templates

Base template:

<html>
    <head>
    {{ block header }}{{end}}
    </head>
  <body>   
     {{ block body }}{{end}}
  </body>
</html>

Extend it:

{{ extends base.html }}

{{ block header }}
    <link href="/css/foo.css" rel="stylesheet" type="text/css" />
{{ end }}

{{ block body }}
      <div>
            <h1>Hello</h1>
      </div>
{{ end }}

Renders:

<html>
    <head>
    	<link href="/css/foo.css" rel="stylesheet" type="text/css" />
    </head>
  <body>   
      <div>
            <h1>Hello</h1>
      </div>
  </body>
</html>

sfx.templates's People

Contributors

scorredoira avatar

Stargazers

 avatar  avatar  avatar  avatar Remo Jongeneelen avatar jiejie-dev avatar Daniil Monin avatar Michael Corbett avatar

Watchers

James Cloos avatar  avatar Remo Jongeneelen avatar  avatar

sfx.templates's Issues

License

@scorredoira
I'm fully aware that you are not updating this Templating solution anymore, and probably never will, but it did suit a specific use case for me using .NET Core, and was wondering if you could specify (and add) the license you have in mind for this solution?

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.