Using better includes for pages? - php

As of right now, the way I use includes is to bring the header, footer, and some content for other pages.
This leads to more includes then I really want, because I need to add more content for the includes.
For example:
<!DOCTYPE html>
<?php include('header.php'); ?>
<body>
<?php include('body-top.php');
custom html
</?php include('footer.php');
</body>
It would be nice if I could add variables to the includes and on the pages I want the includes to show.
I am not good at PHP at all, so is there a better way to use Includes?

This can be easily done:
index.php
$title = 'Hello World!';
include 'content.php';
content.php
<!DOCTYPE html>
<html>
<head>
<title><?php echo $title; ?></title>
</head>
<body></body>
</html>
The problem with this approach is, you'll soon run into problems keeping track what went where, so using functions as suggested in other answers might be a good idea. However, for small projects it's IMHO good enough.

sounds like a job for Smarty
It looks like this
<?php
require 'Smarty/libs/Smarty.class.php';
$smarty = new Smarty;
$smarty->assign('title','Hello World');
$smarty->assign('hello','Hello World, this is my first Smarty!');
$smarty->display('test.tpl');
?>
test.tpl
<html>
<head>
<title>{$title}</title>
</head>
<body>
{$hello}
</body>
</html>
Or even better way, use some of the PHP MVC frameworks, which will give you even more stuff (not just template system)

Your includes are already very few, no need to optimize them.
Also don't pay attention to people suggesting Smarty or MVC's because that will increase dramatically the number of includes (in exchange for other benefits, of course)-

You can turn your included files into functions. PHP has a neat trick where anything between curly-brackets (i.e. { and }) is only executed when that part of the code is reached. This includes the HTML code outside of your PHP tags.
This could be our 'header.php' file, where we wrap our current code in a function.
<?php function doHeader($title) { ?>
<html>
<head>
<title><?php echo $title; ?></title>
</head>
<?php } ?>
Then we make a tester for it. Whatever our tester/caller chooses to pass as $title shows up in our output.
<?php
// All included here
include_once('header.php');
?><!DOCTYPE html>
<?php doHeader('My page title'); ?>
<body></body>
</html>
This produces the output,
<!DOCTYPE html>
<html>
<head>
<title>My page title</title>
</head>
<body></body>
</html>

Related

Is it good practice to send page name through sessions in php 7.1?

Hello, I started learning PHP, I set myself a goal to make a website,
which would be similar to social network, as I am newbie, I don't know what is good or bad, I can code simple functions, have fundamentals of HTML, CSS, JS. But I want to learn more deeply, like what practices should I use and which I shouldn't
Here is the code of header.php :
<?php
$page = $_SESSION['page'];
?>
<!DOCTYPE html>
<html>
<head>
<title><?php echo $page ?></title>
</head>
<body>
<h3>HEADER</h3>
</body>
</html>
<hr>
And here is the code of index.php :
<?php
session_start();
$_SESSION['page'] = "Pradinis Puslapis";
require('tpl/header.php');
echo "Hello World!";
?>
<hr>
Of course, I would include footer too, and also in index.php I would implement html code specially designed for that page, but like I wrote, I want to know If I am thinking good - to send page's name through
$_SESSION variable.
You don't need a session in your case. When you include your header you can use your variables directly in your included script. When you have a template engine you take another way. In that case you load a template file and replace your variables and return the parsed HTML code to your PHP file and print them out.
main.php
$page = "Test";
require('tpl/header.php');
header.php
<!DOCTYPE html>
<html>
<head>
<title><?php echo $page ?></title>
</head>
<body>
<h3>HEADER</h3>
</body>
</html>
<hr>
What you try here is good for learning but not really common in production. There are a ton of really good Frameworks that gives you a perfect structure and help you to prevent mistakes. For your Programm you could start with a template engine like Twig or Dwoo for example. So first you should learn the basics then you can build a website on your own or good tools. Building a good framework is really hard.

Is there a standards-compliant way to start a PHP session and echo a JavaScript script in one include() statement?

