Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
With my current workflow, when I need to add a new CSS sheet or a new script to a 10 pages website, I need to do this 10 times. Same story if I need to rename one item in my navigation menu, or correct a typo in the footer.
That made me realize that I need to start learning PHP to handle that via includes, echo, etc.
Therefore my question is the following: is there a best practice to do this? (I guess this is relatively easy to implement a < ?php include 'header.php'; ?> and a < ?php include 'footer'; ?> but can become quickly messy when it comes to dealing with unique page title and descriptions, etc.
What are your suggestions?
Many thanks
If your building not object style PHP website using require_once seems just the proper way to achieve your goal. It's better than include because require spread all errors and _once only include it once (Even if there is an inclusion of the same file deeper in the including tree) :)
Elsewhere Assetic is library that handle exactly this :)
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
Is it bad practice to make all of my web pages .PHP pages? My main reason for doing this would be to prevent myself from writing repeating code for things such as my nav bar and footer and having these in separate files so that I can 'include' them in a way similar to below.
<?php include('nav.php'); ?>
I was made aware that you can also use JavaScript to do this however my thoughts are that relying on JavaScript for something as crucial as a nav bar would be a bad idea.
This is more of an opinion question than anything. I would not consider this to be bad practice as long as the following conditions are met:
If someone browses to https://example.com/nav.php or any similar "include only" nothing bad can happen as a side-effect. By bad I mean that if the .php expects to be included from wider context and ends up wiping the database for example. Easy way to prevent this is to declare a constant in your main "page" files and check that in the includes.
You find this approach easy to manage. In a larger page you might end up with a lot of files that are included from different places and expect certain global variables to be present. In these cases structuring your directories correctly and documenting the code is essential.
Happy coding!
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
On my site I have overall header and footer that are being created by functions header() and footer(), accessible in overall.php file. All sites are based on the same template so it looks like following:
<?
include ("/overall.php");
header();
echo "<div>";
// content of site
echo "</div>";
footer();
?>
I am thinking about another solution - maybe make a function createsite($content):
<?
function createsite($content)
{
header();
echo "<div>".$content."</div>";
footer();
}
?>
so that it would be simplier to create new page (there will be more than hundred of them). What are your ideas? I have been thinking about something like default include of overall.php on all sites. I hugely prefer clear PHP over frameworks like symfony or zend.
You will probably end up doing the same thing all of the frameworks are also doing, which is having a single point of entry for all your pages which will include all the neccesary functions (such as those in overall.php) and then redirecting the user to whichever actual page they want to see by including that as well.
Keep in mind that what you're currently building is (or rather, will be) a very basic framework in its own right.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I've got a question about php and pages :
Is-it worth to split the code into many other files ?
So at the end, the index.php would be composed of :
include 'footer.php';
include 'header.php';
include 'home.php';
include 'load.php';
[...]
Or should I put all the code in an unique page ?
Other question, if I include a php page, could all the vars created in the included page be used in the main page, or should-I use $_SESSION ?
Thank you all for your help !
As for your main question it’s worth. In fact, it will be a pain for you to manage your web page if you don’t follow this method in the long run. I am not a php guy but I can tell you this because this is a universal truth. The idea here is to write once and use anywhere necessary. You don’t want to write the same thing again and again, do you? That’s why we produce function when programming. We define a function and use it as many times we want. Now consider you have suddenly found that you need some modification to do , as you have created a function you can make the change in just one place and it will be reflected everywhere without touching a single line of code outside the function’s code. In actual project change is a constant. That’s why we find many design pattern in the software industry like MVC.
In a web project, mostly (not always) the header, navigation, footer are same across all the pages. Therefore, you should consider making different file for these stuff and you should always put your content in your index.php file which are unique for index page.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I know CSS and HTML to a reasonable degree, but I've never built a multi-page website before.
I have a header area of my site coded, and want it to display it across multiple webpages. How can I do this?
Save it as header.php/header.html whichever one you need. Then, from every file you code from then on, you can <?php include('header.php');?>. The files you include in HAVE to be .php or it wont work.
Same applies to jsp if for any reason php didnt work or you fancied a change of pace
Create a file called header.jsp then use the tag below when you want to include it on a page.
<jsp:include page="{header.jsp}"/>
This is maybe of no use to you but who knows it may help someone. Some people find this easier and some find this harder it depends on your programming background.
Use a web programming language (PHP / ASP.Net / Whatever you like) to include the navigation bar in all the pages. There are template languages like Twig that can do that for you in a smarter way and provide you with additional template features, if you might need them.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
Question: I've read a lot of tutorials/books that have taught putting the header and footers into their own files and using php to include them in the content pages.
However, if you have javascript running in those headers or footers- isn't this "bad" design- or does it not really matter?
I guess I take out the javascript if it's not needed for a page and I don't really mind CTRL+C. However I can see the usefulness and efficiency of making a change in only one file instead of all of them.
You should start using some template engine instead. Something to start with: Twig and Smarty
The most important feature you will like is called Template Inheritance
I would always separate your header and footer files out, it is a nightmare otherwise!
Just load in the JS when you need it, if using PHP just check the $_SERVER vars - http://uk.php.net/manual/en/reserved.variables.server.php