【发布时间】:2017-10-24 20:54:51
【问题描述】:
我无法显示通过 Apostrophe 小部件上传的图片。具体来说,我正在创建一个“英雄”小部件,在图像顶部带有背景图像、标题和描述文本。该小部件似乎工作正常,因为我可以在其中添加标题和描述文本,但图像不显示。我在命令行中看到的错误是:
'模板警告:无法检索附件 url,因为它丢失了,已设置默认图标。请尽快解决这个问题!'
小部件的 index.js:
module.exports = {
extend: 'apostrophe-widgets',
name: 'heroimage',
label: 'Hero Image',
addFields: [
{
name: 'hero-image',
label: 'Hero Image',
type: 'attachment',
widgetType: 'apostrophe-images',
required: true,
extensions: [ 'jpg', 'png' ],
extensionMaps: {
jpeg: 'jpg'
},
// uploadfs should treat this as an image and create scaled versions
image: true,
options: {
minSize: [ 1280, 900 ],
limit: 1,
}
},
{
name: 'hero-title',
label: 'Hero Title',
type: 'area',
options: {
widgets: {
'apostrophe-rich-text': {
controls: [ 'Bold', 'Italic', 'Link', 'Unlink' ]
}
}
}
},
{
name: 'hero-description',
label: 'Hero Description',
type: 'area',
options: {
widgets: {
'apostrophe-rich-text': {
controls: [ 'Bold', 'Italic', 'Link', 'Unlink' ]
}
}
}
}
]
};
widget.html:
<div class="hero align-center-middle text-center"
style="background: url('{{ apos.attachments.url(image.attachment) }}'); background-size:cover; background-position:center;">
<div class="grid-x grid-padding-x">
<div class="cell">
<h1>
{{ apos.area(data.widget, 'hero-title') }}
</h1>
</div>
<div class="cell">
<p>
{{ apos.area(data.widget, 'hero-description') }}
</p>
</div>
</div>
</div>
我已经尝试了 '{{ apos.attachments.url(image.attachment) }}' 中的所有代码组合,但似乎没有任何效果。我做对了吗,有什么我遗漏的吗?非常感谢任何帮助。
谢谢, 乔恩
【问题讨论】:
-
image是您在其他地方创建的 nunjucks 变量吗?很难从你的 sn-p 中分辨出来。我的第一个想法是您应该将data.widget.hero-image传递给 url 助手。尝试将您传递给帮助者的数据记录下来,例如{{ apos.log(image) }},以确保您有一些东西。 -
hero-image 是 nunjucks 变量。我已经尝试使用 'data.widget.hero-image' 到 url helper 和 '{{ apos.log(image) }}',但我仍然收到 'impossible to retrieve the attachment url' 错误。跨度>
-
使用这个时似乎可以工作 - {{ apos.attachments.url(data.widget['hero-image'], { size: 'full'}) }}。
标签: apostrophe-cms