I am currently building up a static website with PHP.
What I want to have is the following:
There is one main.php file that includes all the common parts of the page (header, footer, navigation and so on) and a couple of pages like index.php, team.php, contact.php and so on.
I do want to be able to edit the main.php in that way, that is effects all the pages in my project. I do however want to be able to output some specific content for each single page by writing code directly in the specific file (not main.php but e.g. index.php). So I want to assign each page of the project to use main.php as the core template.
My main.php file which looks so far like this is here:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>PHP Static Site Genarator</title>
</head>
<body>
<div id="navigation">
<?php echo navigation()?>
</div>
<div id="pageWrap">
<header>
Header
</header>
<main id="content">
<?php echo $templateContent; ?>
</main>
<footer>
Footer
</footer>
</div>
</body>
</html>
and a couple of pages like index.php, contact.php and so on.
All of them look like this:
<?php include_once $_SERVER['DOCUMENT_ROOT'].'/essentials/settings.php'; ?>
<h1>
This is the h1 for the index page
</h1>
<?php require($_SERVER['DOCUMENT_ROOT'].'/essentials/exit.php'); ?>
I deliver some settings in my settings.php file and in my exit.php file I have the following code:
<?php
$templateContent = ob_get_contents();
ob_end_clean();
echo $templateContent;
?>
I need to somehow bind all the pages to be the part of main.php at the point where I output the $templateContent variable at
What is the right way for me to achieve this?
I personally would consider using a micro PHP framework like lumen, slim, fat-free-framework for making even the smallest PHP web application.
That said, below is the solution of the approach you took to solve the problem. I will keep the file structure and file naming similar to yours, even though there is a place for improvement here.
Lets consider the following application structure:
essentials/main.php
essentials/navigation.php
essentials/exit.php
essentials/settings.php
index.php
about.php
contact.php
As you can see, I have moved all common files into the essentials folder and left all pages in the root folder
essentials/settings.php
<?php
// start output buffering
ob_start();
essentials/navigation.php
<ul>
<li>index</li>
<li>contact</li>
<li>about us</li>
</ul>
main.php
<?php $templateContent = ob_get_clean(); ?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>PHP Static Site Generator</title>
</head>
<body>
<div id="navigation">
<?php include("navigation.php"); ?>
</div>
<div id="pageWrap">
<header>
Header
</header>
<main id="content">
<?= $templateContent; ?>
</main>
<footer>
Footer
</footer>
</div>
</body>
</html>
<?php require_once("exit.php"); ?>
essentials/exit.php
<?php
ob_end_flush();
The actual pages structure index.php, about.php and contact.php look similar:
index.php
<?php require_once("./essentials/settings.php"); ?>
<h1>
This is the h1 for the index page
</h1>
<?php require_once("./essentials/main.php"); ?>
I hope this helps to move your idea forward, but highly encourage you to investigate time and learn a modern approach for PHP application development. Laravel is a great stating point.
Maybe you want something like this?
Structure
index.php
about.php
core\function.php
core\setting.php
templates\header.php
templates\footer.php
templates\menu.php
core\function.php - all function in here
core\setting.php - all setting in here
templates* - html page
index.php
<?php
include_once("core\setting.php");
include_once("core\function.php");
/*
* code php for this file in here
*/
$message = 'index data';
include_once("core\header.php");
include_once("core\menu.php");
echo <<<HTML
<div>show content {$message}</div>
HTML;
include_once("core\footer.php");
?>
It looks like you are looking for template engine.
There are many out there like Twig, Blade, Smarty
Or you can write one alone like it is explained in this excellent post by David Adams
Related
my problem is that i have a navbar and need to put i on each page.
So Home, Profile, Search, Add have the same navbar fixed in bottom.
Now i want to save the links to the page in a diffrent file.
So something like:
<div class="botnav">
<a class="active" href="links.php?$homelink">Home</a>
Profile
Search
Add
</div>
is this even possbile with GET or POST?
Thank you
I think you need to create a page with your code:
header.php
<div class="botnav">
<a class="active" href="links.php?$homelink">Home</a>
Profile
Search
Add
</div>
In each page you can call that with
<?php include ('header.php'); ?>
The way I include a nav menu to be used on different pages is with PHP's include statement include().
<?php include("path_to_header"); ?>
What I recommend is to create a few extra files: path.php and header.php to hold your nav menu.
The path.php file should be located in the root of your project so that __FILE__ provides the actual root project path.
<?php
define("ROOT_PATH", realpath(dirname(__FILE__)));
?>
Next, handle the header being in a separate file header.php for reference.
<div class="botnav">
<a class="active" href="links.php?$homelink">Home</a>
Profile
Search
Add
</div>
Then in each of your pages include() the path.php file and also the header.php with another include statement where ever you choose.
<?php include("path.php"); ?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>El test</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<!-- Include header file where you need -->
<?php include(ROOT_PATH . "/header.php"; ?>
</body>
</html>
I am creating my first website from scratch and had seen something where you can reduce code by using PHP includes for sections of the site that are to be repeated. So far, I have a head.php (which I added due to my stylesheet.css being linked there and needing access to it on every page), header.php, footer.php, index.php, and other pages with the php extension (about, contact, that bunch).
Everything is appearing where I'd like it to except for one issue: when setting the body background color, everything (all includes: header.php, footer.php) seems to be in the body. I tested this by setting a border around the body, and it confirmed what I thought.
Does anyone have an idea what is going wrong? I am using flexbox in my header, footer, and other bits within the index file, but I don't think that should be affecting anything.
<!DOCTYPE html>
<?php include 'head.php'; ?>
<?php include 'header.php'; ?>
<body id="main-block">
<!-- Button links to Portfolio and Other stuff -->
<div class="flex-container">
<a class="main-button" href="#">Web Work</a>
<a class="main-button" href="#">Other Work</a>
</div>
</body>
<?php include 'footer.php'; ?>
</html>
your body tag is suppose to wrap around your header and footer elements. see below markup
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
<?php include (header);?>
<?php include (footer);?>
</body>
</html>
if u wanna change the background color of your elements just add css
element { background-color:pink }
I admit that I do not really understand your issue. But I'll give you a little snippet to start coding php:
<html>
<head>
<title>Welcome</title>
<style>
<?php include 'stile.php'; ?>
</style>
</head>
<body>
<?php include 'header.php'; ?>
<?php include 'footer.php'; ?>
</body>
</html>
I'm working on my first php site, I'm running into an issue I can't see to figure out. I'm trying to have one php page that contains my structure, and others that inject their html inside, while retaining url changes so I can still direct link pages.
So far this is what I'm doing, but it doesn't seem efficient:
index.php
<html xmlns="http://www.w3.org/1999/xhtml">
<?php include("head.php"); ?>
<body>
<div class="container">
<!-- Navigation header -->
<?php include("navigation.php"); ?>
<!-- Main container -->
<div id="MainContainer">
<?php include("home.php"); ?>
</div>
<!-- Footer -->
<?php include("footer.php"); ?>
</div>
</body>
</html>
about.php
<html xmlns="http://www.w3.org/1999/xhtml">
<?php include("head.php"); ?>
<body>
<div class="container">
<!-- Navigation header -->
<?php include("navigation.php"); ?>
<!-- Main container -->
<div id="MainContainer">
About me!
</div>
<!-- Footer -->
<?php include("footer.php"); ?>
</div>
</body>
</html>
This feels totally wrong, if I ever want to change my container class, or change the structure, I now have to do it in two places instead of one.
In ASP.net MVC I would have a Layout_Head.cshtml file that would contain my HTML structure and inside I can render views from different pages, the url changes but the layout is always rendered first and then the controller/actions take care of injecting the html of the needed views.
How do I replicate that in PHP?
Usually people use php includes for templating more like this:
header.php
<html>
<head>
<title></title>
</head>
<body>
<div class="container">
footer.php
</div> <!-- .container -->
</body>
</html>
about.php
<?php include('header.php'); ?>
... content goes here ...
<?php include('footer.php'); ?>
This is so you don't need to continuously repeat the start/end tags on every template you make.
The question is fairly simple, yet I've been looking around for an hour and found nothing:
make a page that is exactly the same as the home page, but a specific div has altered content
example index.html:
<html>
<head>
<title>title</title>
<style type="text/css">
/* css goes here */
</style>
</head>
<body>
<div id="stay">I wont change</div>
<div id="change">I will change</div>
</body>
</html>
so I want to be able to code a page so that it inherits the entire html from the index page (WITHOUT COPYING THE CODE), but a specific div (here with the id #change) to have different content. How would I go about doing this?
You don't really "inherit" code snippets, but I understand that you're trying to reuse the page content. From your posted code, it's hard to tell exactly how the change differs from the index. Is it just a content change or does the index page not have that div?
You have a couple of options. If just the content of the div is changing, you could use the same php page and then use jquery to change the content of the div, so something like
index.php
<? php include("page.php"); ?>
other page
<? php include("page.php"); ?>
// javascript to modify div
You could break the page into chunks and just include them as needed, so you could have a top.php and a bottom.php, and the index page could do
<? php include("top.php"); ?>
<? php include("bottom.php"); ?>
And then your similar page could do something like
<? php include("top.php"); ?>
// custom stuff here
<? php include("bottom.php"); ?>
If neither of these solutions work you could always use a templating engine to create a page template, though that may be a little much for your situation.
I see you have tagged this question in php So, I will give you answer inclusive of php implementation.
Create 3 pages. index.php about.php and foo.php
The objective is to show some content in index.php but all content in about.php
Call this page foo.php
<html>
<head></head>
<body>
<p> Show this in index.php </p>
<?php if($_SERVER['PHP_SELF'] === 'about.php'): ?>
<p> Show this in about.php </p>
<?php endif; ?>
</body>
</html>
Now, all you have to do is ... include foo.php in both pages.
Make the page you want and you can go about doing this:
<html>
<head>
<title>title</title>
<style type="text/css">
/* css goes here */
</style>
</head>
<body>
<div id="stay">I wont change</div>
<?php
if(basename($_SERVER['PHP_SELF'] == "other-page.php")){ ?>
<div id="change">I will change</div>
<?php }else{ ?>
<div id="change">Original div</div>
<?php } ?>
</body>
</html>
That takes the file name and based on that you can change content (if is only for one page, otherwise write a function/class based on that).
There are many ways to do this. Here are two, each with their own advantages and disadvantages.
Firstly, if you don't want to modify the page at all, you can add a small PHP code segment which will include a page passed in through the GET variable. For example
<html>
<head>
<title>title</title>
<style type="text/css">
/* css goes here */
</style>
</head>
<body>
<div id="stay">I wont change</div>
<div id="change"><?php require($_GET['page']); ?></div>
</body>
</html>
would mean that using the URL mypage.php?page=home.php would automatically include the contents of a file called home.php into that div.
Another way to do it is to divide up that page into 2 sections, and including both of them in any other page you use. For example, splitting the code into 2 seperate files, such as
top.php:
<html>
<head>
<title>title</title>
<style type="text/css">
/* css goes here */
</style>
</head>
<body>
<div id="stay">I wont change</div>
<div id="change">
bottom.php:
</div>
</body>
</html>
then in your PHP file you can use the following
require("top.php);
MY CONTENT HERE
require("bottom.php);
remember that you will need to use echo to output html code on this method if it is within <?php and ?> tags
hope this helps.
You can't do this will plain HTML.
To do it in php, first create template file like so: (template.php)
<html>
<head>
<title>title</title>
<style type="text/css">
* css goes here */
</style>
</head>
<body>
<div id="stay">I wont change</div>
<div id="change"><?=$main_content?></div>
</body>
</html>
Now, let's say you want to make a "contact me" page.
<?php
// in contact.php
$main_content = "Contact me at my#email.com
include "template.php";
?>
This will write the contents of template.php to the page and echo out the value of $main_content inside div#change
Now, this is generally frowned upon because managing your variables becomes difficult as the size of the template increases. To keep things sane, use a templating engine as all of the other answers are suggesting.
I'm a beginner in cake php. i have needed to convert my HTML template into Cake PHP template. any idea?
It is very easy.
Replace your template main file extension to .ctp
index.html to index.ctp
Look at the layout of you file (html code) and determine what section of that template you want to appear on all pages;
Usually most templates are setup as follows:
<html>
<head>
<title>My Site</title>
// You will include your javascript and css files here
<?php
echo $this->Html->css(array('cake.generic','default'));
echo $this->Html->script(array('myscript','jquery'));
?>
</head>
<body>
<div id="container">
<div id="header"></div>
<div id="body">
<div id="main_content" style="width:600px;float:left">
<?php
//Code for this should be in your home.ctp
// in your pages folder. Usually I cut this content from
// my template and place the whole thing in that file
// everything else happens magically
echo $content_for_layout;
?>
</div>
<div id="side_content" style="width:300px;float:left">
<!-- You can have content here manually or create elements and include them here like the following -->
<?php $this->element("sidebar_content"); ?>
</div>
</div>
<div id="footer">...</div>
</div>
</body>
You should then upload all images to the /img folder in your /app/webroot folder
Change your images path to reference /img folder.
You must do the same with all your CSS and JS files. Place them in their corresponding folders in the /app/webroot location.
Good luck!
Save your template in to APP/views/layouts/template.ctp.
Make sure it has at least two variables:
<title><?php echo $title_for_layout; ?></title>
and
<body>
<?php echo $content_for_layout; ?>
</body>
Fire up your view, or controller and add
$this->layout = 'template'; // view/method
or $layout = 'template'; // controller;
Look at the Cake default.ctp for ideas.