Skip to content

Commit

Permalink
Added some examples.
Browse files Browse the repository at this point in the history
  • Loading branch information
tyler-sommer committed Apr 27, 2016
1 parent 39529e2 commit 3274892
Show file tree
Hide file tree
Showing 8 changed files with 213 additions and 1 deletion.
44 changes: 44 additions & 0 deletions example_fs_test.go
@@ -0,0 +1,44 @@
package stick_test

import (
"os"

"fmt"

"path/filepath"

"github.com/tyler-sommer/stick"
)

// ExampleEnv_Execute_fs shows an example of using the provided
// FilesystemLoader.
//
// This example makes use of templates in the testdata folder. In
// particular, this example shows vertical (via extends) and horizontal
// reuse (via use).
func ExampleEnv_Execute_fs() {
d, _ := os.Getwd()
env := stick.NewEnv(stick.NewFilesystemLoader(filepath.Join(d, "testdata")))

params := map[string]stick.Value{"name": "World"}
err := env.Execute("main.txt.twig", os.Stdout, params)
if err != nil {
fmt.Println(err)
}
// Output:
// This is a document.
//
// Hello
//
// An introduction to the topic.
//
// The body of this topic.
//
// Another section
//
// Some extra information.
//
// Still nobody knows.
//
// Some kind of footer.
}
47 changes: 47 additions & 0 deletions example_macro_test.go
@@ -0,0 +1,47 @@
package stick_test

import (
"os"

"fmt"

"path/filepath"

"github.com/tyler-sommer/stick"
)

// ExampleEnv_Execute_macro shows an example of macro definition and
// usage.
func ExampleEnv_Execute_macro() {
d, _ := os.Getwd()
env := stick.NewEnv(stick.NewFilesystemLoader(filepath.Join(d, "testdata")))

params := map[string]stick.Value{
"title_first": "Hello",
"value_first": []struct{ Key, Value string }{
{"item1", "something about item1"},
{"item2", "something about item2"},
},
"title_second": "Responses",
"value_second": []struct{ Key, Value string }{
{"please", "no, thank you"},
{"why not", "cause"},
},
}
err := env.Execute("other.txt.twig", os.Stdout, params)
if err != nil {
fmt.Println(err)
}
// Output:
// Hello
//
// * item1: something about item1 (0)
//
// * item2: something about item2 (1)
//
// Responses
//
// * please: no, thank you (0)
//
// * why not: cause (1)
}
99 changes: 99 additions & 0 deletions example_test.go
@@ -0,0 +1,99 @@
package stick_test

import (
"os"

"fmt"

"github.com/tyler-sommer/stick"
)

// ExampleEnv_Execute shows a simple example of executing a template.
func ExampleEnv_Execute() {
env := stick.NewEnv(nil)

params := map[string]stick.Value{"name": "World"}
env.Execute(`Hello, {{ name }}!`, os.Stdout, params)
// Output: Hello, World!
}

type exampleType struct{}

func (e exampleType) Boolean() bool {
return true
}

func (e exampleType) Number() float64 {
return 3.14
}

func (e exampleType) String() string {
return "some kinda string"
}

// ExampleCoerceBool demonstrates how a type can be coerced to a boolean.
// The struct in this example has the Boolean method implemented.
//
// func (e exampleType) Boolean() bool {
// return true
// }
func ExampleCoerceBool() {
v := exampleType{}
fmt.Printf("%t", stick.CoerceBool(v))
// Output: true
}

// ExampleCoerceBool2 demonstrates how various values are coerced to boolean.
func ExampleCoerceBool2() {
v0 := ""
v1 := "some string"
v2 := 0
v3 := 3.14
fmt.Printf("%t %t %t %t", stick.CoerceBool(v0), stick.CoerceBool(v1), stick.CoerceBool(v2), stick.CoerceBool(v3))
// Output: false true false true
}

// ExampleCoerceNumber demonstrates how a type can be coerced to a number.
// The struct in this example has the Number method implemented.
//
// func (e exampleType) Number() float64 {
// return 3.14
// }
func ExampleCoerceNumber() {
v := exampleType{}
fmt.Printf("%.2f", stick.CoerceNumber(v))
// Output: 3.14
}

// ExampleCoerceNumber2 demonstrates how various values are coerced to number.
func ExampleCoerceNumber2() {
v0 := true
v1 := ""
v2 := "54"
v3 := "1.33"
fmt.Printf("%.f %.f %.f %.2f", stick.CoerceNumber(v0), stick.CoerceNumber(v1), stick.CoerceNumber(v2), stick.CoerceNumber(v3))
// Output: 1 0 54 1.33
}

// ExampleCoerceString demonstrates how a type can be coerced to a string.
// The struct in this example has the String method implemented.
//
// func (e exampleType) String() string {
// return "some kinda string"
// }
func ExampleCoerceString() {
v := exampleType{}
fmt.Printf("%s", v)
// Output: some kinda string
}

// ExampleCoerceString2 demonstrates how various values are coerced to string.
func ExampleCoerceString2() {
v0 := true
v1 := false // Coerces into ""
v2 := 54
v3 := 1.33
v4 := 0
fmt.Printf("%s '%s' %s %s %s", stick.CoerceString(v0), stick.CoerceString(v1), stick.CoerceString(v2), stick.CoerceString(v3), stick.CoerceString(v4))
// Output: 1 '' 54 1.33 0
}
4 changes: 3 additions & 1 deletion testdata/base.txt.twig
@@ -1,4 +1,6 @@
# {% block title %}A title{% endblock %}
This is a document.

{% block title %}A title{% endblock %}

{% block intro %}An introduction to the topic.{% endblock %}

Expand Down
3 changes: 3 additions & 0 deletions testdata/macros.html.twig
@@ -0,0 +1,3 @@
{% macro list(val) %}{% for i, v in val %}
* {{ v.Key }}: {{ v.Value }} ({{ i }})
{% endfor %}{% endmacro %}
7 changes: 7 additions & 0 deletions testdata/main.txt.twig
@@ -0,0 +1,7 @@
{% extends 'base.txt.twig' %}

{% use 'parts.txt.twig' %}

{% block title %}Hello{% endblock %}

{% block conclusion %}Still nobody knows.{% endblock %}
7 changes: 7 additions & 0 deletions testdata/other.txt.twig
@@ -0,0 +1,7 @@
{% import 'macros.html.twig' as ui %}
{% from 'macros.html.twig' import list %}

{{ title_first }}
{{ ui.list(value_first) }}
{{ title_second }}
{{ list(value_second) }}
3 changes: 3 additions & 0 deletions testdata/parts.txt.twig
@@ -0,0 +1,3 @@
{% block secondary_title %}Another section{% endblock %}

{% block secondary_body %}Some extra information.{% endblock %}

0 comments on commit 3274892

Please sign in to comment.