Very Simple Flat File Template System

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Very Simple Flat File Template System

Post by JAB Creations »

I'd like to build a very simple flat file template system. I want to keep the code as simple as possible as I'm still a PHP noob.

The first and only method that I have been able to figure out is getting the template file's contents displayed in a textarea element...

Code: Select all

<textarea><?php include("example.tpl");?></textarea>
When submitting I'm guessing I'll need...

1.) A second file to receive and write to the template file itself.
2.) A hidden input that is pass along to tell the second file which template is to be written to.

Unless I can simply put the form action as the template file itself?

I assume I'd need to use the fwrite command. I have no interest in doing anything more fancy then necessary. This is a very simple setup and exactly what I want though I just need a little help putting it together. I can worry about adding things like security at a later time but to get the fundamental functions working and to ease the pain of learning PHP is my only goal in this thread. Thanks!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

You have the makings of a very dangerous templating system. I would suggest using an existing templating engine if this is for any production (or publicly available) system.

If this is for your own private use and learning, I would still recommend looking at existing template engines. The very basic level of it is as you've already thought out.
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Post by JAB Creations »

I ALREADY mentioned I know about the security issues. I ALREADY mentioned this is to help me learn PHP on my terms. What more do I have to say in my initial posts to simply have people help me without the hassle? I appreciate the warnings but I always post in every thread on this forum that I know about these issues so could we please stay on the topic?
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Post by JAB Creations »

Everything is modular...so it is the simple concept of figuring out how all the pieces fit together. Here is a piece that looks like I need it but I'm not sure how to fit it in to my puzzle. I'm testing this locally plus I promise I'll add security to this script (in addition to the several layers of security I already have in place) after I figure out how to get this to work.

Code: Select all

$f = fopen ('example.tpl',"w");
fwrite($f, $_POST['textarea']);
fclose($f);
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Post by JAB Creations »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Ok so I found another script that seems to work...

[b]Original Script[/b]

Code: Select all

<?
if ($_POST) {
// submit changes
$file = fopen($uploaddir . 'efb.txt', 'w', 1);
fwrite($file, $_POST['textarea']);
fclose($file);
?>
<b><font color="#FF0000">The changes have been saved and will show as follows.</font></b> 
<textarea name="textarea2" cols="91" rows="50"><? include("efb.txt");?></textarea>
<?
} else {
// display text and wait for changes
?>
<form name="efb" method="post" action="?ac=efb">
<textarea name="textarea" cols="91" rows="50"><? include("efb.txt");?></textarea>
<br><input type="submit" name="Submit" value="Submit"></form>
<?
}
?>
So I modified the above script to work with whatever page refers to that page. I'm not sure what I did that screwed up the script from working. I get this error after only making the modifications between the two scripts...

Modified Script Errors
failed to open stream: HTTP wrapper does not support writeable connections. in edit.php on line 6
Warning: fwrite(): supplied argument is not a valid stream resource on line 7
Warning: fclose(): supplied argument is not a valid stream resource on line 8
Modified Script
edit.php

Code: Select all

<?php
$template = $_SERVER['HTTP_REFERER'];

if ($_POST) {
// submit changes
$file = fopen($uploaddir . $template, 'w', 1);
fwrite($file, $_POST['textarea']);
fclose($file);
?>
<span style="#f00">The changes have been saved and will show as follows.</span>
<form>
<textarea name="textarea2" cols="64" rows="16"><?php readfile($_SERVER['HTTP_REFERER']);?></textarea>
</form>

<?
} else {
// display text and wait for changes
?>
<form name="efb" method="post" action="?ac=efb">
<textarea name="textarea" cols="64" rows="16"><?php readfile($_SERVER['HTTP_REFERER']);?></textarea>
<br><input type="submit" name="Submit" value="Submit"></form>
<?
}
?>
Pages to create refers...

test1.html

Code: Select all

<h1>test page 1</h1>
<span>Page 1</span>
<br />
<a href="test2.html">Page 2</a>
<br />
<a href="edit2.php">edit this page</a>
test2.html

Code: Select all

<h1>test page 2</h1>
<a href="test1.html">Page 1</a>
<br />
<span>Page 2</span>
<br />
<a href="edit2.php">edit this page</a>

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Post Reply