Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[question] Issue sith set_local_var for creating a filter in 3.0 #318

Closed
mardiros opened this issue Feb 29, 2020 · 3 comments
Closed

[question] Issue sith set_local_var for creating a filter in 3.0 #318

mardiros opened this issue Feb 29, 2020 · 3 comments
Labels

Comments

@mardiros
Copy link

I used the (RenderContact.set_local_var)[https://docs.rs/handlebars/2.0.0/handlebars/struct.RenderContext.html#method.set_local_var] which has been drop in the 3.x version
in favor of BlockContext.

BlockContext is really a great things to scope variable properly, I have no doubt with that,
but I have created and use a lot a set filter using the register_helper.

https://github.com/mardiros/rustaman/blob/master/src/helpers/handlebars.rs#L8

My question is:

Is theres a way it did not see to keep my set helper with handlebars 3 ?

Here is my use cases:

I am maintaining a REST Client such as POSTMAN, which is not a clickodrome.
It is used to create local variables on top of every templates,
which is a very usefull feature for my project.

Actually
I use handlebars to render HTTP requests, some variables comes from the context,
but local variables are updated in the template.

Real word example:

Context

%YAML 1.2
---
token: 1a29xxxxxxxxxxxxxxxxxxxxxfb
api_graphql: https://api.github.com/graphql

Template:

# List repositories for the given username
{{ set username = "mardiros" }}

POST {{ api_graphql }}
Authorization: bearer {{ token }}
User-Agent: Rustaman
Content-Type: application/json

{
 "query": "query { repositoryOwner(login:\"{{ username }}\") { repositories(first: 100, isFork: false, privacy: PUBLIC, orderBy: { field: CREATED_AT, direction: DESC }) { edges { node { name nameWithOwner isFork }}}}}"
}
@mardiros mardiros changed the title Issue sith set_local_var for creating a filter in 3.0 [question] Issue sith set_local_var for creating a filter in 3.0 Feb 29, 2020
@sunng87
Copy link
Owner

sunng87 commented Mar 1, 2020

Actually local var in handlebars is designed to carry some contextual data, like index in an iterator. To access local var, you will need to prefix it with @. There was an implementation issue prior to 3 that allows you to access it without @. This has been fixed in 3.0.

So you should be able to fix it by changing {{ username }} to {{ @username }}. But note that local var is only available in current scope. Helpers like each and with will create their own scopes.

But I think handlebars decorator might fit your scenario better. In handlebars-rust, decorator allows to mutate the Context within the remaining render process.

I will add an example for your case.

@sunng87
Copy link
Owner

sunng87 commented Mar 1, 2020

The example: 1f78c8e

Let me know if you have any question with it.

@mardiros
Copy link
Author

mardiros commented Mar 1, 2020

Thank you for your quick feedback.

It is actually what I was looking for. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants