Tag <cms:exists> only checks for the existence of a file relative to the 'snippets' folder - so, your example was incorrect.
SimonWpt wrote: Is there a way without PHP to check for the existence of an image (or file).
What happens with
object tag? Please try and test.
Non-existent images (404) usually do not display the object (width x height = 0 x 0) as in -
- Code: Select all
EMPTY:<br/>
<object border="5" data="https://interactive-examples.mdn.mozilla.net/media/cc0-images/grapefruit-slice-332-332.jp"></object>
<hr>
OK:<br/>
<object border="5" data="https://interactive-examples.mdn.mozilla.net/media/cc0-images/grapefruit-slice-332-332.jpg"></object>
<hr>
The <object> element is universal, well supported and provides many interesting capabilities, like events -
onerror,
onload. You can apply it to any user-uploadables - not exclusively images, but also media, pdfs, docs, files. Note that this element is a paired one, thus you can put as much HTML inside the pair as needed - an alt message to user, links, scripts, tags, a wall of invisible keywords that will appear in HTML for indexing or another placeholder image.
- Code: Select all
Replacement image:<br/>
<object data="https://interactive-examples.mdn.mozilla.net/media/cc0-images/grapefruit-slice-332-332.jp">
<p>Original media not found!<br/>
<img src="https://interactive-examples.mdn.mozilla.net/media/cc0-images/surfer-240-200.jpg" />
</p>
</object>
Events may help you notify Admin or User about some non-existent resource automatically via a JS XHR request or simply log to a file the path / name. Also events help override MSEdge's initial width-height props. For the latter it could be done as follows -
- Code: Select all
EMPTY:<br/>
<object border="5" width="0" height="0" data="https://interactive-examples.mdn.mozilla.net/media/cc0-images/grapefruit-slice-332-332.jp" onerror ="myErrorHandler()" onload="mySize();"></object>
<hr>
OK:<br/>
<object border="5" width="0" height="0" data="https://interactive-examples.mdn.mozilla.net/media/cc0-images/grapefruit-slice-332-332.jpg" onerror ="myErrorHandler()" onload="mySize();"></object>
<hr>
<script>
function myErrorHandler() {
//console.log(event.target);
event.target.setAttribute("height", "0");
event.target.setAttribute("width", "0");
//alert('Image does not exist:\n\n' + event.target.data);
}
function mySize() {
//console.log(event.target);
event.target.setAttribute("height", "auto");
event.target.setAttribute("width", "auto");
}
</script>
Tag <img> also supports events, although not as comfortable as object in my opinion. See
https://stackoverflow.com/questions/927 ... ken-images Although your question explicitly mentioned "without PHP", I must note that Couch itself is PHP-based, thus any possible new Couch-tag or function intended to simplify work
will be based on PHP. If you meant something different, then post appropriately. In my experience I created a couple of functions which return 1 or 0, in case file is found plus readable vs not found or not readable - one is <cms:call 'file_exists' file=myfile />, and another also similarly returns 1 or 0, but works with images only (thus PHP code is much smaller) - <cms:func 'is_image' src=''>. I think for your use-case HTML solution should work just fine. Please report your findings testing the example above.