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

Constants #42

Closed
digitalcrab opened this issue Sep 23, 2014 · 3 comments
Closed

Constants #42

digitalcrab opened this issue Sep 23, 2014 · 3 comments

Comments

@digitalcrab
Copy link
Contributor

Hello
How do you think, do pongo2 needs to have constants?
For me it looks like in templates

{# or expressions can be not allowed #}
{% const NAME = "any expression" %}

and in go code

const NAME = ""
context := pongo2.Context{
    "NAME": pongo2.AsConst(NAME), // or any expression
}

And of course nothing can't change it.

@flosch
Copy link
Owner

flosch commented Sep 23, 2014

I can't see why this should be useful. Are there any real-world scenarios which justify the overhead caused by such "constants"? In fact, context variables are already constant and nothing could change their value (in your original context). Only the names can be overwritten by something else (by using the internal Private map from the ExecutionContext); the values in the Public context remain unchanged.

@digitalcrab
Copy link
Contributor Author

Lets say that we have the next context

context := pongo2.Context{
    "STATUS_ENABLED": 1,
    "STATUS_DISABLED": 2,
}

and then some where in templates i wan to compare some thing like user.status == STATUS_ENABLED. Ok every thing is ok.... but, if someone (we have more than 150 people in company) decide to have his own variable {% set STATUS_ENABLED = 2 %}.

Now we have a problem!

Everything in pongo2.Context are variables, not constants

@flosch
Copy link
Owner

flosch commented Sep 24, 2014

I still don't see a reason to introduce a name protection (which is what you're proposing). It rather looks like an organizational problem than a technical one. I would suggest introducing some variable prefixes in your case so there will be no name clutter in templates and accidental name overrides. Such a runtime name protection would cause some relevant runtime overhead (because on any variable access there is at least one more check to a map which means hashing, looking up, etc.); in addition to that, this feature would only be used very rarely by users. I don't think it's worth introducing it.

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