Copied to clipboard

Flag this post as spam?

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


  • Matthew 93 posts 153 karma points
    Jun 13, 2012 @ 22:10
    Matthew
    0

    check if image exists

    More dufus problems: ImageGen displays 'Hello World!' when no image exists.  I have a doctype laid out for articles with ~15 images, but not all images will be included in every article so I need to just skip the ones that aren't there.

    The following shows me the image ID but doesn't skip it if there's no value for 'picId'.  Using any other operator aside from != null throws an 'Error loading MacroEngine script'.

    @{
    var pic = @Parameter.figure;
    var picId = @Model.GetProperty(pic);
    if (picId != null)
    {
    <a href="@Library.MediaById(Model.GetPropertyValue(pic)).umbracoFile">
    <img src="/imagegen.ashx?width=550&[email protected](Model.GetPropertyValue(pic)).umbracoFile" alt="" />
    </a>
    }
    }

    I've tried everything I can find about checking images but still not getting it.

    Or, if there's a better way to approach the problem, I'm open.

  • Douglas Ludlow 210 posts 366 karma points
    Jun 13, 2012 @ 22:19
    Douglas Ludlow
    0

    Yeah, just check to see if the media node's id is 0, like so:

    @using umbraco.MacroEngines
    @inherits DynamicNodeContext
    @{
    var pic = Parameter.figure;
    var picId = Model.GetProperty(pic);
    var media = Library.MediaById(picId);
    if (media.Id != 0)
    {
    <a href="@media.umbracoFile">
    <img src="/imagegen.ashx?width=550&[email protected]" alt="" />
    </a>
    }
    }
  • Matthew 93 posts 153 karma points
    Jun 13, 2012 @ 23:31
    Matthew
    0

    Thanks for the quick reply Douglas, and the var media refinement.  Is that mostly to clean up my sloppy guesswork image tags?

    Unfortunately, using your code as is still throws the 'Error loading MacroEngine script', so I wonder if I've pooched something elsewhere.

    If I strip it down (use my old image tags instead of yours, kill the 'if') and toss in:

    <p> pic = @pic </p>
    <p> picId = @picId </p>
    <p> media = @media </p>

    When there is an image, it shows the image and:
    pic = fig1
    picId = 1214
    media =

    When there's not, it's 'Hello World!' and:
    pic = fig1
    picId =
    media =

    Based on picId returning a number if there's an image, I thought I had already got the image ID, and was just tripping over not knowing what is the correct way of testing for the empty picId.  Should there be some kind of Int32 or string conversion in there?  Looks like a number when it's a number but seems like it's not if it's not.

  • Matthew 93 posts 153 karma points
    Jun 13, 2012 @ 23:50
    Matthew
    0

    Also, if I add in media.Id:

    <p> mediaID = @media.Id </p>

    It throws the same error.

  • Douglas Ludlow 210 posts 366 karma points
    Jun 13, 2012 @ 23:51
    Douglas Ludlow
    0

    Sorry, this should work:

    @using umbraco.MacroEngines
    @inherits DynamicNodeContext
    @try
    {
    var pic = Parameter.figure;
    var picId = Model.GetProperty(pic);
    if (picId != null)
    {
    var media = Library.MediaById(picId.Value);
    if (media.Id != 0)
    {
    <a href="@media.umbracoFile">
    <img src="/imagegen.ashx?width=550&[email protected]" alt="" />
    </a>
    }
    }
    }
    catch (Exception e)
    {
    <!-- @e.ToString() -->
    }
  • Douglas Ludlow 210 posts 366 karma points
    Jun 13, 2012 @ 23:58
    Douglas Ludlow
    0

    Model.GetProperty(string alias) returns an IProperty object. You can then access the value with .Value (if it's not null, obviously).

  • Matthew 93 posts 153 karma points
    Jun 14, 2012 @ 00:15
    Matthew
    0

    Awesome!  Very much appreciated, and thanks for the explanation.  I will study up on those refinements.

Please Sign in or register to post replies

Write your reply to:

Draft