yield equivelant from Rails - php

I want to have a standard layout for all my pages on my php site. Is there anything in php similar to Rails's yield
Example:
<!DOCTYPE html>
<html>
<head>
<!--STYLESHEETS AND SUCH HERE-->
</head>
<body>
<!--NAVBAR HERE-->
<?php
//YIELD THINGY HERE
?>
<!--FOOTER HERE-->
</body>
</html>

Related

Require doesn't work in basic html / php code

So im trying to import a header.php into my index.php file file but it just doesnt work.
index.php:
<?php
require "header.php";
?>
<main>
<p> online! </p>
<p> offline! </p
</main>
<?php require "footer.php"; ?>
header.php:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<header>
<p> this should be on top</p>
</header>
</body>
</html>
Footer:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<p> This should be on the bottom </p>
</body>
</html>
when i open the project i will only see what is written in index without header or footer
Your code return invalid HTML, because DOCTPYE, html, head and body are defined twice.
Change header.php to:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<header>
<p> this should be on top</p>
</header>
#-- not closing body and html
Change footer.php to:
<p> This should be on the bottom </p>
</body>
</html>
Make sure the path to your header and footer is correct.
Then try including it in your page like this
require(“header.php”);
//Your html code here
require(“footer.php”);

Autocomplete styles through the include php function in PhpStorm

I'm using PhpStorm and I love it, especially the auto-complete feature. I tried to use the autocomplete while including the CSS files with the "include" function, like this:
<!doctype html>
<html lang="en">
<head>
<?php include("head.php"); ?>
</head>
<body>
<div id="page">
<div id="menu" class="heigth-100 bg-blue">
<?php include("menu.php"); ?>
</div>
<div id="content" class="heigth-100 bg-gray padding">
<h2 class="I-want-to-autocomplete">Home</h2>
</div>
</div>
</body>
</html>
Where the function include(head.php); are my css files. But once this is done, the auto-complete stops working because they are not directly linked to the document.
Is there any way that auto-complete founds the styles in the include function?

For which <html> tag was the tag </html> in the footer.php closed?

I am reading << practical php and mysql building eight dynamic web applications>>.In the appendix A--web site design ,there are two files--header.php and footer.php.
It is the header.php file.
<?php
require("config.php");
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<head>
<title><?php echo $config_sitename; ?></title>
<link href="stylesheet.css" rel="stylesheet">
</head>
<body>
<div id="header">
<h1><?php echo $config_sitename; ?></h1>
</div>
<div id="menu">
Home
•
About
•
FAQ
•
Technical Details
</div>
<div id="container">
<div id="bar">
<?php
require("bar.php");
?>
</div>
<div id="main">
It is the footer.php file.
</div>
</div>
</body>
</html>
I have two questions .
1. why the tag in header.php don't close itself,such as
<div id="main"> don't close
<div id="container"> don't close
<body> tag don't close
All of them closed in the footer.php ,if the header.php is long and complicated,it is hard to write the footer.php file to close all the tags in the header.php.
2.There is a tag </html> in the footer.php,but there is no <html> tag in the header.php,what is the matter?
I haven't read that book but typically if a page is broken up into 3 parts :
header.php
content.php
footer.php
Then you can imagine the following block of HTML code :
<!DOCTYPE html>
<html>
<body>
<div class="main">
<!--other content-->
</div>
</body>
</html>
Being the same as this :
<!--header.php-->
<!DOCTYPE html>
<html>
<body>
<div class="main">
<!--header.php end-->
<!--content.php-->
<!--other content-->
<!--content.php end-->
<!--footer.php-->
</div>
</body>
</html>
<!--footer.php end-->
So for your first question, these tags can't be closed within header.php itself because that would not allow content from content.php to be inserted within the .main container.
For your second question, the <html> tag was probably left out accidentally.
A good practise is to include a comment beside the closing tag to make things clearer. For example, your footer.php could look like this :
</div><!--#main-->
</div><!--#container-->
</body>
</html>
Doing so would prevent a lot of careless mistakes in case a tag was closed earlier than intended; or if the closing tag was missed out, you could spot it easily.

Multiple <head> and <body> tag

