8000 Helper within partial doesn't have access to the partial context · Issue #15 · aymerick/raymond · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
This repository was archived by the owner on Jun 19, 2025. It is now read-only.
This repository was archived by the owner on Jun 19, 2025. It is now read-only.
Helper within partial doesn't have access to the partial context #15
Closed
@kabukky

Description

@kabukky

Hi,

thanks for this great library!

Here is the code to reproduce the issue mentioned in the title:

package main

import (
	"fmt"

	"github.com/aymerick/raymond"
)

func main() {
	//template
	templateString := `
<div class="post">
	{{> userMessage tagName="h1" }}
	<h1>Comments</h1>
	{{#each comments}}
		{{> userMessage tagName="h2" }}
	{{/each}}
</div>
`
	tpl := raymond.MustParse(templateString)

	// partial
	partialString := `
<{{tagName}}>
	By {{author.firstName}} {{author.lastName}}
</{{tagName}}>
<div class="body">
	{{content}}
	{{body}}
</div>
`
	tpl.RegisterPartial("userMessage", partialString)

	// content helper
	raymond.RegisterHelper("content", func(options *raymond.Options) string {
		fmt.Println("body: ", options.ValueStr("body"))
		return ""
	})

	// context
	ctx := map[string]interface{}{
		"author": map[string]string{
			"firstName": "Alan",
			"lastName":  "Johnson",
		},
		"body": "I Love Handlebars",
		"comments": []map[string]interface{}{
			map[string]interface{}{
				"author": map[string]string{
					"firstName": "Yehuda",
					"lastName":  "Katz",
				},
				"body": "Me too!",
			},
		},
	}
	result := tpl.MustExec(ctx)
	fmt.Println(result)
}

And here's the output:

body:
body:

<div class="post">

	<h1>
		By Alan Johnson
	</h1>
	<div class="body">

		I Love Handlebars
	</div>
	<h1>Comments</h1>

		<h2>
			By Yehuda Katz
		</h2>
		<div class="body">

			Me too!
		</div>

</div>

As you can see, options.ValueStr("body") inside the content helper is an empty string, while {{body}} in the partial renders just fine.
I would expect the content helper to have access to the context it is used under.
Is this intended behavior?

Best,
Kai

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0