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.
Related
I have a webpage with several subdirectories for example /search or /friends. Each of this subpages has its own javascript and css files. Now I want all this pages to have the same topbar so if I wanted to change the topbar I would only have to do this in one single place.
What's the common way of doing this? Simple php drops out because of the several scripts and css files. My idea was to call a php script via ajax on each subpage and append the returning string to the body element with jquery's append method but this doesn't seem very clean to me.
How does facebook handle this? Facebook's topbar doesn't even blink when clicking an internal link.
Thanks.
What about using an header.php in all the pages where you want to show your top bar?
To do this just create a file with top bar and save it as header.php and then in your index.php just place include('header.php'); repeat second step for each page where you want to have your top bar.
header.php
// top bar stuff
echo '<ul><li>Link</li><li>Link</li></ul>'; //etc
Other Pages
<?php
include 'header.php';
?>
I need one advice from you. I am working on a website, which uses PHP and HTML. As the biggest part of the header and footer code will be same for many pages, I am thinking of using PHP's include to avoid code duplication. But, each of those pages requires different stylesheets and JS files included. What do you think how could I let the other file know what scripts and stylesheet to import?
Our company does this:
The header reads the filename of the page calling it when it's included.
Then, it changes the extension to '.js' and outputs that if it exists. Same for CSS.
So if I have a page "register.php", it will auto-include "register.js" and "register.css" if they exist.
Here's what I do:
<?php include("includes/headContent.php"); ?>
<title>Page title goes here!</title>
<script src="script_only_used_on_this_page"></script>
<?php
require_once("includes/siteHeader.php");
?>
Site Content Goes Here!!
<?php
require_once("includes/siteFooter.php");
?>
Head Content includes any PHP I want included in every page, as well as the opening html and head tag, and any Javascript libraries and css stylesheets I want on every page. Site header closes the /head tag, and opens the body as well as printing out my site header and some other markup that goes on every page. Finally Site Footer closes out my template. Everything in between is my content area!
There are lots of different ways you can do templating, if you wanted to create a simple include and an echoHeader() and an echoFooter() function... just have the echoHeader function accept a parameter which you would pass your javascript and CSS lines to.
you can use MVC coding pattern
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
We need to display the content of one single TYPO3 page in Habari.
It would suffice to retrieve the HTML, as styling (CSS) is done separatly.
However, we only want the HTML of the content elements - not the whole, fully rendered page.
How could we achieve that?
Does TYPO3 (or one of its plugins) provide a facility for that?
This can be done via a custom Typoscript template-record in the Typo3 backend that just outputs the content without any further HTML and or tags.
Putting something like this in the 'setup':
page = PAGE
page.config.disableAllHeaderCode = 1
page.10 < styles.content.get
Then make sure in the template-record it say's that it's a root-template, and that it clears constants and setup before this template. And put this record on the top most page (aka root).
Also make sure that you included the static template of CSS Styled Content. This can be done when editing the template-record inside Typo3.
You could do this in Habari using something like this:
$url = "http://your-typo3-url/";
$output = RemoteRequest::get_contents( $url );
$output will then be the HTML contents of the page. You can then use a combination of strpos() and substr() to pull the relevant HTML content you want, eg just the <body>
You can do this in one of your theme template files, the theme's theme.php file itself or even within a plugin.
You can then use Habari's native caching to cache the content too so you don't have to retrieve the Typo3 page with every page view.
BTW, You could use typo3_webservice fro that. It uses XMLRPC protocol, and quite easy to implement with PHP.
http://typo3.org/extensions/repository/view/typo3_webservice/current/
I have a very dynamic (social networking) site running smarty that I want to enable caching for.
My Structure:
index.php display()s template.tpl
template.tpl include()s indexContent.tpl
Most of the content in template.tpl is static .. such as the scripts, banner, footer.. etc. How can I cache that but not specific parts which look different to depending on whose logged in (among other factors)?
I've discovered 3 methods:
{nocache} {include='indexContent.tpl'} {nocache}
{dynamic} {include ...
Set the cache_id for each page.
Unfortunately each has a problem:
Doesn't really seem to work? Dynamic content still gets cached..
Not sure how to implement or how it's different than (1)
How to identify uniquely? Some pages have the same "name" but different content for specific members... think "myProfile.php"
Any suggestions? Thanks!!
You can use reverse proxy, like Varnish to cache the static part of the page and to include your dynamic content as Server-Side Includes (for Varnishi it is ESI). Next you will need to setup the caching rules for your static and dynamic URLs so that the static one will be cached for a long time period while the dynamic one will not be cached at all.
To make it easier to understand the whole idea here is how your page HTML code could look like:
<html>
<head>...</head>
<body>
...some static layout...
<esi:include src="/esi/indexContent.php"/>
...some another static layout...
</body>
</html>
Where /esi/indexContent.php is the script that generates the dynamic content.
For Varnish: beware of the gzipped or deflated content with ESIs as it is described in the answer here
We have the same scenario. Our entire front page is cached except for a couple of dynamic elements (news, latest forum threads) and the easiest way I found to get around this is to add in a keyword to the cached template
NEWS_BLOCK
on your logic script you then load your news template and preg_replace it with the keyword.
$news_template = $smarty->fetch('news_template.smrt');
$page_body_raw = $smarty->fetch('frontpage.smrt');
$page_body = preg_replace('/NEWS_BLOCK/', $news_template, $page_body_raw);
in 3 way u can save cache file by this name:
myprofile_id for example a persone that registered and his id is 455 in user table u can save cache file for he with this name myprofile_455
after that u can include cached file in tpl file like this:
{include file="cache/myprofile`$smarty.get.userid`.html"}
I know the question is old my i am still proposing a solution to help someone else.
I seem to get into same trouble with a social networking site i am developing. Here is the solution that worked for me
Doesn't really seem to work? Dynamic content still gets cached..
Not sure how to implement or how it's different than (1)
Just remove the static part of your page like footer and header and put them in a different tpl file. Then include the tpl file as
{include file='head.html' cache_lifetime=5000}
or conversely remove the dynamic part of your page and put it in another template and include it as
{include file='head.html' nocache}
3.How to identify uniquely? Some pages have the same "name" but different content for specific members... think "myProfile.php"
for same page with different content like a profile page, you can pass profile Id as a parameter to cache call.
$my_cache_id = $_GET['profile_id'];
$smarty->display('index.tpl', $my_cache_id);
This will ensure that same page with different parameters are not treated as same page.
Hope this helps.