[TextMate] Current date in file templates

Allan Odgaard allan at macromates.com
Sun Oct 10 05:29:21 UTC 2004


On 9. Oct 2004, at 22:34, Isaac Rivera wrote:

> I came up with a pretty decent syntax file mysefl.

Okay, Rob also did some ActionScript support:
    http://one.textdrive.com/pipermail/textmate/2004-October/000422.html

> Trying to do a class template and have gotten most of it.
>
> Can you tell me how to have the perl script input the date in the 
> template file?
>
> Its not one of the shell variables, I know, but maybe its accessible 
> to the script.

These questions really belong on the ML, so I'm cc'ing the list :)

You can add your own variables to the environment by changing the 
command in the Info.plist, for example here I setup TM_DATE to be 
YYYY-MM-DD:

{
    name = "Date Test";
    extension = "txt";
    command = "[ -f $TM_NEW_FILE ] || TM_DATE=`date +%Y-%m-%d` perl -pe 
's/\\$\\{([^}]*)\\}/$ENV{$1}/g' <index.html >\"$TM_NEW_FILE\"";
}

So it's basically just 'var=value' before the perl command (you can set 
multiple variables). And the back-ticks (`) means that it's a command 
to execute, so TM_DATE is set to the result of running date +%Y-%m-%d.  
All this is of course related to bash and not really TextMate.

Another neat one is the full user name which we can get with niutil, 
here's an example:

    command = "[ -f $TM_NEW_FILE ] || TM_DATE=`date +%Y-%m-%d` 
TM_USERNAME=`niutil -readprop / /users/\\$USER realname` perl -pe 
's/\\$\\{([^}]*)\\}/$ENV{$1}/g' <index.html >\"$TM_NEW_FILE\"";


One important note: the commands above are prefixed with '[ -f 
$TM_NEW_FILE ] || '. This is to check if the file already exists, and 
if so, it won't do the "or" branch (i.e. overwrite it).

I really should have added this to the default templates! I'll be 
adding it to all templates shipping with 1.0.1 -- if you create your 
own templates, please add it to avoid templates from overwriting files!

The reason TextMate doesn't perform this check is, that the template 
could generate multiple files, or not generate TM_NEW_FILE at al.


Also, remember that you're not limited to using perl as command. You 
can write your templates in e.g. PHP and use php as the command to 
generate them.



Kind regards Allan




More information about the textmate mailing list