{exists}
Renders its content when a file or directory is there, its else half when not.
Syntax
{exists $path} ... {/exists}
{exists $path} ... @else@ ... {/exists}
Parameters
-
path -
The path, used exactly as given - no app, data or
_includelookup applies, so build it fromAPPorDATA.
Description
{exists $path} is a plain file test: its content renders when the path is there, and the @else@ half when it is not. It is PHP's file_exists, so a directory counts as well.
The path is used exactly as given - none of PAD's application, data or _include lookups apply - so build it from the APP or DATA constant: a relative path is read from the server's working directory.
Use @else@ for the other half: {exists} does not cut an {else} of its own the way {if} and {auth} do. Written alone, without content, it shows nothing either way.
Examples
Content when the file is there
The path is used as given - built here from APP, the application's directory; @else@ renders when it is missing.
{set $notes = APP . '_data/filesDemo/notes.txt'}
{exists $notes}
<p>The notes are there.</p>
@else@
<p>No notes yet.</p>
{/exists}
{set $todo = APP . '_data/filesDemo/todo.txt'}
{exists $todo}
<p>The to-do list is there.</p>
@else@
<p>No to-do list yet.</p>
{/exists}
The notes are there.
No to-do list yet.
A directory counts too
file_exists answers TRUE for a directory as well, so the tag cannot tell a file from a folder.
{set $archive = APP . '_data/filesDemo/archive'}
{set $old = APP . '_data/filesDemo/old'}
<p>archive: {exists $archive}there@else@missing{/exists}</p>
<p>old: {exists $old}there@else@missing{/exists}</p>
archive: there
old: missing
See also
- {files} Scans a directory and iterates its entries, with path, file, ext, item and dir for each.
- {dir} Lists the names in a directory, one occurrence each - the plain version of files.
- {file} Writes its rendered content to a file under DATA/ instead of to the page.
- {if} Renders its content only when a condition holds, with elseif and else branches.