Friday, July 31, 2009

What's the best way to edit PHP files to customise existing files to my own templates?

I have some very basic HTML skills but I've put these on a back-burner in recent years and done what little web site coding I've needed to do in Dreamweaver.





Lately I've been working with some open source code which pulls information from a MYSQL database but the PHP files used are very basic. I'd like to adapt these files to fit in with my website's basic template but don't know how to edit the PHP. I'd like a nice simple way, in something like Dreamweaver where I can just cut the existing tables from the old document, drop them into the relevant section of my site and then just save it.





Can anyone explain the easiest way for me please?

What's the best way to edit PHP files to customise existing files to my own templates?
Dreamweaver has support for templates, but I don't think you'd find the system it uses intuitive or helpful.





Most Web sites are laid out as a template. There are common header items and tags and common footer items and tags; content tends to go to the same place.





Common header items:





%26lt;html%26gt;


%26lt;head%26gt;


%26lt;title%26gt;Basic Page%26lt;/title%26gt;


%26lt;/head%26gt;


%26lt;body%26gt;


%26lt;table%26gt;


%26lt;tr%26gt;


%26lt;td colspan="3"%26gt;%26lt;h1%26gt;My Web Site%26lt;/h1%26gt;%26lt;/td%26gt;


%26lt;/tr%26gt;


%26lt;tr%26gt;


%26lt;td%26gt;This is a navigation pane%26lt;/td%26gt;


%26lt;td width="10"%26gt; %26lt;/td%26gt;








Typically, content will appear in another table cell:





%26lt;td%26gt;Page-specific content%26lt;/td%26gt;





Common footer items:





%26lt;/tr%26gt;


%26lt;/table%26gt;






%26lt;p%26gt;Copyright © 2006 Me, Myself %26amp; I. All rights reserved.%26lt;/p%26gt;


%26lt;/body%26gt;


%26lt;/html%26gt;





Therefore, you can split each page into three parts: A common header file, a common footer file, and the page itself. You just include the header and footer content on the page.





For including pages, I prefer the PHP function require_once(), which serves to resolve conflicts between included variables and functions:





Here is an index page, for example, using the header and footer content noted above saved as their own PHP pages:





%26lt;? require_once("header.inc.php"); ?%26gt;


%26lt;td%26gt;


%26lt;h3%26gt;Here is a test page%26lt;/h3%26gt;


%26lt;p%26gt;This is my super-special test page. I hope you like it!%26lt;/p%26gt;


%26lt;/td%26gt;


%26lt;? require_once("footer.inc.php"); ?%26gt;





Using this method, you can change content as you like, but keep the same look and feel for each page; changes to look and feel made one place apply to all pages using the includes.
Reply:I normally code in asp.net/asp/vb, and it was easy for me to intergrate with the html. Why don't you use the code viewer or whatever dreamweaver has and start your layout, and then go to the php file and copy the particular code you need and stick it between the two tables on the html page? ASP.net allows for separation of code from the visual part... it's worth learning.
Reply:I think first we need to clarify what is meant by template.





First, there are Dreamweaver templates. These reside in Dreamweaver ONLY and never make it onto the website. Because what is REALLY happening is that when you modify a template, then each and every file that uses that template gets modified and then published.





Then you have server-side templates. In a PHP-based site that can mean one of two things...





1) Files that are primarily HTML with elements that you want to include on multiple pages, such as the header, footer, navbar, etc. Then, where you want the invidual pages to go, you've got a php line that looks something like this...





%26lt;?php include('thispage.htm'); ?%26gt;





or





%26lt;?php require('thispage.php'); ?%26gt;





The latter will display an error message if the specified file could not be opened.





2) Then you have PHP-based templating solutions like Smarty, where PHP code is seperated from page design, in the classic design pattern called MVC (Model-View-Controller). In a nutshell, the database is the model, the Smarty template is the View, and the PHP code binding it all together is the Controller.





Well now that I've confused everyone, in a nutshell...





PHP files are best opened in Code View, and if you're going to be cutting and pasting tables you should become familiar with working in that mode.





To test PHP code "live" and be able to preview, your site needs to be set to use PHP.





"Site" menu =%26gt; "Manage Sites"





Pick a site from the list =%26gt; "Edit" button





Click "Testing Server" in the "Category" list





Where it says "Server Model" select "PHP MySQL"





Where it says "Access" select FTP


(unless you're running a local web AND SQL server)


No comments:

Post a Comment