Page 1 of 1
Flatfile page template with $variables?
Posted: Wed Apr 02, 2008 2:17 pm
by JAB Creations
I'd like to create a flat file page template that I will be able to edit via a browser. What I think is a simple concept would be input text elements for $title, $meta_description, $meta_keywords, with a textarea for the $page variable.
At this moment my main interest is figuring out if I can have PHP target $variables on a page and write form content to the specified variable, if so I'm open to examples and suggestions please.
Code: Select all
<?php
$title = 'My Web Page';
$meta_description = 'This is my handy dandy web page, great is it not?';
$meta_words = 'cheese, girls, Mario, candy, explosions ';
$page = <<<EOD
<body>
//stuff here
</body>
EOD;
?>
Re: Flatfile page template with $variables?
Posted: Wed Apr 02, 2008 3:20 pm
by s.dot
Tis really easy till you get into the complicated stuff.
Just define a custom template tag in your template, such as {$var} or ##VAR## or whatever.
Then..
Code: Select all
$page = str_replace('{$title}', $title);
Re: Flatfile page template with $variables?
Posted: Wed Apr 02, 2008 10:43 pm
by JAB Creations
Well I've set up as much as I can think of for the moment.
What I
still need to do is figure out how to target the variables in the template file...I'm not sure I follow scottayy right now so before I start toying with things beyond me I thought I'd post what wasn't yet beyond me.
Here are the two files, I have written the editor to also edit files that request this if they send a referrer. Of course in any live scenario this would need to be encased within an authentication script.
index.tpl
Code: Select all
<?php
// $_POST info should be assigned as the values to the respective variables below ($_POST['title'] goes to $title).
$title = '';
$meta_description = '';
$meta_keywords = '';
$page = <<<EOD
EOD;
?>
index.php
Code: Select all
<?php
// User clicks anchor 'edit this page', sends referer...
if (isset($_SERVER['HTTP_REFERER'])) {$edit_page = basename($_SERVER['HTTP_REFERER']);}
else {$edit_page = basename($_SERVER['PHP_SELF']);}
// Non-Structural content is saved to TPL extension equivelent...
// Content = $title, $meta, $page (between open/close body element)
// Structure = everything else in and around.
$template = str_replace('.php','.tpl',$edit_page);
global $template;
// If POST then let's write to that template! <!-- s:) --><img src=\"{SMILIES_PATH}/icon_smile.gif\" alt=\":)\" title=\"Smile\" /><!-- s:) -->
if ($_SERVER['REQUEST_METHOD'] == "POST") {
$title = $_POST['title'];
$meta_description = $_POST['meta_description'];
$meta_keywords = $_POST['meta_keywords'];
$page = $_POST['page'];
$somecontent = "Add this to the file\n";
// Let's make sure the file exists and is writable first.
if (is_writable($template)) {
// In our example we're opening $template in append mode.
// The file pointer is at the bottom of the file hence
// that's where $somecontent will go when we fwrite() it.
if (!$handle = fopen($template, 'a')) {
echo "Cannot open file ($template)";
exit;
}
// Write $somecontent to our opened file.
if (fwrite($handle, $somecontent) === FALSE) {
echo "Cannot write to file ($template)";
exit;
}
echo "Success, wrote ($somecontent) to file ($template)";
fclose($handle);
} else {
echo "The file $template is not writable";
}
} // End if request method = post
function get_title() {
// Temp echo...
echo 'we need to import the title from the variable on the flatfile';
}
function get_meta_description() {
// Temp echo...
echo 'we need to import the description from the variable on the flatfile';
}
function get_meta_keywords() {
// Temp echo...
echo 'we need to import the keywords from the variable on the flatfile';
}
echo '<?xml version="1.0" encoding="UTF-8"?>'."\n";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title><?php get_title();?></title>
<meta name="Description" content="<?php get_meta_description();?>" />
<meta name="keywords" content="<?php get_meta_keywords();?>" />
<style type="text/css">
form fieldset {
border: #99c solid 1px;
}
h1 {
font-size: 18px;
}
input.button {
background-color: #fff;
border: #000 solid 1px;
clear: left;
cursor: pointer;
display: block;
float: left;
height: 18px;
margin: 1px;
width: 65%;
}
input.button:hover, input.button:focus, input.text:hover, input.text:focus {
background-color: #99c;
}
input.text {
border: #000 solid 1px;
display: block;
float: left;
height: 18px;
margin: 1px;
width: 50%;
}
label {
border: #000 dotted 1px;
clear: left;
display: block;
float: left;
height: 18px;
margin: 1px;
padding-left: 2px;
width: 15%;
}
textarea {
border: #000 solid 1px;
clear: left;
display: block;
float: left;
padding: 4px;
width: 65%;
}
textarea:hover, textarea:focus {
background-color: #99c;
}
</style>
</head>
<body>
<h1>Page Editor</h1>
<div>
<p>The file to be edited below is <b><?php echo $edit_page;?></b> which has it's content saved to the template file <b><?php echo $template;?></b>.</p>
<p>This page was requested via a <i><?php echo $_SERVER['REQUEST_METHOD'];?></i> request.</p>
</div>
<form action="<?php echo basename($_SERVER['PHP_SELF']);?>" method="post">
<fieldset>
<label for="title">Title</label><input class="text" id="title" name="title" tabindex="1" type="text" value="" />
<br />
<label for="meta_description">Meta Description</label><input class="text" id="meta_description" name="meta_description" tabindex="2" type="text" value="" />
<br />
<label for="meta_keywords">Meta Keywords</label><input class="text" id="meta_keywords" name="meta_keywords" tabindex="3" type="text" value="" />
<br />
<textarea id="page" name="page"></textarea>
<br />
<input class="button" type="submit" />
</fieldset>
</form>
</body>
</html>