Giter VIP home page Giter VIP logo

gorazor's Introduction

GoRazor

GoRazor is the Go port of the razor view engine originated from asp.net in 2011. In summay, GoRazor's:

  • Consice syntax, no delineators like <?, <%, or {{.
  • Able to mix go code in view template
    • Insert code block to import & call arbitrary go modules & functions
  • Code generation approach
    • No reflection overhead
    • Go compiler validation for free
  • Strong type view model
  • Embedding templates support
  • Template layout

Usage

Install:

go get github.com/sipin/gorazor

Usage:

./gorazor template_folder output_folder or ./gorazor template_file output_file

This is essentially a Go implementation from razor's port in javascript: vash, with Go code generation. The js directory contains the original Js version.

Syntax

Variable

  • @variable to insert string variable into html template
    • variable could be wrapped by arbitary go functions
    • variable inserted will be automatcially esacped
<div>Hello @user.Name</div>
<div>Hello @strings.ToUpper(req.CurrentUser.Name)</div>

Use raw to skip escaping:

<div>@raw(user.Name)</div>

Only use raw when you are 100% sure what you are doing, please always be aware of XSS attack.

Flow Control

@if .... {
	....
}

@if .... {
	....
} else {
	....
}

@for .... {

}

@{
	switch .... {
	case ....:
	      <p>...</p>
	case 2:
	      <p>...</p>
	default:
	      <p>...</p>
	}
}

Please use example for reference.

Code block

It's possbile to insert arbitary go code block in the template, like create new variable.

@{
	username := u.Name
	if u.Email != "" {
		username += "(" + u.Email + ")"
	}
}
<div class="welcome">
<h4>Hello @username</h4>
</div>

It's recommendation to keep clean separation of code & view. Please consider move logic into your code before creating a code block in template.

Decleration

The first code block in template is strictly for decleration:

  • imports
  • model type
  • layout (Not supported yet, but soon)

like:

@{
	import  (
		kpm "kp/models"
	)
	var totalMessage int
	var u *kpm.User
}
....

first code block must be at the begining of the template, i.e. before any html.

Any other codes inside the first code block will be ignored.

import must be wrapped in (), import "package_name" is not yet supported.

The variables declared in first code block will be the models of the template, i.e. the parameters of generated function.

If your template doesn't need any model input, then just leave it blank.

Helper / Include other template

As gorazor compiles tempaltes to go function, embedding another template is just calling the generated function, like any other go function.

However, if the template are designed to be embeded, it must be under helper namespace, i.e. put them in helper sub-folder.

So, using a helper template is similar to:

@if msg != "" {
	<div>@helper.ShowMsg(msg)</div>
}

GoRazor won't HTML escape the output of helper.XXX.

Please use example for reference.

Layout

TBA

Conventions

  • Template folder name will be used as package name in generated code
  • Template file name must has the extension name .gohtml
  • Template strip of .gohtml extension name will be used as the function name in generated code, with fisrt letter Capitalized.
    • So that the function will be accessible to other modules. (I hate GO about this.)
  • Helper templates must has the package name helper

Example

Here is a simple example of gorazor templates and the corresponding generated codes.

FAQ

TBA

Todo

  • Refactor
    • Avoid regexp in lexer for performance?
  • Add tools, like monitor template changes and auto re-generate.
  • Test suite, Performance benchmark.
  • Generate more function overloads, like accept additional buffer parameter for write.
  • Support direct usage of int/date variables in tempate?
    • i.e. use @user.Level directly, instead of @gorazor.Itoa(user.Level)

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.