Skip to content

Commit

Permalink
Update README.
Browse files Browse the repository at this point in the history
  • Loading branch information
leekchan committed Jul 15, 2015
1 parent 0da6099 commit c9ba05d
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 0 deletions.
56 changes: 56 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,62 @@ Returns a plural suffix if the value is not 1. You can specify both a singular a



#### boolYesno

Returns argument strings according to the given boolean value.

**Argument:** strings for true and false

```
{{ value | boolYesno "yes!" "no!" }}
```


#### stringRjust

Right-aligns the given string in a field of a given width. This function also supports unicode strings.

```
{{ value | stringRjust 10 }}
```

**Examples**

1. If input is {{ "Go" | stringRjust 10 }}, the output will be " Go".
1. If input is {{ "안녕하세요" | stringRjust 10 }}, the output will be " 안녕하세요".



#### stringLjust

Left-aligns the given string in a field of a given width. This function also supports unicode strings.

```
{{ value | stringLjust 10 }}
```

**Examples**

1. If input is {{ "Go" | stringLjust 10 }}, the output will be "Go ".
1. If input is {{ "안녕하세요" | stringLjust 10 }}, the output will be "안녕하세요 ".



#### stringCenter

Centers the given string in a field of a given width. This function also supports unicode strings.

```
{{ value | stringCenter 10 }}
```

**Examples**

1. If input is {{ "Go" | stringCenter 10 }}, the output will be " Go ".
1. If input is {{ "안녕하세요" | stringCenter 10 }}, the output will be " 안녕하세요 ".



## Goal
The main goal is implementing all built-in template filters of Django & Jinja2.

Expand Down
44 changes: 44 additions & 0 deletions gtf.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,50 @@ var GtfFuncMap = template.FuncMap {

return bits[1]
},
"boolYesno": func(yes string, no string, value bool) string {
defer recovery()

if value {
return yes
}

return no
},
"stringRjust": func(arg int, value string) string {
defer recovery()

n := arg - len([]rune(value))

if n > 0 {
value = strings.Repeat(" ", n) + value
}

return value
},
"stringLjust": func(arg int, value string) string {
defer recovery()

n := arg - len([]rune(value))

if n > 0 {
value = value + strings.Repeat(" ", n)
}

return value
},
"stringCenter": func(arg int, value string) string {
defer recovery()

n := arg - len([]rune(value))

if n > 0 {
left := n / 2
right := n - left
value = strings.Repeat(" ", left) + value + strings.Repeat(" ", right)
}

return value
},
}

// gtf.New is a wrapper function of template.New(http://golang.org/pkg/text/template/#New).
Expand Down
24 changes: 24 additions & 0 deletions gtf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,28 @@ func TestGtfFuncMap(t *testing.T) {

ParseTest(&buffer, "{{ 2 | intPluralize \"y,ies,s\" }}")
AssertEqual(t, &buffer, "")

ParseTest(&buffer, "{{ true | boolYesno \"yes~\" \"no~\" }}")
AssertEqual(t, &buffer, "yes~")

ParseTest(&buffer, "{{ false | boolYesno \"yes~\" \"no~\" }}")
AssertEqual(t, &buffer, "no~")

ParseTest(&buffer, "{{ \"Go\" | stringRjust 10 }}")
AssertEqual(t, &buffer, " Go")

ParseTest(&buffer, "{{ \"안녕하세요\" | stringRjust 10 }}")
AssertEqual(t, &buffer, " 안녕하세요")

ParseTest(&buffer, "{{ \"Go\" | stringLjust 10 }}")
AssertEqual(t, &buffer, "Go ")

ParseTest(&buffer, "{{ \"안녕하세요\" | stringLjust 10 }}")
AssertEqual(t, &buffer, "안녕하세요 ")

ParseTest(&buffer, "{{ \"Go\" | stringCenter 10 }}")
AssertEqual(t, &buffer, " Go ")

ParseTest(&buffer, "{{ \"안녕하세요\" | stringCenter 10 }}")
AssertEqual(t, &buffer, " 안녕하세요 ")
}

0 comments on commit c9ba05d

Please sign in to comment.