Php 模板中未显示Silverstripe 4 SiteConfig徽标图像
我在Php 模板中未显示Silverstripe 4 SiteConfig徽标图像,php,silverstripe,silverstripe-4,Php,Silverstripe,Silverstripe 4,我在SiteConfig中为logo上传创建自定义字段,然后在Settings中上传logo并尝试在.ss模板中呈现它。渲染后,模板中未显示任何内容。我在Silverstripe v3中使用了这段代码,所有代码都运行良好。现在在v4中不工作 这是我的分机: class SiteConfigExtension extends \SilverStripe\ORM\DataExtension { private static $db = array ( 'FacebookLin
SiteConfig
中为logo上传创建自定义字段,然后在Settings
中上传logo并尝试在.ss
模板中呈现它。渲染后,模板中未显示任何内容。我在Silverstripe v3
中使用了这段代码,所有代码都运行良好。现在在v4
中不工作
这是我的分机
:
class SiteConfigExtension extends \SilverStripe\ORM\DataExtension
{
private static $db = array (
'FacebookLink' => 'Varchar',
'TwitterLink' => 'Varchar',
'GoogleLink' => 'Varchar',
);
private static $has_one = array(
'Logo' => Image::class,
'DefaultImage' => Image::class
);
public function updateCMSFields(FieldList $fields)
{
parent::updateCMSFields($fields);
$fields->addFieldsToTab('Root.Social', array (
TextField::create('FacebookLink','Facebook'),
TextField::create('TwitterLink','Twitter'),
TextField::create('GoogleLink','Google'),
));
$fields->addFieldsToTab('Root.Main', array(
$logo = UploadField::create('Logo', 'Logo'),
$defaultImage = UploadField::create('DefaultImage', 'Default Image'),
));
$logo->setFolderName('Logo');
$defaultImage->setFolderName("Settings");
}
}
<% with $SiteConfig %>
<div style="display: inline-block;">
<div style="float: left;">
<h1 id="logo">
<% if $Logo %>
<a>$Logo.SetWidth(50)</a>
<% end_if %>
</h1>
</div>
<div id="logo-tagline" style="float:left;">
<% if $Title %>
<h1>$Title</h1>
<% end_if %>
<% if $Tagline %>
<strong>$Tagline</strong>
<% end_if %>
</div>
</div>
<% end_with %>
这是我的模板文件header.ss
:
class SiteConfigExtension extends \SilverStripe\ORM\DataExtension
{
private static $db = array (
'FacebookLink' => 'Varchar',
'TwitterLink' => 'Varchar',
'GoogleLink' => 'Varchar',
);
private static $has_one = array(
'Logo' => Image::class,
'DefaultImage' => Image::class
);
public function updateCMSFields(FieldList $fields)
{
parent::updateCMSFields($fields);
$fields->addFieldsToTab('Root.Social', array (
TextField::create('FacebookLink','Facebook'),
TextField::create('TwitterLink','Twitter'),
TextField::create('GoogleLink','Google'),
));
$fields->addFieldsToTab('Root.Main', array(
$logo = UploadField::create('Logo', 'Logo'),
$defaultImage = UploadField::create('DefaultImage', 'Default Image'),
));
$logo->setFolderName('Logo');
$defaultImage->setFolderName("Settings");
}
}
<% with $SiteConfig %>
<div style="display: inline-block;">
<div style="float: left;">
<h1 id="logo">
<% if $Logo %>
<a>$Logo.SetWidth(50)</a>
<% end_if %>
</h1>
</div>
<div id="logo-tagline" style="float:left;">
<% if $Title %>
<h1>$Title</h1>
<% end_if %>
<% if $Tagline %>
<strong>$Tagline</strong>
<% end_if %>
</div>
</div>
<% end_with %>
$Logo.SetWidth(50)
$Title
$Tagline
我想念什么?我做错了什么?感谢您的回答。图像在SilverStripe 4中进行了版本控制,因此您需要确保在保存SiteConfig对象时发布图像 您没有提到您使用的是哪个版本的SilverStripe 4,到目前为止,已经在4.1.2和4.2.0-beta1中讨论过了。这意味着,如果您应用于这些相关对象,则在保存SiteConfig模型时,这些对象将自动发布,例如:
private static $owns = ['Logo', 'DefaultImage'];
如果您使用的是SilverStripe 4.1.2或更高版本,则只需执行上述操作
对于早于此的版本,您可以在SiteConfigExtension中实现自己的钩子:
public function onAfterWrite()
{
if ($this->owner->Logo()->exists()) {
$this->owner->Logo()->publishSingle();
}
// ... same for other has_ones that are versioned
}
根据这个ss4似乎没有设置宽度谢谢你的回答,我读了这个链接,我尝试了你的例子,不再工作了。在我的
SiteConfigExtension
中,我将$owns=['Logo']
和dev/build?flush=1
放入Settings
页面,然后在中重新加载图像并保存。ss
模板尝试调用
,但仍然没有显示任何内容。但图像成功上传并保存。我的版本是4.1.0
Update:我还保存了SiteConfig
go-in-assets,发现上传的徽标是我发布他的手册。再次刷新首页后,如果您手动发布了图像,但它没有显示出来,那么您的模板可能有问题。对您的问题的评论表明您可能使用了错误的宽度方法-可以尝试ScaleWidth
。另外,如果您没有使用4.1.2,您可能需要尝试我的第二个示例