-
-
Notifications
You must be signed in to change notification settings - Fork 274
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
css: Cannot pass easily an url to background-image
property in a css expression
#261
Comments
background-image
property in a css functionbackground-image
property in a css function
background-image
property in a css functionbackground-image
property in a css expression
Thanks for raising this. I think that what we need here is specific behaviour for the As per https://github.com/google/safehtml/blob/be23134998433fcf0135dda53593fc8f8bf4df7c/style.go#L170 I think that the generator could be updated so that if the CSS property is Thoughts on that? |
background-image
property in a css expressionbackground-image
property in a css expression
What is the status on this? Currently I cannot even get the code to compile with the example otaxhu said was working..... |
The same problem. Even after upgrading to Hope @a-h check this 👍 |
@koddr what I did is just use a regular IMG tag as a bg image with the object fit property. See if that works for you. |
@Oudwins I didn't really understand what you mean, give me an example, please. My tricky solution (I use Tailwind CSS classes): css bgImage(u string) {
background-image: { u };
}
templ Page(pictureUrl string) {
<div class={ "h-48 bg-cover bg-center", bgImage(fmt.Sprintf("url(%s)", pictureUrl)) }>
// ...
</div>
} Very cumbersome and absolutely terrible, yes. On the other hand, we could implement this through the templ Page(pictureUrl string) {
<div
class="h-48 bg-cover bg-center"
style={ fmt.Sprintf("background-image: url(%s)", pictureUrl) }
></div>
} Error in VS Code:
Error after (✗) Error generating code [ file=/Users/user/project/page.templ error=/Users/user/project/page.templ parsing error: <div>: invalid style attribute: style attributes cannot be a templ expression: line 28, col 2 ]
(✗) Event handler failed [ error=failed to generate code for "/Users/user/project/page.templ": /Users/user/project/page.templ parsing error: <div>: invalid style attribute: style attributes cannot be a templ expression: line 28, col 2 ]
(✗) Error [ error=failed to generate code for "/Users/user/project/page.templ": /Users/user/project/page.templ parsing error: <div>: invalid style attribute: style attributes cannot be a templ expression: line 28, col 2 ]
(✗) Command failed: generation completed with 1 errors Therefore, I was really waiting for @a-h to release |
Please refer to this example. I think the docs need to be updated, would take a PR for that.
The output is: <title>
Background image
</title>
<div>
<style type="text/css">
.profileCSS_a59d{background-image:url(image1.png);}
</style>
<div class="profileCSS_a59d">
Profile
</div>
</div>
<div>
<style type="text/css">
.profileCSS_1fcd{background-image:url(image2.png);}
</style>
<div class="profileCSS_1fcd">
Profile
</div>
</div> Also, thinking about the next version of templ, supporting this, where the CSS would automatically output
|
This is now documented: https://templ.guide/syntax-and-usage/css-style-management#css-sanitization |
I have this problem where I can't easily pass a url to a css property that accepts the css function "url()"
The common sense tell me that the
templ.URL()
function works in this case but it doesn't. As an example I have this templ code:main.go file:
The generated code executed in
os.Stdout
and formatted looks like this:So it says that the url is a css unsafe value...
My solution was to preppend
url(\"
and append\")
string literals, something like this:And this works perfectly fine, but as you can see is verbosely obscene.
So i want to make a request of a function called something like
templ.CSSURL()
that behaves just exactly like theurl()
css vanilla function and also with url sanitization or something like thatThe text was updated successfully, but these errors were encountered: