bloggerblogger-dynamic-views

How to include image url If it doesn't have featuredImage blogger?


<b:with value='data:post.featuredImage.isYoutube ? resizeImage(data:post.featuredImage.youtubeMaxResDefaultUrl, 945, &quot;945:600&quot;): &quot;https://1.bp.blogspot.com/-aR5w9KXuWGU/XhSDNRAVuhI/AAAAAAAAHG8/dLxcaZxSgh0v85JG0mWRMQyEwqMgpL1_gCLcBGAsYHQ/w420-h280-c/no-image.jpg&quot;' var='highRes'>
  <b:with value='data:post.featuredImage ? data:highRes : &quot;https://1.bp.blogspot.com/-aR5w9KXuWGU/XhSDNRAVuhI/AAAAAAAAHG8/dLxcaZxSgh0v85JG0mWRMQyEwqMgpL1_gCLcBGAsYHQ/w420-h280-c/no-image.jpg&quot;' var='isSource'>
  <a expr:href='data:post.url'>
  <b:include data='{image: data:post.featuredImage,
    imageSizes: [512, 640],
    imageRatio: &quot;640:360&quot;,
    sourceSizes: &quot;(min-width: 954px) 842px,
    (min-width: 801px) calc(640vw - 640px), 
    calc(512vw - 512px)&quot;,
    enhancedSourceset: data:isSource}' name='responsiveImage' />
  </a>
  </b:with>
</b:with>

That is my code, works well but when the image is missing the url remains empty.


Solution

  • You can use data:post.featuredImage ? data:post.featuredImage : ".../no-image.jpg".

    <b:with value='data:post.featuredImage ? data:post.featuredImage : "https://1.bp.blogspot.com/-aR5w9KXuWGU/XhSDNRAVuhI/AAAAAAAAHG8/dLxcaZxSgh0v85JG0mWRMQyEwqMgpL1_gCLcBGAsYHQ/w420-h280-c/no-image.jpg"' var='postImage'>
    <b:with value='data:postImage.isYoutube ? resizeImage(data:postImage.youtubeMaxResDefaultUrl, 945, "945:600") : data:postImage' var='highRes'>
      <a expr:href='data:post.url'>
      <b:include data='{image: data:postImage,
        imageSizes: [512, 640],
        imageRatio: "640:360",
        sourceSizes: "(min-width: 954px) 842px,
        (min-width: 801px) calc(640vw - 640px), 
        calc(512vw - 512px)",
        enhancedSourceset: data:highRes,
        imageClass: "class names here"}' name='responsiveImage' />
      </a>
      </b:with>
    </b:with>