From 3274892d6c054e66ffd8df9cd49b22dfd1086671 Mon Sep 17 00:00:00 2001 From: Tyler Sommer Date: Tue, 26 Apr 2016 21:52:49 -0600 Subject: [PATCH] Added some examples. --- example_fs_test.go | 44 +++++++++++++++++ example_macro_test.go | 47 +++++++++++++++++++ example_test.go | 99 +++++++++++++++++++++++++++++++++++++++ testdata/base.txt.twig | 4 +- testdata/macros.html.twig | 3 ++ testdata/main.txt.twig | 7 +++ testdata/other.txt.twig | 7 +++ testdata/parts.txt.twig | 3 ++ 8 files changed, 213 insertions(+), 1 deletion(-) create mode 100644 example_fs_test.go create mode 100644 example_macro_test.go create mode 100644 example_test.go create mode 100644 testdata/macros.html.twig create mode 100644 testdata/main.txt.twig create mode 100644 testdata/other.txt.twig create mode 100644 testdata/parts.txt.twig diff --git a/example_fs_test.go b/example_fs_test.go new file mode 100644 index 0000000..b0b1e6a --- /dev/null +++ b/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. +} diff --git a/example_macro_test.go b/example_macro_test.go new file mode 100644 index 0000000..e4df649 --- /dev/null +++ b/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) +} diff --git a/example_test.go b/example_test.go new file mode 100644 index 0000000..b8d7d85 --- /dev/null +++ b/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 +} diff --git a/testdata/base.txt.twig b/testdata/base.txt.twig index 366762e..2024781 100644 --- a/testdata/base.txt.twig +++ b/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 %} diff --git a/testdata/macros.html.twig b/testdata/macros.html.twig new file mode 100644 index 0000000..bbb9d23 --- /dev/null +++ b/testdata/macros.html.twig @@ -0,0 +1,3 @@ +{% macro list(val) %}{% for i, v in val %} +* {{ v.Key }}: {{ v.Value }} ({{ i }}) +{% endfor %}{% endmacro %} \ No newline at end of file diff --git a/testdata/main.txt.twig b/testdata/main.txt.twig new file mode 100644 index 0000000..2c62bdc --- /dev/null +++ b/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 %} \ No newline at end of file diff --git a/testdata/other.txt.twig b/testdata/other.txt.twig new file mode 100644 index 0000000..ea47cff --- /dev/null +++ b/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) }} \ No newline at end of file diff --git a/testdata/parts.txt.twig b/testdata/parts.txt.twig new file mode 100644 index 0000000..5c3be08 --- /dev/null +++ b/testdata/parts.txt.twig @@ -0,0 +1,3 @@ +{% block secondary_title %}Another section{% endblock %} + +{% block secondary_body %}Some extra information.{% endblock %} \ No newline at end of file