Skip to content

Commit

Permalink
Implemented "contentFor" and "block" helpers.
Browse files Browse the repository at this point in the history
  • Loading branch information
kabukky committed Apr 29, 2015
1 parent ce21e29 commit 41282bb
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 5 deletions.
5 changes: 3 additions & 2 deletions structure/requestdata.go
Expand Up @@ -13,6 +13,7 @@ type RequestData struct {
CurrentIndexPage int
CurrentPostIndex int
CurrentTagIndex int
CurrentHelperContext int // 0 = index, 1 = post, 2 = tag, 3 = author - used by block helpers
CurrentTemplate int // 0 = index, 1 = post, 2 = tag, 3 = author - never changes during execution. Used by funcs like body_classFunc etc to output the correct class
CurrentHelperContext int // 0 = index, 1 = post, 2 = tag, 3 = author - used by block helpers
CurrentTemplate int // 0 = index, 1 = post, 2 = tag, 3 = author - never changes during execution. Used by funcs like body_classFunc etc to output the correct class
ContentForHelpers []Helper // contentFor helpers that are attached to the currently rendering helper
}
4 changes: 2 additions & 2 deletions templates/generation.go
Expand Up @@ -170,10 +170,10 @@ func compileTemplate(data []byte, name string) *structure.Helper {
data, allHelpers = findHelper(data, allHelpers)
baseHelper.Block = data
baseHelper.Children = allHelpers
// Handle extend helper
// Handle extend and contentFor helpers
for index, child := range baseHelper.Children {
if child.Name == "body" {
baseHelper.BodyHelper = &baseHelper.Children[index]
baseHelper.BodyHelper = &baseHelper.Children[index] //TODO: This handles only one body helper per hbs file. That is a potential bug source, but no theme should be using more than one per file anyway.
}
}
return &baseHelper
Expand Down
24 changes: 24 additions & 0 deletions templates/handlebars.go
Expand Up @@ -43,6 +43,30 @@ func nullFunc(helper *structure.Helper, values *structure.RequestData) []byte {
return []byte{}
}

func contentForFunc(helper *structure.Helper, values *structure.RequestData) []byte {
// If there is no array attached to the request data already, make one
if values.ContentForHelpers == nil {
values.ContentForHelpers = make([]structure.Helper, 0)
}
// Collect all contentFor helpers to use them with a block helper
values.ContentForHelpers = append(values.ContentForHelpers, *helper)
return []byte{}
}

func blockFunc(helper *structure.Helper, values *structure.RequestData) []byte {
if len(helper.Arguments) != 0 {
// Loop through the collected contentFor helpers and execute the appropriate one
for index, _ := range values.ContentForHelpers {
if len(values.ContentForHelpers[index].Arguments) != 0 {
if values.ContentForHelpers[index].Arguments[0].Name == helper.Arguments[0].Name {
return executeHelper(&values.ContentForHelpers[index], values, values.CurrentHelperContext)
}
}
}
}
return []byte{}
}

func paginationDotTotalFunc(helper *structure.Helper, values *structure.RequestData) []byte {
if values.CurrentTemplate == 0 { // index
return []byte(strconv.FormatInt(values.Blog.PostCount, 10))
Expand Down
2 changes: 2 additions & 0 deletions templates/helperfunctions.go
Expand Up @@ -27,6 +27,8 @@ var helperFuctions = map[string]func(*structure.Helper, *structure.RequestData)
"plural": pluralFunc,
"date": dateFunc,
"image": imageFunc,
"contentFor": contentForFunc,
"block": blockFunc,

// @blog functions
"@blog.title": atBlogDotTitleFunc,
Expand Down
3 changes: 2 additions & 1 deletion watcher/watcher.go
@@ -1,6 +1,7 @@
package watcher

import (
"github.com/kabukky/journey/helpers"
"gopkg.in/fsnotify.v1"
"log"
"os"
Expand Down Expand Up @@ -58,7 +59,7 @@ func createWatcher(extensionsFunctions map[string]func() error) (*fsnotify.Watch
case event := <-watcher.Events:
if event.Op&fsnotify.Write == fsnotify.Write {
for key, value := range extensionsFunctions {
if filepath.Ext(event.Name) == key {
if !helpers.IsDirectory(event.Name) && filepath.Ext(event.Name) == key {
// Call the function associated with this file extension
err := value()
if err != nil {
Expand Down

0 comments on commit 41282bb

Please sign in to comment.