简体   繁体   English

如何用 Phoenix 渲染 img?

[英]How to render img with Phoenix?

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 lib/blog_web/templates/post/index.html.heex:19:21: expected closing for attribute value预期关闭"

What i missing?我错过了什么?

The HEEx documentation notes: HEEx 文档说明:

...code interpolation using <%= ... %> and <% ... %> are restricted to the body (inner content) of the HTML/component nodes and it cannot be applied within tags. ...使用<%= ... %><% ... %>的代码插值仅限于 HTML/组件节点的主体(内部内容),不能在标签内应用。

Instead, there is specific HEEx syntax attribute={expression} for interpolating attribute values.相反,有特定的 HEEx 语法attribute={expression}用于插入属性值。 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).引擎将根据需要正确插入引号和其他转义(或者,如果Blog.Image.url/2返回nil ,则完全省略该属性)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM