Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
39529e2
commit 3274892
Showing
8 changed files
with
213 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{% macro list(val) %}{% for i, v in val %} | ||
* {{ v.Key }}: {{ v.Value }} ({{ i }}) | ||
{% endfor %}{% endmacro %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{% extends 'base.txt.twig' %} | ||
|
||
{% use 'parts.txt.twig' %} | ||
|
||
{% block title %}Hello{% endblock %} | ||
|
||
{% block conclusion %}Still nobody knows.{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{% block secondary_title %}Another section{% endblock %} | ||
|
||
{% block secondary_body %}Some extra information.{% endblock %} |