I have two scripts that I call with two PHP include() calls. The first starts a session / sets cookies, the second loads one of two JavaScript scripts. To keep things valid, I've been using the two calls but I'd like to just combine them into one.
Current setup (simplified):
<? include "session.php" ?>
<!DOCTYPE HTML>
<html>
<head>
<? include "scripts.php" ?>
...
What I'd like:
<? include "session_and_scripts.php" ?>
<!DOCTYPE HTML>
<html>
<head>
...
But it's invalid markup. Now if it really doesn't matter, I'd like to do it this way. If there are serious repercussions, then I'm thinking of just echoing a DOCTYPE in the included PHP file, which I'd rather not do.
So which is better: echo the DOCTYPE, use include() twice, or use include() once and have invalid markup?
EDIT - The whole script (session and javascript) should ideally be fully implementable with one line of code (e.g. the one include())
Use ob_start at first to avoid problems with session_start
<?php ob_start();?>
<!DOCTYPE HTML>
<html>
<head>
<?php include "session_and_scripts.php"; ?>
A way that uses only 1 1file and no additional instructions:
<?php include "session_and_scripts.php" ?>
<!-- more head-stuff-->
</head>
<body>
<!--more content-->
session_and_scripts.php should do the following:
<?php
//do the session stuff
?>
<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript">
//some javascript
</script>
(But I would'nt say it's a good approach)
But it's invalid markup. Now if it really doesn't matter, I'd like to do it this way. If there are serious repercussions, then I'm thinking of just echoing a DOCTYPE in the included PHP file, which I'd rather not do.
Assuming that you do not want to have a valid markup, there is no problem, the only restriction is that session_start is called before any kind of "echo"...
Assuming you want a valid markup using only one include and without echoing the DOCTYPE from the included file, you can save the script text into a php variable and echo it in the main page after the inclusion
//main page
<? include "session_and_scripts.php" ?>
<!DOCTYPE HTML>
<html>
<head>
<?php echo $script;?>
// session_and_scripts.php
<?php
session_start();
$script = '<blablabla>';

How to change HTML title in PHP without breaking the XHTML markup validation?

Here is the structure of the web site:
PHP index file
//my class for analyzing the PHP query
$parameter = new LoadParameters();
//what this does is it accesses the database
//and according to the query, figures out what should be
//loaded on the page
//some of the things it sets are:
// $parameter->design - PHP file which contains the design
// HTML code of the page
// $parameter->content - Different PHP file which should be loaded
// inside the design file
$parameter->mysqlGetInfo($_SERVER['QUERY_STRING']);
//load the design file
include($parameter->design);
PHP design file
Just the generic structure. Obviously it has a lot more design elements.
<html>
<head>
...
</head>
<body>
<?php
//this loads the content into the design page
include($parameter->content);
?>
</body>
</html>
Question
So here is the problem I experience. The $parameter->content file is a dynamic PHP file, meaning the content also changes according to the query.
For instance if I have a image pages with queries like ?img=1 and ?img=2, my LoadParameter class will only look at the img part of the query and will know that the content of the page should be image.php. image.php however will look at the query again and figure out exactly what image to load.
This causes issues for me because I want to have a different <title></title> for different images. So my solution was just to set the <title></title> element in the content page. This works but it breaks the XHTML markup validation at W3C because it makes the structure of the site to be the following:
<html>
<head>
...
</head>
<body>
...
<title>sometitle</title>
...
</body>
</html>
And having <title></title> within <body></body> is not allowed.
So how can I change the title without breaking the XHTML markup validation?
Note: I can't use javascript because then Search engines would not be able to see the title of the page. I need to do it directly in PHP.
Thanx in advance.
why not do a second include to perform the title in the proper place?
<html>
<head>
<?php
inlcude($parameter->title);
?>
...
</head>
<body>
<?php
//this loads the content into the design page
include($parameter->content);
?>
</body>
</html>
Can't you just change the PHP code so that you can do something like:
<html>
<head>
<title><? print($parameter->title); ?></title>
</head>
<body>
<?php
//this loads the content into the design page
include($parameter->content);
?>
</body>
</html>
I'd move all of the <head> code into a 'common function' called something like html_head($title) and then have it put the title where it belongs.
Then simply call that function from within the pages and it's fixed.
Don't forget to include the <body> tag in that function, otherwise it won't work!
Elaborating ;)
function html_head($title) {?>
<html>
<head>
<title><?=$title?></title>
<!-- Put whatever you want... here! -->
</head>
<body>
<?}
Then in $parameter->content, call html_head("Title")
It would be easier if $parameter->content could be included without displaying its HTML code, but instead have a $parameter->display (or similar) function that displays the HTML code. That way, you can include the PHP code at the beginning of the file and not worry about being unable to access the title.
<?php
require_once($parameter->content);
?>
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="UTF-8" />
<title><?php echo $parameter->title; ?></title>
</head>
<body>
<?php
echo $parameter->display;
?>
</body>
</html>
This is how I solved the issue.
I changed the PHP design to something like:
//get the content PHP file
//inside the file I set the following variables
//which are used below:
//$parameter->title - the string which contains the title
//$parameter->html - the string which contains the HTML content
include($parameter->content);
//string which will contain the html code of the whole page
$html = <<<EndHere
<html>
<head>
<title>
EndHere;
//add title
$html .= $parameter->title;
$html .= <<<EndHere
</title>
</head>
<body>
EndHere;
//add the content of the page
$html .= $parameter->html;
$html .= <<<EndHere
</body>
</html>
EndHere;
//output the html
echo $html;
And here is the basic structure of the Content PHP file. Since the only page which can possibly include the file is the my design page, I can reference $parameter in it.
//set the title
$parameter->title = "sometitle";
//set the content HTML
$parameter->html = "some HTML here";
It's not a very clean solution but it works fine.

