Template Toolkit Documentation

SYNOPSIS

Top
[% USE File(filepath) %]
[% File.path %]         # full path
[% File.name %]         # filename
[% File.dir %]          # directory

DESCRIPTION

Top

This plugin provides an abstraction of a file. It can be used to fetch details about files from the file system, or to represent abstract files (e.g. when creating an index page) that may or may not exist on a file system.

A file name or path should be specified as a constructor argument. e.g.

[% USE File('foo.php') %]
[% USE File('foo/bar/baz.php') %]
[% USE File('/foo/bar/baz.php') %]

The file should exist on the current file system (unless nostat option set, see below) as an absolute file when specified with as leading '/' as per '/foo/bar/baz.php', or otherwise as one relative to the current working directory. The constructor performs a stat() on the file and makes the 13 elements returned available as the plugin items:

dev ino mode nlink uid gid rdev size 
atime mtime ctime blksize blocks

e.g.

[% USE File('/foo/bar/baz.php') %]

[% File.mtime %]
[% File.mode %]
...

In addition, the user and group items are set to contain the user and group names as returned by calls to getpwuid() and getgrgid() for the file uid and gid elements, respectively. On Win32 platforms on which getpwuid() and getgrid() are not available, these values are undefined.

[% USE File('/tmp/foo.php') %]
[% File.uid %]      # e.g. 500
[% File.user %]     # e.g. abw

This user/group lookup can be disabled by setting the noid option.

[% USE File('/tmp/foo.php', noid=1) %]
[% File.uid %]      # e.g. 500
[% File.user %]     # nothing

The isdir flag will be set if the file is a directory.

[% USE File('/tmp') %]
[% File.isdir %]    # 1

If the stat() on the file fails (e.g. file doesn't exists, bad permission, etc) then the constructor will throw a File exception. This can be caught within a TRY...CATCH block.

[% TRY %]
   [% USE File('/tmp/myfile') %]
   File exists!
[% CATCH File %]
   File error: [% error.info %]
[% END %]

Note the capitalisation of the exception type, 'File', to indicate an error thrown by the File plugin, to distinguish it from a regular file exception thrown by the Template Toolkit.

Note that the File plugin can also be referenced by the lower case name 'file'. However, exceptions are always thrown of the File type, regardless of the capitalisation of the plugin named used.

[% USE file('foo.php') %]
[% file.mtime %]

As with any other Template Toolkit plugin, an alternate name can be specified for the object created.

[% USE foo = file('foo.php') %]
[% foo.mtime %]

The nostat option can be specified to prevent the plugin constructor from performing a stat() on the file specified. In this case, the file does not have to exist in the file system, no attempt will be made to verify that it does, and no error will be thrown if it doesn't. The entries for the items usually returned by stat() will be set empty.

[% USE file('/some/where/over/the/rainbow.php', nostat=1) 
[% file.mtime %]     # nothing

METHODS

Top

All File plugins, regardless of the nostat option, have set a number of items relating to the original path specified.

path

Top

The full, original file path specified to the constructor.

[% USE file('/foo/bar.php') %]
[% file.path %]     # /foo/bar.php

name

Top

The name of the file without any leading directories.

[% USE file('/foo/bar.php') %]
[% file.name %]     # bar.php

dir

Top

The directory element of the path with the filename removed.

[% USE file('/foo/bar.php') %]
[% file.name %]     # /foo

ext

Top

The file extension, if any, appearing at the end of the path following a '.' (not included in the extension).

[% USE file('/foo/bar.php') %]
[% file.ext %]      # html

home

Top

This contains a string of the form '../..' to represent the upward path from a file to its root directory.

[% USE file('bar.php') %]
[% file.home %]     # nothing

[% USE file('foo/bar.php') %]
[% file.home %]     # ..

[% USE file('foo/bar/baz.php') %]
[% file.home %]     # ../..

root

Top

The root item can be specified as a constructor argument, indicating a root directory in which the named file resides. This is otherwise set empty.

[% USE file('foo/bar.php', root='/tmp') %]
[% file.root %]     # /tmp

abs

Top

This returns the absolute file path by constructing a path from the root and path options.

[% USE file('foo/bar.php', root='/tmp') %]
[% file.path %]     # foo/bar.php
[% file.root %]     # /tmp
[% file.abs %]      # /tmp/foo/bar.php

rel(path)

Top

This returns a relative path from the current file to another path specified as an argument. It is constructed by appending the path to the 'home' item.

[% USE file('foo/bar/baz.php') %]
[% file.rel('wiz/waz.php') %]      # ../../wiz/waz.php

EXAMPLES

Top
[% USE file('/foo/bar/baz.php') %]

[% file.path  %]      # /foo/bar/baz.php
[% file.dir   %]      # /foo/bar
[% file.name  %]      # baz.php
[% file.home  %]      # ../..
[% file.root  %]      # ''
[% file.abs   %]      # /foo/bar/baz.php
[% file.ext   %]      # html
[% file.mtime %]      # 987654321
[% file.atime %]      # 987654321
[% file.uid   %]      # 500
[% file.user  %]      # abw

[% USE file('foo.php') %]

[% file.path %]           # foo.php
[% file.dir  %]       # ''
[% file.name %]           # foo.php
[% file.root %]       # ''
[% file.home %]       # ''
[% file.abs  %]       # foo.php

[% USE file('foo/bar/baz.php') %]

[% file.path %]           # foo/bar/baz.php
[% file.dir  %]       # foo/bar
[% file.name %]           # baz.php
[% file.root %]       # ''
[% file.home %]       # ../..
[% file.abs  %]       # foo/bar/baz.php

[% USE file('foo/bar/baz.php', root='/tmp') %]

[% file.path %]           # foo/bar/baz.php
[% file.dir  %]       # foo/bar
[% file.name %]           # baz.php
[% file.root %]       # /tmp
[% file.home %]       # ../..
[% file.abs  %]       # /tmp/foo/bar/baz.php

# calculate other file paths relative to this file and its root
[% USE file('foo/bar/baz.php', root => '/tmp/tt2') %]

[% file.path('baz/qux.php') %]         # ../../baz/qux.php
[% file.dir('wiz/woz.php')  %]     # ../../wiz/woz.php

AUTHORS

Top

Michael Stevens wrote the original Directory plugin on which this is based. Andy Wardley split it into separate File and Directory plugins, added some extra code and documentation for VIEW support, and made a few other minor tweaks.

COPYRIGHT

Top

Copyright 2000-2007 Michael Stevens, Andy Wardley.

This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.