Giter Site home page Giter Site logo

turbx's Introduction

Turbx

npm version dependency status gitHub top language npm license

npm weekly downloads npm monthly downloads

donation link

A Fast and Easy To Use View Engine, Compiled In Go.

Note: this is the info for the Go module. Click here to find info on the NodeJS module.


Notice: the Go module v2 is ready, but the NodeJS module v2 has Not been setup yet. The html templates between v1 and v2 are slightly different. For the NodeJS module, you should continue to use v1. This is only a pre-release of v2 for Go.

Whats New

  • New rebuild of the compiler to replace my old spaghetti code (and create new spaghetti code)
  • Performance and Stability improvements.

Installation

sudo apt-get install libpcre3-dev

go get github.com/AspieSoft/turbx/v2

Setup

package main

import (
  turbx "github.com/AspieSoft/turbx/v2/compiler"
)

func Test(t *testing.T){
  defer turbx.Close()

  turbx.SetConfig(turbx.Config{
    Root: "views",
    Static: "public",
    Ext: "html",
    IncludeMD: true,
    DebugMode: true,
  })

  // note: if 'turbx.SetConfig' is never called, you will need to run 'turbx.InitDefault' in its place
  // this runs some initial code that cannot run before the config is set
  turbx.InitDefault()

  startTime := time.Now().UnixNano()

  html, path, comp, err := turbx.Compile("index", map[string]interface{}{
    "@compress": []string{"br", "gz"}, // pass the browser compression options from the client
    "@cache": true,

    "key": "MyKey",
    "name": "MyName",

    "$myConstantVar": "this var will run in the precompiler",

    "test": 1,
    "var": "MyVar",
    "list": map[string]interface{}{
      "key1": "value1",
      "key2": "value2",
      "key3": "value3",
    },
  })

  if err != nil {
    // this method will only log errors if debug mode is enabled
    turbx.LogErr(err)
    return
  }

  endTime := time.Now().UnixNano()


  // a path will be provided if we precompiled a static file
  // you can send this file directly to the user (it will automatically choose a compressed file as needed)
  // if there is no path (path == ""), we will have an html output instead (which will also be compressed as needed)
  if path != "" {
    html, err = os.ReadFile(path)
    if err != nil {
      turbx.LogErr(err)
      return
    }
  }

  if comp == 1 {
    if html, err = goutil.BROTLI.UnZip(html); err != nil {
      turbx.LogErr(err)
      return
    }
  }else if comp == 2 {
    if html, err = goutil.GZIP.UnZip(html); err != nil {
      turbx.LogErr(err)
      return
    }
  }

  fmt.Println("----------")
  fmt.Println(string(html))
  fmt.Println("----------")

  fmt.Println(float64(endTime - startTime) / float64(time.Millisecond), "ms")
}

Usage

<!-- this is a comment -->
/* this is also a comment */
// this is an inline comment


<!-- output a variable with escaped HTML -->
{{title}}

<!-- output a variable and allow HTML -->
{{{content}}}

<!-- output a variable with a fallback -->
{{title|name|'Default Title'}}


<!-- output a variable as an attribute (this method will remove the attribute if the value is undefined) -->
<a {{href="url"}}>Link</a>

<!-- this is a shortcut if the attribute name matches the variable name -->
<a {{="href"}}>Link</a>


<!-- you can safely insert object keys (if an object is undefined, this will return blank, and will Not crash) -->
{{obj.key}}

<!-- use dot notation for array indexes -->
{{arr.0}}

<!-- use the value of the "key" var as the key of "obj" -->
{{obj[key]}}
{{obj[myVar]}}


<!-- using vars as if they were constant -->
{{$normalVal|'make this constant anyway, even if not sent as a constant'}}


<!-- functions start with an _ -->
<_if var1 & var2="'b'" | var2="'c'" | !var3 | (group & group1.test1)>
  do stuff...
<_else !var1 & var2="var3" | (var3=">=3" & var3="!0")/>
  do other stuff...
<_else/>
  do final stuff...
</_if>

<_each myObj as="value" of="key">
  {{key}}: {{value}}
</_each>

<!-- 'if/else' statements and 'each' loops are a special kind of function, and do not need the '_' prefix -->
<if test>
  {{test}}
<else/>
  no test
</if>

<each myObj as="value" of="key">
  {{key}}: {{value}}
</each>


<!-- A component is imported by using a capital first letter -->
<!-- The file should also be named with a capital first letter -->
<!-- args can be passed into a component -->
<MyComponent arg1="value 1" arg2="value 2">
  Some body to add to the component
</MyComponent>

<!-- component without a body -->
<MyComponent arg1="value"/>

<!-- file: MyComponent.html -->
{{arg1}} - {{arg2}}
<h1>
  {{{body}}}

  <!-- escaped html body -->
  {{body}}
</h1>


<!-- file: layout.html -->
<html>
  <head></head>
  <body>
    <header></header>
    <main>
      <!-- Insert Body -->
      {{{body}}}
    </main>
    <footer>
  </body>
</html>

Other Functions

<!-- set a new variable to crypto random bytes -->
<_rand randBytes/>
{{randBytes}}

<!-- define a size -->
<_rand rand size="64"/>
{{rand}}

<!-- random paragraph of lorem ipsum text -->
<_lorem/>
<!-- 2 paragraphs of lorem ipsum text -->
<_lorem 2/>
<!-- 3 sentence of lorem ipsum text with 5-10 words -->
<_lorem s 3 5 10/>
<!-- 1 word of lorem ipsum text with 5-10 letters -->
<_lorem w 1 5 10/>

<!-- output json as a string -->
<_json myList/>

turbx's People

Contributors

aspiesoft avatar snyk-bot avatar

Stargazers

 avatar

Watchers

 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.