Copied to clipboard

Flag this post as spam?

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


  • Jason 22 posts 41 karma points
    May 20, 2012 @ 03:14
    Jason
    0

    Check document type property using IsNullOrEmpty or HasValue causing error loading MacroEngine script.

     

    Trying a test to check if my page contains an image using DAMP and if no image is selected to show a generic header image.  Below is the code I'm using.  Two issues.  In my dev environment below works fine, in my test environment on a shared hosted account (identical umbraco setup 4.7.1)  the below code fails.  My second question is how would I find the error being generated as all I get on my shared hosted account is Error loading MacroEngine script (file: ) .

    <umbraco:Macro  runat="server" language="cshtml">
    @if (String.IsNullOrEmpty(Model.productHeaderImage.mediaItem.Image.umbracoFile))
    {
       <img src="/images/generic-header.jpg" />
    }
    else
    {
       <img src="@Model.productHeaderImage.mediaItem.Image.umbracoFile" />
     } 
     </umbraco:Macro>

    Below is the umbraco.config output for the blank value:

     <productHeaderImage><![CDATA[]]></productHeaderImage>

    Below is the umbraco.config output for an example that contains an image value:

    <productHeaderImage>
              <DAMP fullMedia="">
                <mediaItem>
                  <Image id="1123" version="e9aeb8e3-c850-4099-8fe8-184347dafc8f" parentID="-1" level="1" writerID="0" nodeType="1032" template="0" sortOrder="59" createDate="2012-05-11T17:32:12" updateDate="2012-05-11T17:32:13" nodeName="mgms1" urlName="mgms1" writerName="admin" nodeTypeAlias="Image" path="-1,1123">
                    <umbracoFile>/media/1941/mgms1.jpg</umbracoFile>
                    <umbracoWidth>747</umbracoWidth>
                    <umbracoHeight>139</umbracoHeight>
                    <umbracoBytes>38989</umbracoBytes>
                    <umbracoExtension>jpg</umbracoExtension>
                  </Image>
                </mediaItem>
              </DAMP>
            </productHeaderImage>

     

     

    Thanks!

     

  • Trevor Loader 199 posts 256 karma points
    May 20, 2012 @ 03:27
    Trevor Loader
    0

    I think you need to check the property first before charging into mediaItem etc.  Try:

    @if (Model.HasValue("ProductHeaderImage"))
    {
    if (String.IsNullOrEmpty(Model.ProductHeaderImage.mediaItem.Image.umbracoFile)) {
     
    etc 


  • Jason 22 posts 41 karma points
    May 20, 2012 @ 07:37
    Jason
    0

    Thanks Trevor.  I took the .HasValue you suggested and used it as the basis of the check, the code below works as I would expect.

    <umbraco:Macro runat="server" language="cshtml">
      @if (Model.HasValue("ProductHeaderImage"))
      {
        <img src="@Model.productHeaderImage.mediaItem.Image.umbracoFile" />
      }
      else
      {   
        <img src="/images/generic-header.jpg" />   
      }
    </umbraco:Macro>
  • Trevor Loader 199 posts 256 karma points
    May 20, 2012 @ 08:42
    Trevor Loader
    0

    Just be careful...what if the editor deletes the image or crop.  You might end up with ProductHeaderImage having a value...but that value is XML markup with empty nodes...which might break still.

  • Jason 22 posts 41 karma points
    May 20, 2012 @ 19:46
    Jason
    0

    Yeah, I tested that senario and when an editor removes the image from the DAMP control and the check for HasValue gets to the else branch so the generic-header.jpg image is displayed.  Thanks for the follow up.

  • MikeD 92 posts 112 karma points
    Aug 23, 2012 @ 23:07
    MikeD
    0

    Jason,

    Did you ever get an answer to your second question?  I really could use that info as well...

    >>My second question is how would I find the error being generated as all I get on my shared hosted account is Error loading MacroEngine script (file: ) .

Please Sign in or register to post replies

Write your reply to:

Draft