Skip to content

Commit

Permalink
Merge pull request #21 from spekary/code_cleanup
Browse files Browse the repository at this point in the history
Code cleanup
  • Loading branch information
spekary authored Feb 9, 2022
2 parents b4963e2 + 29ce620 commit 5706010
Show file tree
Hide file tree
Showing 16 changed files with 381 additions and 352 deletions.
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,11 @@ and you can define these fragments in include files. To use these fragments, you
use the name of the fragment as the tag. This essentially gives you the ability to create your own template
language. When you use a custom tag, you can also include parameters that will
replace placeholders in your fragment, giving you even more power to your custom tags.
- **Error Reporting**. Errors in your template files are identified by line and character
number. No need to guess where the error is.

Using other go libraries, you can have your templates compile when they are changed,
use buffer pools to increase performance, write to io.Writers, and more. Since the
use buffer pools to increase performance, and more. Since the
templates become go code, you can do what you imagine.


Expand Down Expand Up @@ -150,9 +152,7 @@ import (
)

func main() {
var b bytes.Buffer
_ = template.OutTemplate(b)
_,_ = b.WriteTo(os.Stdout)
_ = template.OutTemplate(os.Stdout)
}
```
Expand Down Expand Up @@ -336,7 +336,9 @@ Defined fragments start a block of text that can be included later in a template
be sent as is, and then processed in whatever mode the template processor is in, as if that text was simply
inserted into the template at that spot. You can include the `{{` or `{{g` tags inside of the fragment to
force the processor into the text or go modes if needed. The fragment can be defined
any time before it is included, including being defined in other include files. You can add optional parameters
any time before it is included, including being defined in other include files.
You can add optional parameters
to a fragment that will be substituted for placeholders when the fragment is used. You can have up to 9
placeholders ($1 - $9). Parameters should be separated by commas, and can be surrounded by quotes if needed
to have a parameter that has a quote in it.
Expand Down Expand Up @@ -514,7 +516,7 @@ func writeTemplate(ctx context.Context, buf *bytes.Buffer) {
{{# Define the body that will be inserted into the template }}
{{< body }}
<p>
The caller is: {{=s ctx.Value("something") }}
The caller is: {{=s ctx.Value("caller") }}
</p>
{{end body}}

Expand All @@ -533,9 +535,7 @@ type myHandler struct {}

func (h myHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx := context.WithValue(r.Context(), "caller", r.Referer())
b := new(bytes.Buffer)
writeTemplate(ctx, b) // call the got template
w.Write(b.Bytes())
writeTemplate(ctx, w) // call the got template
}


Expand Down
2 changes: 1 addition & 1 deletion doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@
//
// For complete usage documentation, see https://github.com/goradd/got
//
package main
package main
8 changes: 4 additions & 4 deletions internal/got/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func buildAst(fileName string, namedBlocks map[string]namedBlockEntry) (ret astT
l := lexFile(fileName, inFile, namedBlocks)
ret.topItem = parse(l)
if ret.topItem.typ == itemError {
err = fmt.Errorf(ret.topItem.FormatError())
err = fmt.Errorf(ret.topItem.formatError())
}
return
}
Expand Down Expand Up @@ -184,9 +184,9 @@ func (a *astWalker) outputText(val string) (err error) {
func (a *astWalker) outputRun(item tokenItem) error {
if !a.textMode {
return a.outputGo(item.val)
} else {
return a.outputText(item.val)
}

return a.outputText(item.val)
}

func (a *astWalker) outputGo(code string) (err error) {
Expand Down Expand Up @@ -244,7 +244,7 @@ func (a *astWalker) outputValue(item tokenItem) (err error) {
}

func (a *astWalker) outputGoErr(item tokenItem) (err error) {
_,err = fmt.Fprintf(a.w, "\nif err = %s; err != nil {return}\n", item.val)
_, err = fmt.Fprintf(a.w, "\nif err = %s; err != nil {return}\n", item.val)
return
}

Expand Down
2 changes: 1 addition & 1 deletion internal/got/ast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import "testing"
func Test_quoteText(t *testing.T) {
tests := []struct {
name string
val string
val string
want string
}{
{"standard text", "abc", "`abc`"},
Expand Down
Loading

0 comments on commit 5706010

Please sign in to comment.