Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Mike Manusama 45 posts 195 karma points
    Jan 17, 2023 @ 22:01
    Mike Manusama
    0

    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:

    @{
                    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

  • Huw Reddick 1929 posts 6697 karma points MVP 2x c-trib
    Jan 18, 2023 @ 08:45
    Huw Reddick
    0

    What does the 500 error say?

  • Mike Manusama 45 posts 195 karma points
    Jan 18, 2023 @ 14:01
    Mike Manusama
    0

    In chrome:

    This page isn’t working

    The website is currently unable to handle this request.

    HTTP ERROR 500

  • Huw Reddick 1929 posts 6697 karma points MVP 2x c-trib
    Jan 18, 2023 @ 14:39
    Huw Reddick
    0

    Is there anything in the umraco logs for the error?

  • Mike Manusama 45 posts 195 karma points
    Jan 18, 2023 @ 20:53
    Mike Manusama
    0

    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

Please Sign in or register to post replies

Write your reply to:

Draft