Skip to content

Renaming CakePHP template file extensions for 1.2

2008 July 22
by Eddie

A good many people have hit the CakePHP group asking what needs to be done to upgrade from 1.1. to 1.2.

I imagine the most painful changes are the loss of $html helper calls in the views, and $this->generateList calls in controllers.

$form helper will handle all the deprecated html calls, and $this->Model->find(‘list’); will handle select boxes.

Of course most people notice immediately that templates and layouts no longer use .thtml extensions.

If your running Linux, changing all the template extensions from thtml to ctp is a breeze.
Just use the bash file below in your views directory.

It is worth noting this script will rename any extension to any new extension on all matching files in a directory. It is not limited to CakePHP, that is merely the need that begat its creation.

If no files match in a particular directory a message is printed; otherwise files are listed and renamed.


#script by edward a webb
#renames files recursively based on old and new extensions passed into the script.
#lists files that match or prints no match message for each sub-directory
if [ $# -ne 3 ]
	echo "Usage: $0 dir_name old_ext new_ext "
	exit 1
pushd $TOPD >/dev/null
for DIR in $DIRS
	if [ -d $DIR ]
		pushd $DIR >/dev/null 2&>1
		echo NOW IN: $DIR;echo " "
		FILES=`ls *.$OEXT 2>/dev/null`
		dummy=`echo $FILES | cut -d" " -f1`
		if [ $dummy ]
			echo "	Files to rename";echo " "
			echo "	No files with extension .$OEXT"
		for FILE in $FILES
			if [ -f $FILE -a -w $FILE ]
				echo "		$NAME"
				mv $FILE $NAME.$NEXT
		echo " "
		popd >/dev/null 2>&1

The script can be saved anywhere, and requires 3 parameters, the directory, the old extension, and the new one.

# ./renameExt /srv/www/cake/app/views thtml ctp

Leave out the hash(#) that represents your ‘nix command prompt..duh.

And as Peter points out, don’t use periods ‘.’ in the extensions, just the letters.

2 Responses leave one →
  1. September 29, 2008

    Hello there,

    i was searching for this kind of script (i’m lazy, yes) but it nearly took me 1/4 of an hour to find that the “.” in the command line for the extension are in excess 😉

    wouldn’t took me this long to write the script by myself. but thanks anyway.

  2. Eddie permalink*
    September 29, 2008

    My apologies for your lost 15 minutes.
    I posted from memory and forgot the script already inserted the separator. Thanks for catching that.

    For everyone else I updated the command line, everything should work as promised.

Leave a Reply

Note: You can use basic XHTML in your comments. Your email address will never be published.

Subscribe to this comment feed via RSS