separating header and footers into their own files [closed] - php

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

Related

Adding header/footer in php (beginner level query) [closed]

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
How do we write files that are included in a php webpage using includes or requires.
(1)Is there any programming convention for the stuff?
For example: Do you add title in the header/footer file or not? if yes won't there be two titles one from the file included and one of the file that is including the footer/header file. What about bootstrap/js files do I include them in header/footer file or not? Because header/footer contain the usage of bootstrap classes.Won't importing them again and again increase the loading time of pages? (2)So basically do we add head section? if yes how do these values affect the current document?
As I am just beginner I would be glad if you could share some example that has html and php code in the file that gets included. Along with bit of explanation regarding the questions above (1&2). Thank you.
Instead of asking about includes or requires for rendering conventions I would suggest you learn how does a "Render Engine" works. For example Twig (from Symfony), Blade (from Laravel) or Mustache.
Basically, because you should want to distinguish between the different layers in your system. The rendering layer should not contain any other logic but rendering.
The easiest approach here, especially if you're new, is to go with the MVC (Model-View-Controller). And from there you can escalate the structure.
The answer for this question "Adding header or footer in php" is basically Twig or Blade.

Best way to manage multiple php includes? [closed]

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 :)

Separating PHP and HTML [closed]

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 7 years ago.
Improve this question
I apologize for asking such a beginner question, but i'm unsure on where else I could go to get such help. I'm currently creating a php website using GoggleApp Engine. I've created a very simple form using html in the same file as my PHP. Now, I know this isn't good practice, so I want to break up my html into it's own file. My only question is, how would I re-write this code assuming that my html is in it's own file?
It is perfectly fine to place your PHP code in the same file as your HTML code. However, if you are going to duplicate code, it would be best to have separate files (maybe say a header and footer file).
That way you can use require_once("header.php") and require_once("footer.php") in each file that wants to use the top and bottom portions of your code.
As Sven mentioned, you can look into templating. You can also look into coding habits such as MVC (Model View Controller) and similar methods.

Default php include on all pages [closed]

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.

How can i display a navigation bar across multiple webpages? [closed]

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.

Categories