Using variables before include()ing them

I'm using a file, page.php, as an HTML container for several content files; i.e., page.php defines most of the common structure of the page, and the content files just contain the text that's unique to every page. What I would like to do is include some PHP code with each content file that defines metadata for the page such as its title, a banner graphic to use, etc. For example, a content file might look like this (simplified):
<?php $page_title="My Title"; ?>
<h1>Hello world!</h1>
The name of the file would be passed as a URL parameter to page.php, which would look like this:
<html>
<head>
<title><?php echo $page_title; ?></title>
</head>
<body>
<?php include($_GET['page']); ?>
</body>
</html>
The problem with this approach is that the variable gets defined after it is used, which of course won't work. Output buffering also doesn't seem to help.
Is there an alternate approach I can use? I'd prefer not to define the text in the content file as a PHP heredoc block, because that smashes the HTML syntax highlighting in my text editor. I also don't want to use JavaScript to rewrite page elements after the fact, because many of these pages don't otherwise use JavaScript and I'd rather not introduce it as a dependency if I don't have to.
Most people store the output of the included page into another variable. Have you tried putting all the content of the included page into the output buffer, then storing the ob_get_clean() into a variable like $page_html, then having your page look like this:
<?php include($_GET['page']); ?>
<html>
<head>
<title><?php echo $page_title; ?></title>
</head>
<body>
<?php echo $page_html; ?>
</body>
</html>
Edit: So the second page would look something like this:
<?php
$page_title="My Title";
ob_start();
?>
<h1>Hello world!</h1>
<?php $page_html=ob_get_clean(); ?>
The best thing I can think of would be to separate the inclusion of the file from the rendering. So your template looks like this:
<?php include($_GET['page']); ?>
<html>
<head>
<title><?php echo $page_title; ?></title>
</head>
<body>
<?php renderPage() ?>
</body>
</html>
And the file you are including looks like this:
<?php
$page_title="My Title";
function renderPage() {
?>
<h1>Hello world!</h1>
<?php
}
?>
This is also nice since you can pass parameters to renderPage() so that the template can pass info along to the page it is including.

Is it possible to write the contents of b.aspx on a.aspx?

Here is the scenario: I have two asp pages. a.aspx is layout and b.aspx is content. I want to display the contents of b.aspx inside a <div> on a.aspx. I know with PHP you can do it like so:
//a.php
<html>
<head>
<title>test</title>
</head>
<body>
<?PHP
include "b.php";
?>
</body>
</html>
//b.php
<?PHP
echo "Content String";
?>
//result
<html>
<head>
<title>test</title>
</head>
<body>
Content String
</body>
</html>
Thanks!
This scenario is handled by masterpages and or composing the page out of (user)controls in ASP.NET. As described at for instance here.
Probably Server.Execute will help.
//a.aspx
<html>
<head>
<title>test</title>
</head>
<body>
<% Server.Execute("b.aspx"); %>
</body>
</html>
//b.aspx
Content String
//result
<html>
<head>
<title>test</title>
</head>
<body>
Content String
</body>
</html>
By the way, I do not recommend this approach. It's just to show it can be done. Master pages and user controls are normally the way to go.
create a B.ascx that does everything you need, and then both B.aspx and A.aspx can include that control.
It sounds like MasterPages will accomplish this for you. Is this not an option for you?
you can go old-skool and use an IFRAME
alternatively, could use a WebRequest in a.aspx.cs to open b.aspx, store the results in a string, and return that string inside a div on a.aspx

Categories