|
 |
|
 |
11-11-2004, 11:48 AM
|
#1 (permalink)
|
|
Regular Contributor
Join Date: Oct 2004
Posts: 209
|
include vs. str_replace
a lot of people will include a header and footer on their pages and just have the content in the middle be different, or do content includes. i found a way thats in my opinion is twice as clean. make a file called layout.html, and where you want certian things to be put [Menu], [Content], [Poll] etc. then in your index.php file use the following script at the bottom:
$layout = implode("", file("layout.html"));
$layout = str_replace("[Menu]", $menu, $layout);
$layout = str_replace("[Content]", $content, $layout);
$layout = str_replace("[Poll]", $poll, $layout);
echo $layout;
that way if you decided to change your layout you dont have to worry about forgeting to close of a certian html tag, it'll be right in front of you. what do you guys think of this tip?
|
|
|
11-11-2004, 12:32 PM
|
#2 (permalink)
|
|
Moderator
Join Date: May 2002
Location: us.ca
Posts: 4,487
|
that method is known as developing with templates. the only real good use i see for that is if you are having a non-php person design your site.
includes would keep it more simple and more efficient. what your doing is sort of how templates work.
i don't see how that is twice as clean either.
it's all personal preference though, so to each his own.
__________________
Mike
|
|
|
11-11-2004, 02:29 PM
|
#3 (permalink)
|
|
Regular Contributor
Join Date: Oct 2004
Posts: 209
|
i think its cleaner, becuase if you have a table code in your header and footer, you cant preview the layout without copying and pasting the html codes, in my way you can just open up the layout file
|
|
|
11-11-2004, 04:30 PM
|
#4 (permalink)
|
|
Moderator
Join Date: May 2002
Location: us.ca
Posts: 4,487
|
ah, i guess it is how you develop. some editors have ftp access built in, so when you save, it uploads the files. most of my web apps communicate to a database on every page to generate the content, and when you're working with that, and/or apps that deal with cookies and session info, you're going to need to develop on a server w/ php anyway.
so, i suppose if your content doesn't have anything to do with a database, then it may be easier to preview .. although i'm not sure i'd change the basic structure of my apps for something that may make 1 fraction of the development process easier.
__________________
Mike
|
|
|
11-12-2004, 10:50 AM
|
#5 (permalink)
|
|
Code Monkey
Join Date: Mar 2003
Location: Evansville, IN
Posts: 75
|
I think there may be another benefit as well. Besides the fact that you have a more modular design, as well as presentation/logic decoupling, it also provides more maintainable code in the future. It is my experience that inline php with html is hard n the eyes. It is also a pain to go and change anything if you havn't just looked at it recently. Furthermore, templates provide the ability to add onto the site easier in the future. This is just my opinion though.
Oh, and the template engine I use most is patTemplate: http://www.php-tools.de/site.php
It has wonderful features that allow database driven apps to really use the presentation layer better than most. It also has a clean xml-based template file syntax. Ok, maybe not 'that' clean, but it works
Hope this adds something.
-Ted
__________________
while(1) fork();
|
|
|
11-16-2004, 08:05 AM
|
#6 (permalink)
|
|
Registered User
Join Date: Jul 2004
Location: San Francisco
Posts: 2
|
I think the include() method is also developing with templates, considering that the include('your_header_here') and include('your_footer_here') acts as "common" content.
I agree that it is left to the developer's style of coding. However, I do think falsepride has a good idea here. It does make it a little more pleasing to review if your content is very detail oriented.
You might also consider dividing all the content and using include() instead of of imploding. For instance, seperate the header, content, and footer into seperate files. Instead of layout.html, use layout.php. Include the following in the layout file:
PHP Code:
include('header.php');
include('content.php');
include('footer.php');
Then, you work on the content files individually and save disk space by not having include()'s in every file. It might not be a big deal if your site is small. Yet, if you have a lot of pages, the size can add up.
I believe this is similar to falsepride's method, but instead of implode(), I use include(). I think that both falsepride and my own methods allow for easier management than each "content" file including headers and footers. Although, I think my method uses less disk space than falspride's.
Again, I think it all comes down to a developer's preference.
Alcides
|
|
|
11-16-2004, 04:14 PM
|
#7 (permalink)
|
|
Regular Contributor
Join Date: Oct 2004
Posts: 209
|
seperating the layout file is destroying the puprose of my method. the whole point of my method was to keep the layout in one simple html file so that if you put your content within tables within tables like almost everyone does, then its easier if you decide to change your layout latter on. everything is right infront of you in the same file, no need to worry about looking at header and footer files and try editing them both at the same time, or remember exactly how many closing html codes you need.
|
|
|
11-16-2004, 04:16 PM
|
#8 (permalink)
|
|
PHP Pilgrim
Join Date: Aug 2004
Location: London
Posts: 170
|
There is way too much deep thought needed for this. No offence but FalsePride's code is fugly as hell I don't know what the hell is going on with that!
What is wrong with having the components all written as functions in functions.php then including this file where necessary. In the content of that page you'd then refer to the function.
Obviously then you'd also have includes for header, footer and nav bar/s.
On a website I'm continuously freelancing for on a voluntary basis, the chief admin (Jules for ref's sake) has everything set up in this functions.php file and he then refers to everything using the functions so you'd have this:
PHP Code:
<?php
include ('functions.php');
include ('db.php'); //This file contains the database connecting code which sets all the connections up so that within the main body of the page I can code in the queries straight away.
start();
/* This contains all the HTML header and title info that gets 'echo'd. The last HTML to get echo'd is the opening <body> tag. Possibly the last php code in this function is what starts the 'This page took x milliseconds to load' feature he has on his site.
It also sets up all the .css layouts. The site is laid out with absolute positioned div layers with the menus, header logo and right nav bar. */
echo "<!-- Main body stuff goes here for what will be unique for each page -->";
finish();
/* This function closes the body tag with </body>, stops the page loading timer and echos it in the right nav bar and just closes the </html> tag */
?>
If the site was to have something such as a regular poll on index.php then I could code the poll completely within the functions.php page then I could just put in the page content:
PHP Code:
poll("Today's question is \'Do you love Bush?\'","No!|Kinda...|Nah|YESSS!! =D ");
// The first variable is the poll question then the second variables is a pipe | delimited string with the answers in.
Surely this makes more sense then that bloody
Quote:
$layout = implode("", file("layout.html"));
$layout = str_replace("[Menu]", $menu, $layout);
$layout = str_replace("[Content]", $content, $layout);
$layout = str_replace("[Poll]", $poll, $layout);
echo $layout;
|
... crap?! (no offence!)
BTW guys I'm designing a project that will take templates to a new level so I will need to start a topic soon so you guys can help me brainstorm
$0.02
__________________
Davy - Programming since 1998 [CV]
Currently working on: n/a
Status: n/a
|
|
|
11-16-2004, 04:27 PM
|
#9 (permalink)
|
|
Regular Contributor
Join Date: Oct 2004
Posts: 209
|
as i stated in the post before this, my whole method was to have your whole layout in one html page instead of splitting it into parts. when i used to split mine into parts i was prone to html errors. my method was just a way to make it easy to aviod html errors. and theres such things as creative critizism and completely putting down someones code without trying it out.
|
|
|
11-16-2004, 04:54 PM
|
#10 (permalink)
|
|
PHP Pilgrim
Join Date: Aug 2004
Location: London
Posts: 170
|
I re read my post and yeah... twas a tad harsh. Sorry dude. I've just got back from a funeral wake so I'm probably a bit sour to say the least. Didn't mean to sounds so harsh...
I didn't read that post cus it was 2 minutes before I posted mine above so you posted it while I was still typing
Anyway I'm starting to understand it now but am still confused as to why it needs to be so damn complicated.
The picture I'm getting is that instead of getting lost amongst tags that might be open or closed or both (you don't really know hence why you say you continually try to debug both header and footer includes to find the fix) ... won't you just have the header leaving the <?php tag open with you being within the <body></body> tags and the footer closing the HTML tags and the ?> tag?
Then everything you put between the header and footer includes would be assumed unique php content.
I think I'm starting to understand this now...
I've studied your snippet now and it appears you have all the component coding within this html file. You are including it, imploding it (I don't know what imploding is btw, I'm workin on this with assumptions) then assigning where everything goes by just typing in for example a table with a poll in it...
Right?
So yeah...as said earlier on that would only really suit the non-coder. But then the non-coder shouldn't go near web page authoring IMHO...
I'm assuming the poll would then lookup the current poll question and available answers within a db based upon environment factors such as today's date or something...? Because other wise the 'coder' has no control over what the poll is without having to edit that raw layout.html file.
But still to be honest it seems alot more work then is necessary. I need to develop a totally-dynamic layout templating system, tho so I could be wrong. I could even refer to this method... maybe reverse engineer it a bit 
__________________
Davy - Programming since 1998 [CV]
Currently working on: n/a
Status: n/a
|
|
|
11-16-2004, 05:30 PM
|
#11 (permalink)
|
|
Regular Contributor
Join Date: Oct 2004
Posts: 209
|
implode simplay takes all the values in an array and uses a "glue" and glues them together. implode([glue], [array]); thats the proper syntax for it. my imploding the array which contains my layout page is just another way of opening the page into a variable, instead of using fread and fopen. maybe if you take a look at my file you mite know what i mean by my method my layout file for the time being can be found at http://skatinsatan.jelloz.net/layout.html right now my only thing that im replacing is the content, cuz its only a beta of what really is gonna be done, im still makin an admin section as i stated in another thread and right now that is my top priority along with some side projects. but as youll notice if you visist my layout then my homepage of http://skatinsatan.jelloz.net/ my index page opens the layout page, checks if theres a variable being passed in the url and if not, replacing [content] with the apropriate html. most of which is tables. but if i woke up tomorrow and decided i wanted my menu on the right and leave the open space which is becoming a poll in the future on the left i could simply do all the html editing in my layout file, and because i mostly use frontpage to make my tables faster for me then having to type it out myself because my time on my computer is limited, sux having to share it, i can change my layout in a matter of minutes and have a 100% garuntee that i didnt leave a open <td> tag etc.
|
|
|
11-16-2004, 05:46 PM
|
#12 (permalink)
|
|
PHP Pilgrim
Join Date: Aug 2004
Location: London
Posts: 170
|
Ahh see now that's interesting what you just said about waking up one day and deciding to move things just like that.
I'm designing an application that lays things out on a page in a particular way the user wants them to be according to thier preferred template. If the templates aren't satisfactory then I want them coded in such as way that even a novice can re-lay them. In that case then setting thier location using css positioned div layers is out of the question.
Hmm your way of templating may seem necessary come to think of it..  (ironic, ay?)
But then the components would have to be coded so that they would all conform onto the page without distorting it into a stupid fashion all over the shop. So then maybe have a set array of layout options that was pre-coded by myself would seem the way.
But then (but theeeeen...) say the user decides not to show a poll on thier website. The poll is a table about 100 by 200 so that's a massive chunk missing. The rest of the page's layout would then just slump into this gap and possibly creating a layout chaos.
Unless I set things to be absol;utely positioned using pixel widths instead of percentages then they would stay there. But then that gap would still be there...
ARGHH!!! I can't think!! Sod this I'm goin to bed...
I'll leave the other pros to tell me about some secrets they know of that might help
Night!
__________________
Davy - Programming since 1998 [CV]
Currently working on: n/a
Status: n/a
|
|
|
11-16-2004, 05:59 PM
|
#13 (permalink)
|
|
Regular Contributor
Join Date: Oct 2004
Posts: 209
|
i once had a page and it would play midi music in the background. but on an extremely slow computer like i would be at every other weekend it couldnt even load and play a small small midi song. so i had the page use a variable in url it was either ?music = on or ?music=off, if it was off, it would simply replace [music frame] with nothing instead of an iframe. if you decide no to display a poll then you could simple just replace the [poll] with nothing. now as for the extra space, it all depends how you have you tables set up. a td set to any width you like will not be displayed on your page if theres nothing in it. so if you have your poll in a td with a width set to 0 pixels and if the user decides to display the poll have it in a table set to a pixel width of your choice, if they decide not to, have the td have nothing inside, completely removing its display and if you have one next to it with its width set to 100% then itll cover the mising space. it would be a little easier to help with a potential layout chaos if i had some html code sitting right infront of me
|
|
|
11-16-2004, 06:06 PM
|
#14 (permalink)
|
|
Senior Grasshopper
Join Date: Jun 2003
Location: FL
Posts: 317
|
You guys ever use Smarty? I've tried several and then went the roll your own route. For the minor speed increase you get with smaller engines (or your own) it never turned out being worth the time.
The feature set, ease of use and caching (big plus) is hard to beat. Even non-coders should be able to wrap their design head around smarty's blocks and section loops.
What I typically do is have a system where each page includes a central pre-processing file that does all sorts of things (session, db, config, auth stuff, function calls, page header/footer), then processes whatever the page does (fetch client data/list, etc..) and pass the data to smarty if necessary. If the page is small enough its typically not worth it to template.
..all depends on the data and who is maintaining..
-r
|
|
|
11-16-2004, 06:10 PM
|
#15 (permalink)
|
|
PHP Pilgrim
Join Date: Aug 2004
Location: London
Posts: 170
|
*about to go bed*
Imagine a newspaper page. You see how the articles are all sitting next to each other in a seemingly random rashion with different widths and multiple columns and all the rest of it...
Nor look at that newspaper and image one of the articles was to disappear. It's not so easy to refill that space when, say there's a colspan in the mix somewhere.
I see what you mean about not being able to see the HTML, tho. I don't have any right now... but I spose only certain things can be taken away such as that poll.
I'd code each layout so that if for instance that poll wasn't there because the admin disabled them in thier control panel then the article next to it would expand to fill the gap.
Thinking about it now...it would be easier to have a set amount of templates that are carefully coded so they stay fluid when the admin makes changes to the highly configurable control panel. Keeping the novice away from developing his own dodgy template...this would be easier.
Then as time goes on and maybe the demand for new templates would increase, I could develop more of these carefully constructed templates for available free download.
Of course these would only be useful to owners of the package...
Oh head's spinnin.. I defo gotta go bed! Night!
__________________
Davy - Programming since 1998 [CV]
Currently working on: n/a
Status: n/a
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -8. The time now is 10:09 AM.
|
Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
|
 |
|