I have a composition document type that holds all general page information like page name, featured image, header/footer code. For one template, I am trying to write an if statement that gracefully handles a page that does NOT have a featured image uploaded.
For now, I am using this:
@{
var altText = Model.Value("pageHeader");
var typedMediaPickerSingle = Model.Value<MediaWithCrops>("featuredImage");
if (typedMediaPickerSingle != null) {
<img src="@typedMediaPickerSingle.MediaUrl()" class="img-fluid cds-rounded cds-shadow cds-p-0" alt="@altText" />
} else {
<img src="path/to/generic/image.jpg" />
}
}
This shows the featured image fine if there is one, but id there is no image selected - rather than show the generic image I would like, the site throws a 500 error.
I use this same statement on other pages and it works fine. Used here:
@{
var typedMediaPickerSingle = Model.Value<MediaWithCrops>("featuredImage");
if (typedMediaPickerSingle != null)
{
<section class="interior-page-header-img" style="background: url('@typedMediaPickerSingle.MediaUrl()'); background-repeat: no-repeat; background-position: center center; background-size: cover">
<div class="container">
<div class="row">
<div class="col-sm-10 col-lg-8">
<div class="interior-page-header-card">
<h1>This is a page title
</div>
</div>
</div>
</div>
</section>
}
else {
<section class="interior-page-header-no-img">
<div class="container">
<div class="row">
<div class="col-sm-10 col-lg-8">
<div class="interior-page-header-card">
<h1>This is a page title
</div>
</div>
</div>
</div>
</section>
}
}
Any idea why the server doesn't like the way that the image is being handled in the first one compared to the 2nd? thanks in advance
If statement around featured image in template
I have a composition document type that holds all general page information like page name, featured image, header/footer code. For one template, I am trying to write an if statement that gracefully handles a page that does NOT have a featured image uploaded.
For now, I am using this:
This shows the featured image fine if there is one, but id there is no image selected - rather than show the generic image I would like, the site throws a 500 error.
I use this same statement on other pages and it works fine. Used here:
Any idea why the server doesn't like the way that the image is being handled in the first one compared to the 2nd? thanks in advance
What does the 500 error say?
In chrome:
This page isn’t working
The website is currently unable to handle this request.
HTTP ERROR 500
Is there anything in the umraco logs for the error?
I believe I figured it out. There were 2 references to this image on the page, and the logic statement was only referenced for one of them. Doh
is working on a reply...