Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Dreamweaver CS5 offers another feature for managing your code, where you want to use the code over and over again. It's called the Snippets panel, and it's been a part of Dreamweaver for quite a while. For this exercise, I'll use the version of my site in the folder 03_managingcode\06_snippets. The Snippets folder is located in the same panel group as the Files panel. It's populated with some default snippets, including commenting, footers, form elements, and many others, but you can add your own snippets quite easily.
For this exercise, I'll start with the file footer.php that's in the _includes folder of my site. I'll look at the file in Code View in fullscreen and scroll down to the dateutil section. I'm going to take a few elements of this php code and make them into a reusable code snippet. I'll start with the PHP Include command up at the top. I'm going to move the code around a little bit to get the code ready to use in a code snippet, but I won't save the changes to this file; I'm only going to use it as a resource.
I'll first select the include command from the first php code block, and I'll copy it to the clipboard. Then I'll go down to the other php code that's calling the DateUtility class. I'll place the cursor within the php code block and press Enter, and then paste in the Include command. Then I'm going to change the indenting of this code. I'll select the entire code block and then press Shift+Tab until all of the code is aligned left in the page.
The purpose is just to strip out all the tabs before I create the snippet. I'll leave the code selected, and I press F4 to show my panels again. Then I'll go to the Snippets panel. Within the Snippets panel, you can add a new snippet to any of the existing folders, or you can create your own folder. I'll right-click in the panel, but not on any of the folder icons, and choose New Folder, and I'll name my new folder PHP. Then I'll right-click again and choose New Snippet.
The code that I'd already selected in the editor area is automatically copied into the snippet dialog. You can also manually copy and paste, if you like. I'll name my new snippet Get Current Time. You can select your snippets to one of two types. If you choose Wrap selection, you'll be inserting code before and after selected code. If you choose Insert block, that means you're inserting all of the code as a single block. That's the type that I want to use.
In the Description, you can type in anything you want. I'll type in, "Use my custom DateUtility class" and click OK. Anytime you want to use that code, you'll have it available in your Snippets folder. I'll go back to footer.php, I'll go back to the top of the page, and I'll remove the php code block at the top, and I'll go down to the bottom and remove that php code block as well. Now, in the future, if I want to use the snippet, all I have to do is make a little bit of space, then go to the Snippets panel and then double-click the snippet, or drag it where I want it to go.
Get unlimited access to all courses for just $25/month.Become a member