Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML Templates -- php?

So, I'm making a site about WWI as a school assignment, and I want this to appear in every document:

<!DOCTYPE html>

<html>
<head>
    <title>1914</title>
    <script src="modernizr-1.5.js"></script>
    <link href="styles.css" type="text/css" rel="stylesheet" />
    <meta charset="utf-8" />

</head>
<body>
  <div id="container">
    <header>
    <img src="images/banner.png" alt="World War I" style="border: none"/>
    <nav>
        <ul>
        <a href="index.htm"><li><span>Home</span></li></a>
        <a href="1914.htm"><li><span>1914</span></li></a>
        <a href="1915.htm"><li><span>1915</span></li></a>
        <a href="1916.htm"><li><span>1916</span></li></a>
        <a href="1917.htm"><li><span>1917</span></li></a>
        <a href="1918.htm"><li><span>1918</span></li></a>
        </ul>
    </nav>
    </header>
    <section>
    <article>
    <br style="clear: both" />
    </article>
    <aside>
    </aside>
    </section>
    <footer style="font-weight: bold; letter-spacing: .1em">
    <a href="citations.htm">Citations</a> &bull;
    <a href="about.htm">About</a>
    </footer>
  </div>
</body>
</html>

I think it's kind of stupid to copy-paste all this into each document, and painstakingly go in and change each page separately if I just want to change one word or tag. Is there a way I can put this in template.htm (or something similar) and have php or javascript code take this and insert everything from the requested file inside of the <article> tag? I don't know a lot of php, so this is probably a piece of cake for you gurus, but I would appreciate the help.

like image 796
Code Monkey Avatar asked Oct 25 '12 15:10

Code Monkey


People also ask

What are templates in PHP?

The template. php file contains your sub-theme's functions to manipulate Drupal's default markup. It is one of the most useful files when creating or modifying Drupal themes.

What are HTML templates?

The <template> tag is used as a container to hold some HTML content hidden from the user when the page loads. The content inside <template> can be rendered later with a JavaScript. You can use the <template> tag if you have some HTML code you want to use over and over again, but not until you ask for it.

Is PHP a template language?

PHP was originally designed as a templating language, but adopted more features over time and eventually became a full programming language in its own right.

Which of the following is a PHP based template?

Smarty is a template engine for PHP, facilitating the separation of presentation (HTML/CSS) from application logic.


2 Answers

Using php includes:

Save this as top.php

<html>
<head>
<title><?php echo $title; ?></title>
<script src="modernizr-1.5.js"></script>
<link href="styles.css" type="text/css" rel="stylesheet" />
<meta charset="utf-8" />

</head>
<body>
<div id="container">
<header>
<img src="images/banner.png" alt="World War I" style="border: none"/>
<nav>
    <ul>
    <a href="index.htm"><li><span>Home</span></li></a>
    <a href="1914.htm"><li><span>1914</span></li></a>
    <a href="1915.htm"><li><span>1915</span></li></a>
    <a href="1916.htm"><li><span>1916</span></li></a>
    <a href="1917.htm"><li><span>1917</span></li></a>
    <a href="1918.htm"><li><span>1918</span></li></a>
    </ul>
</nav>
</header>
<section>

Save this as bottom.php

<aside>
</aside>
</section>
<footer style="font-weight: bold; letter-spacing: .1em">
<a href="citations.htm">Citations</a> &bull;
<a href="about.htm">About</a>
</footer>
</div>
</body>
</html>

Then your individual pages would be like this:

<?php $title = '1914'; include("top.php");?>
//This would be where you would make the changes that need to be made on each page.

<article>
<br style="clear: both" />
</article>

<?php include("bottom.php");?>
like image 155
Rick Calder Avatar answered Sep 29 '22 10:09

Rick Calder


You could consider one of these popular template engines :

  • Smarty (becoming outdated)

  • Latte (used mostly by the Nette community)

  • Twig (used mostly by the Symfony community)

  • Mustache (official implementations of this templating engine exist in more than two dozen proramming/scripting languages)

I tend to favor Mustache, mostly because it has an official JS version, an official Ruby version, an official Java version, etc. It allows you to use the same templates frontend and backend, which is very useful for widgets that are first rendered in background and rerendered in foreground at updates.

like image 33
John Slegers Avatar answered Sep 29 '22 08:09

John Slegers