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

Ability to set a variable within a template #167

Closed
SirCameronMcAlpine opened this issue Jul 28, 2015 · 8 comments
Closed

Ability to set a variable within a template #167

SirCameronMcAlpine opened this issue Jul 28, 2015 · 8 comments

Comments

@SirCameronMcAlpine
Copy link

Hi

I would like to write a helper that could set the value of a template variable within the template.
Something like:
{{#set "variableName"}} bla bla value for the variable{{anotherhelper "sdsds"}} {{/set}}

This is possible provided the $options argument of the helper closure is passed by reference.
Unless there is an easier way to do this that I have missed..

Thanks
C

@zordius
Copy link
Owner

zordius commented Aug 1, 2015

All lightncandy features should align with handlebars.js , I need time to look through handlebars.js to ensure how it support this or not.

@zordius
Copy link
Owner

zordius commented Aug 1, 2015

Please refer to handlebars-lang/handlebars.js#476 , so far handlebars.js do not support this.

Another way to support this is implement #set yourself and store the variable into a global or singleton, then you can get the variable back with your own helper function.

@zordius
Copy link
Owner

zordius commented Aug 2, 2015

Another way to do this is the 'private variable' feature of block helpers, #171 will deal with it.

@zordius
Copy link
Owner

zordius commented Oct 13, 2015

Please read the document README.md and search for Private variables , thanks.

@zordius zordius closed this as completed Oct 13, 2015
@laurenhamel
Copy link

laurenhamel commented May 22, 2019

While this issue has been closed for quite some time, I'd like to make a case for why it should be reopened. As of the latest implementation of handlebars.js, variable assignment can easily be achieved through helpers, both block and inline. For instance, the following works without the need for the explicit declaration of a global and/or private variable:

function set( key, value, context ) {
  context[key] = value;
}

With the above helper, I could then assign a key to the context of my choosing, like so:

{{set 'foo' 'bar' this}}
{{set 'foo' 'bar' someOtherObject}}

When using handlebars.js, I frequently leverage helpers like this to set and then unset a temporary key on the given context. In LightnCandy, however, this is unachievable due to the imposed limitations on helpers. In keeping consistent with handlebars.js, especially when FLAG_HANDLEBARSJS is enabled, I do feel that the ability to pass an array by reference to a custom helper should be supported.

@laurenhamel
Copy link

I'd also like to add that, in JavaScript, objects and arrays are always passed by reference, which is why my example above works, and I think this is the underlying theme of this thread. I feel like LightnCandy has missed the mark in this area.

@flavi1
Copy link

flavi1 commented Feb 27, 2020

Not perfect, but less difficult :
When reading php code generated, we can see that closure helper function is "var_exported" in the closure template function.
As long as the variables parameter of the closure template function is $in, this will simply work (passed by reference) :

        $helpers['set'] = function($k, $v) use(&$in) {
            $in[$k] = $v;
        };

tested with
{{set "var" "myvalue"}}

BUT FAILED with
{{set "var" "my val ue"}}

maybe helpers doesn't like spaces?.. Maybe i forget a compilation token?

@flavi1
Copy link

flavi1 commented Feb 27, 2020

However.

  • I don't know if it's hard to reproduce in handlebarsjs.
  • We should want the real variable context, not the global. (if we're between "with" tags for exemple...). We need to be able to target on it (may be another variable to pass by reference?)
  • Another "poly-langage" way can be to write a JSON in the template comments to declare globally some vars and parse it before the template engine. Comments should be a good place for "meta" informations...

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

No branches or pull requests

4 participants