I am learning SilverStripe by developing a simple project. Now I am having a problem with displaying images in the template file.
This is my BlogPage.php
<?php
namespace {
use SilverStripe\AssetAdmin\Forms\UploadField;
use SilverStripe\Assets\Image;
class BlogPage extends Page
{
private static $can_be_root = false;
private static $has_one = [
'Banner' => Image::class,
];
public function getCMSFields()
{
$fields = parent::getCMSFields();
$fields->addFieldToTab('Root.Attachments', $banner = UploadField::create('Banner'));
$banner->setFolderName('banners');
return $fields;
}
}
}
I can upload the banner image inside the Admin panel. I am trying to display the image in the BlogPage.ss like this.
<% with $Banner.ScaleWidth(750) %>
<img class="my-custom-class" src="$URL" alt="" width="$Width" height="$Height" />
<% end_with %>
But it is not displaying anything. When I inspect the URL in the console, it is (unknown). What is wrong? How can I display the image?
Edit
I edited question according to Robbie's answer below.
This is my BlogPage.php
<?php
namespace {
use SilverStripe\AssetAdmin\Forms\UploadField;
use SilverStripe\Assets\Image;
class BlogPage extends Page
{
private static $can_be_root = false;
private static $has_one = [
'Banner' => Image::class,
];
private static $owns = [
'Banner',
];
public function getCMSFields()
{
$fields = parent::getCMSFields();
$fields->addFieldToTab('Root.Attachments', $banner = UploadField::create('Banner'));
$banner->setFolderName('banners');
return $fields;
}
}
}
This is my BlogPage.ss
<% with $Banner %>
<img class="my-custom-class" src="$URL" alt="" width="$Width" height="$Height" />
<% end_with %>
<h3>$Title</h3>
$Content