<commit_before># gtf - a useful set of Golang Template Functions
[![Build Status](https://travis-ci.org/leekchan/gtf.svg?branch=master)](https://travis-ci.org/leekchan/gtf)
[![Coverage Status](https://coveralls.io/repos/leekchan/gtf/badge.svg?branch=master&service=github)](https://coveralls.io/github/leekchan/gtf?branch=master)
[![GoDoc](https://godoc.org/github.com/leekchan/gtf?status.svg)](https://godoc.org/github.com/leekchan/gtf)

gtf is a useful set of Golang Template Functions. The goal of this project is implementing all built-in template filters of Django & Jinja2. 

## Basic usages

### Method 1 : Uses gtf.New

gtf.New is a wrapper function of [template.New](http://golang.org/pkg/text/template/#New). It automatically adds the gtf functions to the template's function map and returns [template.Template](http://golang.org/pkg/text/template/#Template).

```Go
package main

import (
	"net/http"
	"github.com/leekchan/gtf"
)

func main() {
    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		tpl, _ := gtf.New("test").Parse("{{ \"The Go Programming Language\" | stringReplace \" \" }}")
		tpl.Execute(w, "")
	})
    http.ListenAndServe(":8080", nil)
}
```

### Method 2 : Adds gtf functions to the existing template.

You can also add the gtf functions to the existing template. Just call ".Funcs(gtf.GtfFuncMap)".

```Go
package main

import (
	"net/http"
	"html/template"
	"github.com/leekchan/gtf"
)

func main() {
    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		tpl, _ := template.New("test").Funcs(gtf.GtfFuncMap).Parse("{{ \"The Go Programming Language\" | stringReplace \" \" }}")
		tpl.Execute(w, "")
	})
    http.ListenAndServe(":8080", nil)
}
```


## Safety
All gtf functions have their own recovery logics. The basic behavior of the recovery logic is silently swallowing all unexpected panics. All gtf functions would not make any panics in runtime. (**Production Ready!**)

If a panic occurs inside a gtf function, the function will silently swallow the panic and return "" (empty string). If you meet any unexpected empty output, [please make an issue](https://github.com/leekchan/gtf/issues/new)! :)



## Naming convention

1. prefix(type of input value) + function : It supports only one type.
2. no prefix + function : It supports various types and evaluates input type in runtime. Please refer to "supported value types" of each function in reference. (For example, the input value of "filesizeformat" could be int or float.)


**Examples**

1. <a href="#stringLength">stringLength</a> => The type of the input value is string, and the function will return the length of the given value.
1. <a href="#intDivisibleby">intDivisibleby</a> => The type of the input value is int, and the function will return true if the value is divisible by the argument.
1. <a href="#filesizeformat">filesizeformat</a> => It supports various types(int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64).



## Reference

### Index

* [stringReplace](stringReplace)
* <a href="#stringDefault">stringDefault</a>
* <a href="#stringLength">stringLength</a>
* <a href="#stringLower">stringLower</a>
* <a href="#stringUpper">stringUpper</a>
* <a href="#stringTruncatechars">stringTruncatechars</a>
* <a href="#stringUrlencode">stringUrlencode</a>
* <a href="#stringWordcount">stringWordcount</a>
* <a href="#intDivisibleby">intDivisibleby</a>
* <a href="#stringLengthIs">stringLengthIs</a>
* <a href="#stringTrim">stringTrim</a>
* <a href="#stringCapfirst">stringCapfirst</a>
* <a href="#intPluralize">intPluralize</a>
* <a href="#boolYesno">boolYesno</a>
* <a href="#stringRjust">stringRjust</a>
* <a href="#stringLjust">stringLjust</a>
* <a href="#stringCenter">stringCenter</a>
* <a href="#filesizeformat">filesizeformat</a>

#### stringReplace

Removes all values of arg from the given string.

```
{{ value | stringReplace " " }}<commit_msg>Update README.<commit_after># gtf - a useful set of Golang Template Functions
[![Build Status](https://travis-ci.org/leekchan/gtf.svg?branch=master)](https://travis-ci.org/leekchan/gtf)
[![Coverage Status](https://coveralls.io/repos/leekchan/gtf/badge.svg?branch=master&service=github)](https://coveralls.io/github/leekchan/gtf?branch=master)
[![GoDoc](https://godoc.org/github.com/leekchan/gtf?status.svg)](https://godoc.org/github.com/leekchan/gtf)

gtf is a useful set of Golang Template Functions. The goal of this project is implementing all built-in template filters of Django & Jinja2. 

## Basic usages

### Method 1 : Uses gtf.New

gtf.New is a wrapper function of [template.New](http://golang.org/pkg/text/template/#New). It automatically adds the gtf functions to the template's function map and returns [template.Template](http://golang.org/pkg/text/template/#Template).

```Go
package main

import (
	"net/http"
	"github.com/leekchan/gtf"
)

func main() {
    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		tpl, _ := gtf.New("test").Parse("{{ \"The Go Programming Language\" | stringReplace \" \" }}")
		tpl.Execute(w, "")
	})
    http.ListenAndServe(":8080", nil)
}
```

### Method 2 : Adds gtf functions to the existing template.

You can also add the gtf functions to the existing template. Just call ".Funcs(gtf.GtfFuncMap)".

```Go
package main

import (
	"net/http"
	"html/template"
	"github.com/leekchan/gtf"
)

func main() {
    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		tpl, _ := template.New("test").Funcs(gtf.GtfFuncMap).Parse("{{ \"The Go Programming Language\" | stringReplace \" \" }}")
		tpl.Execute(w, "")
	})
    http.ListenAndServe(":8080", nil)
}
```


## Safety
All gtf functions have their own recovery logics. The basic behavior of the recovery logic is silently swallowing all unexpected panics. All gtf functions would not make any panics in runtime. (**Production Ready!**)

If a panic occurs inside a gtf function, the function will silently swallow the panic and return "" (empty string). If you meet any unexpected empty output, [please make an issue](https://github.com/leekchan/gtf/issues/new)! :)



## Naming convention

1. prefix(type of input value) + function : It supports only one type.
2. no prefix + function : It supports various types and evaluates input type in runtime. Please refer to "supported value types" of each function in reference. (For example, the input value of "filesizeformat" could be int or float.)


**Examples**

1. <a href="#stringLength">stringLength</a> => The type of the input value is string, and the function will return the length of the given value.
1. <a href="#intDivisibleby">intDivisibleby</a> => The type of the input value is int, and the function will return true if the value is divisible by the argument.
1. <a href="#filesizeformat">filesizeformat</a> => It supports various types(int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64).



## Reference

### Index

* <a href="https://github.com/leekchan/gtf#stringreplace">stringDefault</a>
* <a href="#stringDefault">stringDefault</a>
* <a href="#stringLength">stringLength</a>
* <a href="#stringLower">stringLower</a>
* <a href="#stringUpper">stringUpper</a>
* <a href="#stringTruncatechars">stringTruncatechars</a>
* <a href="#stringUrlencode">stringUrlencode</a>
* <a href="#stringWordcount">stringWordcount</a>
* <a href="#intDivisibleby">intDivisibleby</a>
* <a href="#stringLengthIs">stringLengthIs</a>
* <a href="#stringTrim">stringTrim</a>
* <a href="#stringCapfirst">stringCapfirst</a>
* <a href="#intPluralize">intPluralize</a>
* <a href="#boolYesno">boolYesno</a>
* <a href="#stringRjust">stringRjust</a>
* <a href="#stringLjust">stringLjust</a>
* <a href="#stringCenter">stringCenter</a>
* <a href="#filesizeformat">filesizeformat</a>

#### stringReplace

Removes all values of arg from the given string.

```
{{ value | stringReplace " " }}
{'fraction_non_alphanumeric': 0.1551288529560384, 'fraction_numerical': 0.008590197069226882, 'mean_word_length': 5.071319018404908, 'pattern_counts': {'":': 4, '<': 85, '<?xml version=': 0, '>': 91, 'https://': 15, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5944334', 'n_tokens_mistral': 2603, 'n_tokens_neox': 2502, 'n_words': 728}