I am trying to create a very simple web application, basically to understand the best practices of coding in HTML5, CSS and JavaScript.
My application has 3-4 pages and each one is using same menu header. So I want to make it reusable by writing it in a separate file (either PHP or HTML).
head.php (it is to be made reusable):
<!DOCTYPE html>
<html>
<head>
<link href="../../css/headermenu.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<ul id="menu">
<li>Home<span></span></li>
</ul>
<p>
</p>
</body>
</html>
front.php:
<?php
include ("$_SERVER[DOCUMENT_ROOT]/page/common/head.php");
?>
HTML markup (dirty code):
<!DOCTYPE html>
<html>
<head>
<!DOCTYPE html>
<html>
<head>
<link href="../../css/headermenu.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<ul id="menu">
<li>Home<span></span></li>
</ul>
<p>
</p>
</body>
</html></head>
<body>
<div>
</div>
<p>
</p>
</body>
</html>
I have following questions:
head.php has both <body> and <head> tag. So where should I write these PHP lines to include it? (in <head> or <body>) (I don't want to have multiple <head>s and <body>s in the final page)
What are the other best practice I should follow? (any link to references welcome)
I have already read w3schools.
In my opinion it would be a good idea to read about templating systems or have a look how frameworks/CMS handle this.
Doing it your way, you can't completly avoid repeating e.g. the closing head tag </head> in every content.php.
So this is just an idea:
head.php
<?php
// Some other includes / requires,
// code snippets...
?>
<!DOCTYPE html>
<html>
<head>
<!-- site-wide stylesheets -->
<!-- & js-files -->
<link href="css/main.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="my/global/scripts.js"></script>
content.php
<?php
include ($_SERVER['DOCUMENT_ROOT'] . '/page/common/head.php');
?>
<!-- put page specific scripts &
<!-- styles here -->
<link href="my/pages/css/style.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="my/pages/js/scripts.js"></script>
</head>
<body>
<div id="container">
<!-- content start -->
<div id="content">
<h1>title</h1>
<p>Your content</p>
</div>
<!-- end of content div -->
<?php
include ($_SERVER['DOCUMENT_ROOT'] . '/page/common/foot.php');
foot.php
<div id="foot">
copyright etc
</div>
</div> <!-- end of container div -->
</body>
</html>
php is rendering html and if you have in both files header of course it will be printed twice
you should separate in includes but don't write in both files tag
example
<header>
<?php
// this file should not include <head> taf
include ($_SERVER[DOCUMENT_ROOT] . '/page/common/header.php);
?>
</header>
BODY
<header>
<?php
include ($_SERVER[DOCUMENT_ROOT] . '/page/common/foot.php);
?>
</header>
include will bring content from head.php and foot.php and will put in index.php file
Another possible solution to avoid multiple head tags which also makes it possible to add additional css files:
<?php
// head.php
$html = <<< EOF
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en-US">
<head>
<title>{$title}</title>
{$meta}
<link href="../../css/headermenu.css" rel="stylesheet" type="text/css"/>
{$additional_style}
</head>
<body>
<ul id="menu">
<li>Home<span></span></li>
</ul>
{$mainbody}
{$footer}
{$javascript}
</body>
</html>
EOF;
?>
<?php
// page1.php
$title = 'some title';
$meta = '';
$additional_style = '';
$mainbody = 'your body';
$footer = '';
$javascript = '';
include_once("head.php");
echo $html;
?>
<?php
// page2.php
$title = 'some other title';
$meta = '';
$additional_style = '<link href="../../css/page2.css" rel="stylesheet" type="text/css"/>';
$mainbody = 'your body';
$footer = '';
$javascript = '';
include_once("head.php");
echo $html;
?>
It also allows for multiple levels of inheritance (for example, you could define the same footer for a couple of pages). This principle can also be used without EOF, but I think that it looks nicer this way.
The main downside is that you will get a warning if you do not set all the variables of head.php in the pages including it.

problem in shorthand php tag of files in cake php

I have this simple code:
<html>
<head>
<title>My Cake Blog Application</title>
<?=$html->css('styles');?>
</head>
<body>
<div id="container">
<div id="content">
<?=$content_for_layout;?>
</div>
</div>
</body>
</html>
I saved it in app/views/layouts
name it to default.ctp
the output in browser only shows me css('styles');
also I have styles.css in app/webroot/css
it is not working in my browser then I tried this string for begin and close:<?php echo();?>
again not worked
<html>
<head>
<title>My Cake Blog Application</title>
<?php=$html->css('styles'); echo();?>
</head>
<body>
<div id="container">
<div id="content">
<?php=$content_for_layout; echo();?>
</div>
</div>
</body>
</html>
Either use full tags as you already found out or enable short tags in php.ini: How to enable PHP short tags?
<html>
<head>
<title>My Cake Blog Application</title>
<?php echo $html->css('styles'); ?>
</head>
<body>
<div id="container">
<div id="content">
<?php echo $content_for_layout; ?>
</div>
</div>
</body>
</html>

Categories