diff options
author | Adam Spragg <adam@spra.gg> | 2018-06-19 16:35:15 +0100 |
---|---|---|
committer | Adam Spragg <adam@spra.gg> | 2018-06-19 16:35:15 +0100 |
commit | 2f5472519016b3092dea07d6f041b1cb74c3cd13 (patch) | |
tree | 404cf9eef936a67e91b5dc1a6a066fd5f336d366 /html/@include.php | |
parent | 65c5c4043fe05ed4d94f3530c405363d3aee541c (diff) |
Output the page head/header/footer with PHP
Allows putting all the repeated stuff in one place (DRY!), so that if it
needs changing it only needs to change once.
Start the name of the included file with "@", and disallow the retrieval
of files beginning with "@" (returning 404) so that it can't be
downloaded, because it won't output anything meaningful.
Why "@"? Because it's about the only ASCII punctuation character that
doesn't mean anything special to most shells (bash), or programs, and is
therefore easy to use as a filename on the command line. I almost went
with "+", but it turns out that nvim treats arguments beginning with "+"
as options. So "@" it is.
Diffstat (limited to 'html/@include.php')
-rw-r--r-- | html/@include.php | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/html/@include.php b/html/@include.php new file mode 100644 index 0000000..2cf50d3 --- /dev/null +++ b/html/@include.php @@ -0,0 +1,59 @@ +<?php + +/// Indent all the lines in a string with a given number of tabs +function indent($s, $n, $indent_first_line = false) { + $s = preg_replace("/^/m", str_repeat("\t", $n), $s); + + if (!$indent_first_line) { + $s = substr($s, $n); + } + + return $s; +} + +/// Get the standard <head> elements for the site +function site_head($title) { + return "<title>{$title}</title>\n" + . "<link rel=\"stylesheet\" href=\"/style/main.css\" />\n" + . "<!--\n" + . " Hi there. Nice to see you here. Feel free to have a look around. Also check out the /about\n" + . " page while you're at it. It's got a little bit more info about the site and the resources\n" + . " used.\n" + . "-->\n"; +} + +/// Get a single top-level navigation item for the site +function site_navitem($url, $text, $hide = false) { + $selected = $url == $_SERVER['REQUEST_URI']; + + $hbeg = $hide && !$selected ? '!--' : ''; + $hend = $hide && !$selected ? '--' : ''; + $sel = $selected ? ' class="selected"' : ''; + + return "<{$hbeg}li{$sel}><a href=\"{$url}\">{$text}</a></li{$hend}>"; +} + +/// Get the standard page header elements for the site +function site_header($title) { + return "<h1 class=\"test\">{$title}</h1>\n" + . "<nav>\n" + . "\t<ul>\n" + . "\t\t" . site_navitem('/', 'Home') . "\n" + . "\t\t" . site_navitem('/projects', 'Projects') . "\n" + . "\t\t" . site_navitem('/blog', 'Blog') . "\n" + . "\t\t" . site_navitem('/privacy', 'Privacy') . "\n" + . "\t\t" . site_navitem('/contact', 'Contact') . "\n" + . "\t\t" . site_navitem('/about', 'About', true) . "\n" + . "\t</ul>\n" + . "</nav>\n"; +} + +/// Get the standard page footer elements for the site +function site_footer() { + return "<hr />\n" + . "<p>Spragg Software Services Ltd is registered in England, No. 11248242.\n" + . " Registered office: 82 Upper Hanover Street, Sheffield, S3 7RQ.\n" + . " VAT reg No. 295343283.</p>\n"; +} + +?> |