Skip to content

Commit

Permalink
Renames data to ctx and cleanups code doc
Browse files Browse the repository at this point in the history
  • Loading branch information
aymerick committed Jun 3, 2015
1 parent 5034cdf commit e37a955
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 74 deletions.
34 changes: 17 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ func main() {
</div>
`

data := map[string]string{
ctx := map[string]string{
"title": "My New Post",
"body": "This is my first post!",
}

result, err := raymond.Render(tpl, data)
result, err := raymond.Render(tpl, ctx)
if err != nil {
panic("Please fill a bug :)")
}
Expand Down Expand Up @@ -77,7 +77,7 @@ func main() {
</div>
`

dataList := []map[string]string{
ctxList := []map[string]string{
{
"title": "My New Post",
"body": "This is my first post!",
Expand All @@ -94,9 +94,9 @@ func main() {
panic(err)
}

for _, data := range dataList {
for _, ctx := range ctxList {
// render template
result, err := tpl.Exec(data)
result, err := tpl.Exec(ctx)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -131,7 +131,7 @@ You can use `MustParse()` and `MustExec()` functions if you don't want to deal w
tpl := raymond.MustParse(source)

// render template
result := tpl.MustExec(data)
result := tpl.MustExec(ctx)
```


Expand All @@ -148,13 +148,13 @@ By default, the result of a mustache expression is HTML escaped. Use the triple
</div>
`

data := map[string]string{
ctx := map[string]string{
"title": "All about <p> Tags",
"body": "<p>This is a post about &lt;p&gt; tags</p>",
}

tpl := raymond.MustParse(source)
result := tpl.MustExec(data)
result := tpl.MustExec(ctx)

fmt.Print(result)
```
Expand Down Expand Up @@ -182,12 +182,12 @@ When returning HTML from a helper, you should return a `SafeString` if you don't
return raymond.SafeString("<a href='" + url + "'>" + text + "</a>")
})

data := map[string]string{
ctx := map[string]string{
"text": "This is a <em>cool</em> website",
"url": "http://www.aymerick.com/",
}

result := tpl.MustExec(data)
result := tpl.MustExec(ctx)
fmt.Print(result)
```

Expand All @@ -208,7 +208,7 @@ Output:

@todo doc

### Private data
### Private Data

@todo doc

Expand Down Expand Up @@ -290,7 +290,7 @@ For example, that template randomly evaluates the `foo` or `baz` partial:
"baz": "<span>bat</span>",
})

data := map[string]interface{}{
ctx := map[string]interface{}{
"whichPartial": func() string {
rand.Seed(time.Now().UTC().UnixNano())

Expand All @@ -299,7 +299,7 @@ For example, that template randomly evaluates the `foo` or `baz` partial:
},
}

result := tpl.MustExec(data)
result := tpl.MustExec(ctx)
fmt.Print(result)
```

Expand All @@ -313,14 +313,14 @@ For example:
tpl := raymond.MustParse("User: {{> userDetails user }}")
tpl.RegisterPartial("userDetails", "{{firstname}} {{lastname}}")

data := map[string]interface{}{
ctx := map[string]interface{}{
"user": map[string]string{
"firstname": "Jean",
"lastname": "Valjean",
},
}

result := tpl.MustExec(data)
result := tpl.MustExec(ctx)
fmt.Print(result)
```

Expand All @@ -340,11 +340,11 @@ For example:
tpl := raymond.MustParse("{{> myPartial name=hero }}")
tpl.RegisterPartial("myPartial", "his name is: {{name}}")

data := map[string]interface{}{
ctx := map[string]interface{}{
"hero": "Goldorak",
}

result := tpl.MustExec(data)
result := tpl.MustExec(ctx)
fmt.Print(result)
```

Expand Down
20 changes: 11 additions & 9 deletions data_frame.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@ package raymond

import "reflect"

// Cf. private variables at: http://handlebarsjs.com/block_helpers.html

// A private data frame
// DataFrame represents a private data frame
//
// Cf. private variables documentation at: http://handlebarsjs.com/block_helpers.html
type DataFrame struct {
parent *DataFrame
data map[string]interface{}
}

// Instanciate a new private data frame
// NewDataFrame instanciates a new private data frame
func NewDataFrame() *DataFrame {
return &DataFrame{
data: make(map[string]interface{}),
}
}

// Returns a new private data frame, with parent set to self
// Copy instanciates a new private data frame, with parent set to self
func (p *DataFrame) Copy() *DataFrame {
result := NewDataFrame()

Expand All @@ -30,6 +30,7 @@ func (p *DataFrame) Copy() *DataFrame {
return result
}

// NewIterDataFrame instanciates a new private data frame, with parent set to self an with iterable data set (@index, @key, @first, @last)
func (p *DataFrame) NewIterDataFrame(length int, i int, key interface{}) *DataFrame {
result := p.Copy()

Expand All @@ -41,17 +42,18 @@ func (p *DataFrame) NewIterDataFrame(length int, i int, key interface{}) *DataFr
return result
}

// Set a data value
// Set sets a data value
func (p *DataFrame) Set(key string, val interface{}) {
p.data[key] = val
}

// Get a data value
// Get gets a data value
func (p *DataFrame) Get(key string) interface{} {
return p.Find([]string{key})
}

// Get a deep data value
// Find gets a deep data value
//
// @todo This is NOT consistent with the way we resolve data in template (cf. `evalDataPathExpression()`) ! FIX THAT !
func (p *DataFrame) Find(parts []string) interface{} {
data := p.data
Expand Down Expand Up @@ -81,7 +83,7 @@ func (p *DataFrame) Find(parts []string) interface{} {
return nil
}

// converts any `map` to `map[string]interface{}`
// mapStringInterface converts any `map` to `map[string]interface{}`
func mapStringInterface(value reflect.Value) map[string]interface{} {
result := make(map[string]interface{})

Expand Down
3 changes: 1 addition & 2 deletions escape.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ func escape(w writer, s string) error {
return err
}

// EscapeString escapes special characters like "<" to become "&lt;". It
// escapes only five such characters: <, >, &, ' and ".
// EscapeString escapes special HTML characters like "<" to become "&lt;"
func EscapeString(s string) string {
if strings.IndexAny(s, escapedChars) == -1 {
return s
Expand Down
Loading

0 comments on commit e37a955

Please sign in to comment.