This question already has answers here:
How to create a template in HTML?
(3 answers)
Closed 9 years ago.
I looked around, but couldn't find a satisfying answer.
Problem:
I have a menu bar that appears on the top of the page. I want it to show across all of the pages on the website. So how would someone do that without copying the same code each time. Would someone use html, php, css, or javascript/jQuery to accomplish this?
Note: I want to have a separate html file to access the information from.
From what I have seen, this is typically done with php using a template file.
The template file may have HTML code in it that you want to display on every page, as well as placeholders for content that is page specific. e.g: template.php
<html>
<head>
<title><?php print $title; ?></title>
</head>
<body>
<nav>Test</nav>
<?php print $content; ?>
</body>
</html>
In this case, as long as $title and $content variables are set, you can then do a include 'template.php'; to output this HTML code in other php files.
Read more about php's include.
It seems that you will need to use include, although an explaination on how to use it (or at least an example can be found here: https://www.youtube.com/watch?v=XmoF-6vshSI
Related
I absolutely don't post a question here in SO unless I really can't find a way to solve my problem myself. I did a lot of googling and was not able to find a solution for this one problem I am about to describe.
Here is the problem. I am creating a templated php website. With templated I mean something like below:
<?php include("header.php");?>
<div id="content">
<div id="main">
<h2><?php echo($page_title);?></h2>
<?php
echo ($page_content);
?>
</div>
<?php include("sidebar.php");?>
</div>
<?php include("footer.php");?>
As you can see here page template ECHOES the content of the $page_content variable between header and footer sections to build the page.
To keep the code clean and separated (in my own way) I have been placing the html content in .txt files (let's say page1_content.txt) and assigning the txt content to this variable ($page_content) as below:
$page_content = file_get_contents("page1_content.txt");
My problem starts when I place some php code in page1_content.txt, lets' call this file page2_content.php (yes, I change the file from .txt to .php). Then I assign the content of this file to $page_content variable as below as usual:
$page_content = file_get_contents("page2_content.php");
Now, when the page template ECHOES page2_content.php contents the php code in it is also echoed as string and not executed, but I am trying to query a database and do some stuff in this file with some php code. I mean, I want the php code inside page2_content.php to be executed and the cumulative html code to be echoed by the "echo" line inside the template file.
How can I achieve this?
Please ask me any questions if you need more info/clarification.
Thanks
EDİT:
As many people here suggested the solution was including the file. Actually, I tried including the file before but it didn't look like it was working, it broke my template, so I though I was on the wrong track and quit the "include" way of doing this. Since everybody here is advising to use include I tried that again. I replaced the php code in "page2_content.php" with a basic 1-line code just to see if it gets executed before adding generated html code without breaking the template and it worked. Apparently my php code had a problem at first place and hence broke my template execution.
Now I have changed the template structure slightly and pages using the template, and it seems to work nicely. Thanks a lot everybody. I have up-voted every answer suggesting that I use include :)
As #Ali suggested, you could include the files. The other option which I highly suggest you do not use is the eval() function.
I think what you want to do is to include your content PHP file, not echo it (as you are doing with header.php and footer.php).
echo($page_content);
Would become as below:
include("page2_content.php");
You've already done this in your footer and sidebar, just use include()
This question already has answers here:
Multiple <head> and <body> tag
(3 answers)
Closed 7 years ago.
Now a days I am designing a website in php and I want to add more than 5 pages which should have same styles for ex- All the pages should have same header, footer, left side bar, right sidebar etc.
I know that I can do this using include() method but I am unable to use it effectively.
I have created some pages like below.
header.php-
<html><body><div><img src="logo.png"/></div></body></html>
leftsidebar.php
<html><body><div>Categories<ul><li>business</li><li>management</li><ul>div></body></html>
index.php- this is the main file where I wan to include files.
<html>
<body>
<div class="top">
<?php include("header.php"):
?>
</div>
<div class ="leftsidebar">
<?php include("leftsidebar.php"):
?>
</div>
</body>
</html>
the problem is that layout is not proper. I dont know where I have to use width and height style proprties for div whether I should use div width in index.php or header.php. And one problem is when I view the source code I can see some duplicate elements like html,head,body etc.
I would appreciate if somebody know how to use this effectively.
Looking forward to your answers.
First point:
you are including php files that contain html and body tags that are also present in the page where you are including them... keep them only in the initial page where you put the includables.
header.php should be:
<div><img src="logo.png"/></div>
leftsidebar.php should be:
<div>Categories<ul><li>business</li><li>management</li></ul></div>
Second point:
to have styles across the different pages you should use css external stylesheets and not inline styles. Have a look here
Your include files should not re-open the <html> and <body> tags! Wherever you use the <?php include 'something.php' ?>, imagine what's contained in that included file sitting directly in your code -- because that's all that's happening when you include via PHP.
Additionally, a PHP statement should end with a semi-colon (;), not a colon (:), like this:
<?php include("header.php"); ?>
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Lets say I have markup like:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<?php
PutIntoHeader("<script src="im_on_top"></script>")
?>
</body>
</html>
The PutIntoHeader function would put the given script into the head section of the html which is rendered. I might be missing something really obvious, but I cant seem to figure that out. Any ideas how the function would be able to do that?
The output would be:
<!DOCTYPE html>
<html>
<head>
<script src="im_on_top"></script>
</head>
<body>
</body>
</html>
Thank you for any ideas.
Edit:->
Hi, I feel like a noobie getting this kind of answer:) My system is highly complex and in the time I write header I dont know which scripts will get requested for that particular page as its all dynamic. I could put all html in variable before it gets rendered and then traverse it as xml and put it there manually while storing the scripts in an array but I dont want to go this road.
In almost all situations you can simply get away with combining and minifying you scripts into a single file. This slows down the initial loading time (first visit of site), but will speed up further visits / navigation because the file will be cached.
Of course YMMV and it depends on your specific situation. See for example this thread.
There is also the thing that user agents cannot do unlimited concurrent connections to the same domain to load resources.
See for example these two (somewhat outdated) benchmarks:
What's the maximum number of simultaneous connections a browser will make?
Max parallel http connections in a browser?
In the comments you said you are working with a million different widgets. Does this mean that these widgets all have completely specific and different code or is just the data different or presented differently? If it is just data / presentation you don't need different script for that.
If indeed you are talking about a million completely different codes for the different widgets (a million suddenly sounds like a made up arbitrary number) it indeed may make sense to not load everything in a single script. But this is impossible to answer by us, because it depends on the filesize, how fast you want to load the first visit, how many shared code it has and more numbers like that.
So to get back to your questions "How can I load specific scripts in the head?".
One option would be to not render it in the head, but place it just before the </body> and body tag instead. The advantage of this approach is that you know what script to load by then. The pages will load faster (because resources in the head will be first downloaded in full before the actual page is ever going to be rendered). The drawback of this approach is that there is a small latency before your script will kick in because it will be last thing that gets loaded in the DOM.
Another option would be to defer the rendering of you HTML on the PHP side and using it more like a templating engine. A simple example of this is by first getting all the widgets you want to display and only after you have done this start the HTML rendering in PHP.
some bootstrap file
<?php
// load widgets from db or wherever
$widgets = [
[
'template' => '/templates/widget1.phtml',
'script' => '/js/widget1.js',
],
[
'template' => '/templates/widget2.phtml',
'script' => '/js/widget2.js',
],
];
require __DIR__ . '/templates/page.html';
some template (page.phtml)
<!DOCTYPE html>
<html>
<head>
<title>My page with widgets</title>
<?php foreach ($widgets) { ?>
<script src="<?php echo $widgets['script']; ?>"></script>
<?php } ?>
</head>
<body>
<?php foreach ($widgets) { ?>
<?php echo $widgets['template']; ?>
<?php } ?>
</body>
</html>
P.S. in the above example I have used script tags, but it could also be expanded for css files also obviously.
If you don't want to put it directly in the head because at the time you don't know what's going to be needed, you need to carry out the logic beforehand.
You need to separate the logic from the view and have the view as a simple way of rendering your information.
First calculate everything about your page and leave yourself with variables which you can use to extract relevant information, then output the relevant html for the variables you have calculated beforehand.
This could be done yourself in one page, or what I would suggest, is using some sort of library to help out with this. Something like Kohana is really good at this, you could use Kohana to separate out your controllers, models etc and then use something like mustache as a templating library. Mustache lets you create views with only the most basic of logic in them (the way it should be).
Your PHP code has no way to know where the contents should be output. PHP loses its control over the data once the page is rendered.
You seem to be trying to write the content in the <body> and then have it injected in <head> section. Why don't you output the content in the <head> section instead?
<!DOCTYPE html>
<html>
<head>
<?php echo "<script src=\"im_on_top\"></script>"; ?>
</head>
<body>
<!-- Some HTML markup here -->
</body>
</html>
You may try something like this:
<head>
<?php echo '<link rel="stylesheet" type="text/css" href="theme.css">'; ?>
</head>
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to get useful error messages in PHP?
I have the following html list:
<ul id="ulGroups">
<?php
include('organizer_picturearchive_groups.php');
?>
</ul>
I am trying to include this file, which also includes another file:
<?php
include('lib/organizer_functions/picture_archive.php');
echo select_all_picture_category_groups();
?>
Basically, what I am trying to do is to call a function that is in the picture_archive.php file. I do that in the organizer_picturearchive_groups.php file.
I am including this file in the first file where the html list is. I am not pasting the whole code from there, because it is a lot, but it works.
However the include thing doesn't, because when I tried to make a test echo statement in the "organizer_picturearchive_groups.php" file it works and I can see the text I am printing.
Do you have ideas what may cause this problem ?
Double check the path. If organizer_picturearchive_groups.php is in the same directory as the code, then try configuring the path as relative:
<?php
include('./organizer_picturearchive_groups.php');
?>
Let's try to explain the title of my question to be the more concise I can: I'm basically designing a static HTML website from scratch. Nothing to worry about here.
The point is that I'm trying to include some links that will retrieve some items (a product inventory) from a database (and therefore the site won't be so 'static' anymore), as there're > 300 products and creating an html for each one is not feasible.
After googling and reading several sites for days, the "easiest" solution I came up with is to use PHP and MySQL. Again, nothing to worry about. Just took my time for reading documentation and move along.
My question is more related about the correct workflow for integrating both worlds. Let's see my idea in code:
This is one schematic example of the page where you can browse some products (e.g: product.html):
<html>
<head>
<title>My Site - These are our products</title>
</head>
<body>
<!--Site goes here-->
Search by name
Search by color
<!--rest of site goes here-->
</body>
</html>
Where the links
product_search_by_name.php
product_search_by_color.php
are actually a modified clone of the same page (product.html). This is, keeping same html code, plus the .php code embedded into it, as I want to have the DB results displayed into a div on that same page, keeping exactly same layout.
So, am I doing this right if I want to maintain the appearance of the whole website? I'm absolutely wrong from the base and should start again? Should I give up and work selling frappuchinos on a Star*ucks?
As a sample of the idea I want to achieve is the following: http://www.w3schools.com/tags/default.asp (when you click on the left menu bar, the center zone updates with the content). By the way, are they using AJAX on that website to update just the center zone, or I'm misunderstanding what is AJAX for?
I'm sure I'm missing something but I'm too confused to separate the sheep from the goats, so I'd thank a lot any tips you can give to me (and additional documentation on the internets to read as well).
There are two main ways to merge or migrate from static HTML to dynamic HTML (PHP, PERL, whatever).
(1) One is to have most of the contest as HTML, and the stuff like inventory as dynamic.
<html>
<head>
<title>My Site - These are our products</title>
</head>
<body>
<h1>My Site - These are our products</h1>
<?php
// php code to retrieve links
?>
</body>
</html>
(2) To have a full PHP site.
<?php
echo "<html>" . "\n";
echo "<head>";
echo "My Site - These are our products"
echo "</head>";
echo "<body>" . "\n";
// php code to retrieve links
echo "</body>" . "\n";
echo "</html>" . "\n";
?>
Many developers start by merging both HTML & PHP.
I suggest to learn how to do a very simple but full php site, connect to a database, retrieve some records with a S.Q.L. query, display them as read-only text or links, and later you may change to the other HTML plus PHP way of doing things.
There are several editors and tools to help develop in PHP, specially by looking for a PHP function, or just highlight HTML tags. Scintilla (Linux) or Notepad++ in windowze, its a very simple yet useful tool.
Cheers.
Well, if you want it to make it using Ajax ...
You might want to do the following.
Create the two files as you said, those are not to be included in the HTML file.
Create a JavaScript function to call the files, You can use jQuery api, to link to php. http://api.jquery.com/jQuery.post/ or http://api.jquery.com/jQuery.get/
Link the click of the button inquired to call the JavaScript function, and add html you get from php to the tag you want.
You can create a index.php page and put down your html code in it. It looks like this
<html>
<body>
<!--- links goes here -->
Search Products
</body>
</html>
If you are retrieving products from a database and if you want to create multiple links for the products create a function in your function.php which pulls all the product name from the database. Now add this function into your index.php
index.php
<?php include ('functions.php')?>
<html>
<body>
<h3>Product List</h3>
product name
</body>
</html>
Always embed html in php file. Do not embed php in html file. File should have .php extension.