如何用凤凰渲染IMG?

发布于 2025-02-09 03:15:32 字数 282 浏览 2 评论 0原文

我是凤凰城的新手。我需要在主页上向他们显示所有帖子和图片。我在我的索引模板中写了下面的代码:

< img src =“<%= blog.image.url({ @post.image,@post},signed:true)%>”>

我得到了此

lib/blog_web/templates/post/index.html.heex:19:21:预期关闭“ for Attribute value

我缺少什么?

I'm new in phoenix. I need to display all the posts and pictures to them on the main page. I written below code in my index template:

<img src="<%= Blog.Image.url({@post.image, @post}, signed: true) %>">

And i got this

lib/blog_web/templates/post/index.html.heex:19:21: expected closing"for attribute value

What i missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

画尸师 2025-02-16 03:15:32

...使用&lt;%= ...%&gt;&lt;%...%&gt;的代码插值仅限于身体(内部HTML/组件节点的内容),不能在标签中应用。

取而代之的是,有特定的HEEX语法attribute = {expression}用于插值属性值。在您的示例中,您将使用

<img src={Blog.Image.url({@post.image, @post}, signed: true)}> 

引擎将正确插入引号和其他逃脱的引擎完全属性)。

The HEEx documentation notes:

...code interpolation using <%= ... %> and <% ... %> are restricted to the body (inner content) of the HTML/component nodes and it cannot be applied within tags.

Instead, there is specific HEEx syntax attribute={expression} for interpolating attribute values. In your example, you'd use

<img src={Blog.Image.url({@post.image, @post}, signed: true)}> 

The engine will correctly insert quotation marks and other escaping as required (or, if Blog.Image.url/2 returns nil, omit the attribute entirely).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文