-
Notifications
You must be signed in to change notification settings - Fork 37
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
Pass context variables down to components #44
Comments
This is a deliberate design decision (the rationale of which I should add to the docs at some point). I like to think of components as functions. You "call" a component, pass in necessary variables, and it returns some HTML. Just like normal functions, it's not great practice to rely on global variables inside the component code. Let's take your example above. If slippers passed all current context variables to the component, the code would look something like this:
But this assumes that This is the worst case scenario of course. The level of coupling depends on how you structure your code. In any case, the option of using |
Thank you for the response, I think your arguments really make sense. But in our working context, where we use slippers extensively (everything is componerised), it feels annoying and redundant. Also i find that a view that renders an HTML page with multiple component, is like a Class in python, and the components are like the functions in that class. The init.py is sets the context for all the functions in that class. Imagine having to reset/fetch the context for each of the functions. This is what it feels like when using components right now. Our templates are indeed hard coupled to our view logic, but we use DetailView and ListView a lot, and there by default we have access to the context variables: Like you said there is indeed Would there be a way to add a settings/flag somewhere to add this functionality for people who want it? |
Problem - How to define context variable that should be available only to the component?While I love the deliberate decision to not pass context variables, I think it would be great to have an escape hatch to be able to access them. Consider the following: I have an Before using Slippers, {
"bars_3": ["M4.26 10.147a60.436 ....", "M4.26 10.147a60.436 ...."],
...
} Since the icon components has no way to access the "global" 1. Pass the
|
@mixxorz Change of plans. As I was just implementing the feature I suggested above in my fork, I noticed that the library already allows to pass in additional context via the front matter. The good news is, that this solves my issue. The bad news is that it would be unwise to use it when it's not documented. Hence, I wrote a documentation to it: See MR #58. Please review. Thanks! |
Currently context variables are not passed down to the componenets, which is really a pain when using components extensively.
I have to repeat the context variables multiple times like this:
{% header_title_component count=object_list.count tab_name=tab_name title=title %}
It's redundant and error prone, and also a bit against the whole concept of using components.
Components should allow us to write less code, by having to repeat these variables i feel like we are going against that concept.
The text was updated successfully, but these errors were encountered: