Conversation
| hg.A( | ||
| hg.SPAN( | ||
| company, | ||
| style=f"position: absolute; left: {hg.If(searchbar, hg.BaseElement('50 %'), hg.BaseElement('5rem'))}", |
There was a problem hiding this comment.
This will not work as expected, because formatted strings in python are not able to render hg.BaseElement elements correctly (mainly because the string gets interpolated immediately and the __format__/__str__ function is used to convert objects to strings instead of hg.render). I am aware that a notation as used here would be much nicer. There is already an open issue to add an API for this in htmlgenerator: basxsoftwareassociation/htmlgenerator#6
So now I am even more motivated to add the feature ;)
There was a problem hiding this comment.
If you have some input on how the implementation or the usage for this feature should look like, feel free to comment on the issue in htmlgenerator.
There was a problem hiding this comment.
Okay, I added a first version for this to htmlgenerator (released already). With the current implementation the implementation would look like this:
style=hg.format("position: absolute; left: {}", hg.If(searchbar, "50%", "5rem"),
It is not perfect yet, compared to f-strings. However, it is fully compatible to the python string formatting system. The f-string notation does not really allow for lazy evalutation of its content, it is immediatley evaluated as part of the string literal.
There could be a solution with a custom function like hg.f("position: absolute; left: {hg.If(searchbar, '50%', '5rem')}") (note the missing f-string specifier) and dynamically evaluates that string when we render. But we would have the problem on how we can capture the scope where the string is defined and pass it to the place where the string is evaluated.
There was a problem hiding this comment.
Btw. hg.If works with strings directly. hg.BaseElement is only required if you want to have a list of things (without a wrapping hg.DIV or similar).
There was a problem hiding this comment.
Alright thanks! it should work now
| platform, | ||
| _class="bx--header__name", | ||
| style="font-weight: 400", # override carbon design | ||
| href=hg.F(lambda c: c["request"].META["SCRIPT_NAME"] or "/"), |
There was a problem hiding this comment.
In case you wondered about this line: The WSGI protocol allows us to "mount" an application on something else than the root of a domain. Se we can e.g. have https://website.com/app1 and https://website.com/app2 and both running with on the same code and seeing themselfes as beeing on the root. All django URL resolving will work correctly as well with this. But when we use our own constructed URLs (like here, because we just want the root-url of this django instance and we don't know the name of that URL) then we have to take care of it.
saemideluxe
left a comment
There was a problem hiding this comment.
Thanks, new search bar looks great.

No description provided.