I'm quite new to the whole php scene but I have managed to make a php header and footer and include them on my pages so I dont have to edit the header and footer on each page now. My next challenge is making a whole template for something like a blog page where if I change the template then all the blog pages will change accordingly but the content will of course have to remain the same much like the php header and footers I have. I have read a bit about theme engines etc but they all seem to be quite confusing, and I don't wish to convert it to wordpress. So what are my options as to making a template? thank you in advance.
You can simply use smarty, powerfull template engine for PHP.
First - create template with html base, header, footer. Save it to templates/_Frame.html
<!DOCTYPE html>
<html>
<head>
<title>{block name='Title'}{/block} - My website</title>
</head>
<body>
<div>Header and some other stuff</div>
{block name='Content'}{/block}
<div>Footer and some other stuff</div>
</body>
</html>
Then, crate a template file for each page. If its a blog with same look on each page and the only variable thing is the post content - you need only 1 template. Lets call it 'Post.html'
{extends '_Frame.html'}
{block name='Title'}{$Post.Title|escape}{/block}
{block name='Content'}{$Post.Content}{/block}
In php - do such thing:
<?php
//Lets say at this poin you've got $BlogPost = array('Title' => 'Blog post title', 'Content' => 'Body of blog post')
$S = new Smarty();
$S->assign('Post', $BlogPost); //This creates new variable $Post which is availible inside templates.
$S->display('Post.html'); //this displays your template
?>
|escape - escapes all html in variable < goes ^lt; etc.
Related
I am quite new in php
I would like to make a website where all pages has a default menu with 4 choices.
Then, depending of the choice a specific sub-menu will be showed in a new page (including the main menu).
I could obviously do something like:
index.php - main-menu - sub-menu1
index.php - main-menu - sub-menu2
index.php - main-menu - sub-menu3
But, is there any other way I can make a single page "sub-menu" and show different "sub-menu 'div' " so that I dont need to create a new page for each sub-menu?
Maybe I should use smarty php template engine?
Could anybody put me in the right way or give any suggestions? either advice`?
Exemple: if my index is something like:
<html>
<head></head>
<body>
<?php include ("main-menu.php"); ?>
</body>
</html>
Do I have to create different page for each submenu? Like...
<html>
<head></head>
<body>
<?php include ("main-menu.php"); ?>
<?php include ("submenu1.php"); ?>
</body>
</html>
or I can create page "submenu" and show that specific submenu I want base on a page title for exemple?
I write my template in index.php using bootstrap.
In this template there is a place <div id="content"></div> which is dynamic content of this site.
It means that, there I want to change the content.
Other for login.php , register.php, news.php page etc...
Okey I can copy this template(from index.php) to all of these pages but it is not good idea. If I want to change something static (like menu) I should do it in all pages.
I can use mysql to generate menu of course, but I want to include other pages (which is need) to index.php content div
Best Regards.
Okey I have one template (in html + js - bootstrap) which have static elements for example navbar, logo etc...
But this template have one dynamic element I mean - div = content it change depending on which site is user watching (login.php,register.php etc...)
How to include this template to all subpages for example login.php,register.php etc... without copy and paste it everywhere ?
A basic solution could be to create a layout.php page containing your whole template, and just put a where your content is supposed to be.
Then, on each page, set your $content variable with the html of your page and just include layout.php then. A better solution is to implement the MVC design pattern, but it's more "complicated".
From personal experience, I find that it's best to break down every component of the page, for example I'll have
<?php
$pageTitle = "MyAwesomeWebpage";
require_once('header.php'); //Contains Doctype, HTML header tags
require_once('navbar.php'); //Contains navigation
?>
// Unique content here
<?php require_once('footer.php'); ?>
header.php example:
<!DOCTYPE html>
<html lang="en"><!-- ended in footer.php -->
<meta content= .... />
<title><?php echo $pageTitle; ?></title>
<link rel="stylesheet" href="mystyle.css" />
<!-- .... Other links and styles here -->
<body> <!-- ended in footer.php -->
I'm new to php coding, web development, and search optimization - so newbie overall. In the process of learning php and web development I've been trying out different website architectures and layouts. One I'm working on uses an approach like the following:
I have one index.php page which always loads a header.php, sidebar.php, and footer.php. The index.php also contains a switch so that depending on the page variable the index.php is passed it loads different core content. So for example a examplesite.com/index.php?page=photos and examplesite.com/index.php?page=stories would both have the same header, footer, and side bar but one would have photos and one would have stories as the main content.
<?php $page = $_GET['page'];?>
<?php include("header.php"); ?>
<?php include("nav.php"); ?>
<?php
switch ($page)
{
case 'play':
include("photos.php");
break;
case 'cards':
include("stories.php");
break;
default:
include("frontpage.php");
}
?>
<?php include("footer.php"); ?>
My navigation is made up of href="index.php?page=..." links so choosing a menu button on the index page essentially calls itself passing it the new core to load.
I have no idea if this is a totally unorthodox approach but it all started because I was initially going to create a wordpress theme but then halfway through decided not to do it in wordpress.
What i'm concerned about are what drawbacks could be associated with this approach when it comes to search engines, indexing, seo etc.
What are other drawbacks or issues I should be thinking about that maybe I'm not?
Thanks in Advance!
I have no idea if this is a totally unorthodox approach
There is nothing essentially "unorthodox" in using a query string to load various pages. Billions of sites using this approach. Search engines can index such pages all right.
Nevertheless,
I have one index.php page which always loads a header.php, sidebar.php, and footer.php.
This is wrong concept.
Having an index.php file only to load header and footer makes no sense and makes your site plainly unusable.
Here are main faults in your design:
You're assuming that header.php would be called with the every page call. That's wrong.
You're assuming that header.php will always be static. That's wrong.
You forgot to create a template for the page itself.
The main rule everyone have to learn by heart:
Not a single character has to be sent into browser, until all data gets ready.
Why?
it's 2012 today. AJAX era. What if your code will have to send JSONed data instead of whole HTML page?
there is a thing called HTTP header. Sometimes we have to send them. And it's gets impossible if you already have your ornate HTML header sent.
Separating display logic from the business logic will let you use the same php code on many sites. You will have to change only templates and don't touch engine files. That's really great benefit.
Imagine you're going to make a custom <title> tag for your pages, based on the page content. Isn't it extremely common thing? But you can't make it without using templates.
So, you have to have one common site template containing header and footer and also dedicated templates for the every php script.
And these templates have to be called only when all business logic is done - i.e. you have got all your data ready.
An example layout is going to be like this:
.1. page itself.
it outputs nothing but only gathers required data and then calls a template:
<?
//include our settings, connect to database etc.
include dirname($_SERVER['DOCUMENT_ROOT']).'/cfg/settings.php';
//getting required data
$DATA=dbgetarr("SELECT * FROM links");
$pagetitle = "Links to friend sites";
//etc
//and then call a template:
$tpl = "links.tpl.php";
include "template.php";
?>
.2. template.php which is your main site template,
consists of your header and footer:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>My site. <?=$pagetitle?></title>
</head>
<body>
<div id="page">
<? include $tpl ?>
</div>
</body>
</html>
.3. and finally links.tpl.php is the actual page template:
<h2><?=$pagetitle?></h2>
<ul>
<? foreach($DATA as $row): ?>
<li><?=$row['name']?></li>
<? endforeach ?>
<ul>
this way you'd need no index with includes at all
Something basic that i don't understand:
I have header.php with navigation bar for my site. Inside it, there's a <head>...</head> section.
Now, in each other page of my site, I'm using require_once 'header.php' so that each page will show the navigation bar. But, I need also specific <head>...</head> sections to the different page.
For example, in page customers.php, I'm using <script>...</script> to include the jQuery library. I don't need to include it in other pages.
Now, searching the web I see that multiple head tags is wrong syntax.
So, how can anyone:
avoid multiple "head" tags
WHILE
separating his work to different PHP files and including them ?
You have to change your page structure and employ templates.
Instead of loading header at the top of the code, you have to do it at the bottom!
And page code should output not a word, but collect all data in variables.
And only after that output can be started by calling template.
A example layout is going to be like this:
First. page itself.
it outputs nothing but only gather required data and calls a template:
<?
//include our settings, connect to database etc.
include dirname($_SERVER['DOCUMENT_ROOT']).'/cfg/settings.php';
//getting required data
$DATA=dbgetarr("SELECT * FROM links");
$pagetitle = "Links to friend sites";
//etc
//and then call a template:
$tpl = "links.php";
include "template.php";
?>
Next, template.php which is your main site template, consists of your header and footer:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>My site. <?=$pagetitle?></title>
</head>
<body>
<div id="page">
<? include $tpl ?>
</div>
</body>
</html>
And, finally, links.php is the actual page template:
<h2><?=$pagetitle?></h2>
<ul>
<? foreach($DATA as $row): ?>
<li><?=$row['name']?></li>
<? endforeach ?>
<ul>
easy, clean and maintainable.
there are many advantages in such approach:
as requested, you can populate header with actual page-relevant data.
HTTP headers can be sent as well, before any output. It includes cookies, sessions, cache-control and many more.
it's 2011 today. AJAX era. You may wish change your code to return JSONed data instead of whole HTML page. It's going to be easy using such layout.
Imagine you're going to create very similar site with just different design. You will have to change only templates and don't touch engine files. That's really great advantage of using templates.
Here are some simple ways you can look at.
You can have jQuery on the pages
that don't need it; once it's
downloaded it will be cached so it
still wont use more bandwidth.
You can move out the closing </head>
tag from header.php and close the
<head> tag in the page that's including
header.php.
You can include javascript anywhere
on a page, not only in the header.
You can also do something like this.
Before you do require_once 'header.php'; you put a variable called $jquery = true;
In your header.php file you check if $jquery is set to true, if it is, you include jQuery.
in header.php
you can type like this
<head>
<?php echo $script; ?>
</head>
then in your customers.php
you can first assign the variable
$script = '<script>...</script>'
then
require_once 'header.php'
One possible solution.
You create a global variable before including header.php.
You test this variable in header.php.
If it is true, You print script or something. Something like this:
<!-- Fragment of header.php -->
<?php if ($i_want_jquery): ?>
<script ...>
...
</script>
<?php endif; ?>
On the other hand, a template may be a better solution.
ok, the title did not make much sense but this is what i am planning to do. I have designed a template for my website, with head body and div for specific stuff and everything. The website consists of header file, footer file, right-side column, header dropdown menu and a main body which would be present beneath the header dropdown menu, to the left of the right-side column, and above the footer. Right now there is some content is this main body area. What i am trying to achieve is that whenever any link is clicked on any of the other parts of the webpage, i want that content to be displayed in this main body. Right now i am copying this template to each and every page, but I want to keep this standard template as index.php and then replace main body content based on the link clicked. This is a php based website. Are there any examples where i can see how this can be achieved? or is there any standard procedure to do this. Please guide me, Thanks.
Here's a very simple way to do this:
index.php
<?php
function putPage($page) {
// put a list of allowed pages here
$allowed = array('page1', 'page2');
$page = trim($page);
$page = (in_array($page, $allowed)) ? $page : 'home';
echo #file_get_contents('.\html\\' . $page . '.html');
}
?>
<html>
<head>
<title>Title</title>
<!-- put stylesheets, js files, etc. here -->
</head>
<body>
<!-- you can have a nav bar or something here -->
<div class="navbar">
Page 1 Page 2
</div>
<?php putPage($_GET['page']); ?>
<!-- put a footer here -->
</body>
</html>
Then just put .html pages with the contents in an html subfolder.
The script will fetch them and insert them in the body.
There are a few ways you can achieve this. Off hand the two obvious ones I would say are:
Ajax to obtain content with event handlers attached to links/buttons/menus that produce maincontent specific to the request.
This requires server and client side scripting to achieve.
w3 ajax
Or alternatively use mod_rewrite with apache to determine what content to load in index.php page. For example with mod rewrite you may have a link http://www.site.com/subject/content/item# as a link structure. This could translate to www.site.com/index.php?subject=&content=&id= And these GET values would allow you to determine what to display in main content area.
This requires server side scripting and configuration of apache or (any web server with similar functionality to mod_rewrite).
mod_rewrite - apache
I use this:
<?php
$pag = array(1 => 'Home.php', 3 => '2.php');
echo require $pag[(int)#$_GET['p'] | 1];
?>
This is called either a Template View as far as you build your link specific HTML completely in PHP. You create a page layout template containing some wildcards. You load the template into a string and use string replacements or XML functions (more fancy but only suggestive if transformation is more complex).
Otherwise it is called Two Step View where you create the page layout template (as above) and a specific template for the links. Now first load the link specific template, put your dynamic content into (same techniques as above), load the page layout template and put the previous transformed specific template into.