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

Using variables in partials #6

Open
hli24 opened this issue Jun 10, 2019 · 1 comment
Open

Using variables in partials #6

hli24 opened this issue Jun 10, 2019 · 1 comment

Comments

@hli24
Copy link

hli24 commented Jun 10, 2019

Hi Craig,

your package is very good, but I have got a problem when using variable in partials, it works fine when the code is in the root template, but if I try to do the same thing in a partial, the increment doesn't work and throw an exception saying the variable is not defined (or cannot find the variable something like that cannot remember the exact massage)

        {{var index=0}}
        {{index++}}
        {{$.ShiftTimes[index]}}
@craigbridges
Copy link
Owner

Hi hli24,

Sorry for taking so long to get back to you - it's been extremely busy at work!

I've had a quick look at the code to refresh my memory as I haven't touched Nettle for a while. Every time a partial is rendered it gets a new template context (this contains the model data and variables etc). I clone the parent context and then overwrite the model with the partials model (if there is one, otherwise it gets the parents model). But then I clear the variables collection - I do this to ensure variables are 'scoped' to their current context. This is likely to be why you are getting the error - although it would be good to see more of the source code causing your particular problem to get more context.

If this is being caused by what I think it is, then I will need to have a think about how to handle this problem better. Are you defining the index variable in the root template and then trying to increment it within the partial?

Thanks,
Craig

@craigbridges craigbridges changed the title using variable in partials Using variables in partials Jun 20, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants