Reputation: 25
Here I am again... I've been watching some tutorials and I'm now in good way on php programing...
What I want to know is: with a template manager like for example smarty, you can make 1 php page with the code and redirect it to the view html file... something like this on the php...
$a = new Trabalhos();
$id = new Users();
$sm->assign("id", $a->select());
if (isset($_GET['del'])) {
$a->setId($_GET['del']);
$a->delete();
}
if (isset($_POST['id'])
and isset ($_POST['trolha'])
and isset($_POST['padeiro'])
and isset($_POST['arquitecto'])
and isset($_POST['engenheiro'])
and isset($_POST['medico'])
and ! isset($_GET['edit'])) {
$a->setId($_POST['id']);
$a->setTrolha($_POST['trolha']);
$a->setPadeiro($_POST['padeiro']);
$a->setArquitecto($_POST['arquitecto']);
$a->setEngenheiro($_POST['engenheiro']);
$a->setMedico($_POST['medico']);
$a->insert();
}
$lista = $a->select();
$sm->assign("lista", $lista);
$sm->display("trabalhos.html");
Then I make the view page in trabalhos.html and I put this:
<form method="post">
| id <select name ="id">
{foreach from=$id item=d}
<option value ="{$d.id}">{$d.id}</option>
{/foreach}
</select>
| Trolha <input name ="trolha" type="text" />
| Padeiro <input name ="padeiro" type="text" />
| Arquitecto <input name ="arquitecto" type="text" />
| Engenheiro <input name ="engenheiro" type="text" />
| Medico <input name ="medico" type="text" />
<input type="submit" value="Guardar"/>
</form><br>
<table border="1">
<tr>
<td>ID</td>
<td>Trolha</td>
<td>Padeiro</td>
<td>Arquitecto</td>
<td>Engenheiro</td>
<td>Medico</td>
<td>Acoes</td>
</tr>
{foreach from = $lista item = row}
<tr>
<td>{$row.id}</td>
<td>{$row.trolha}</td>
<td>{$row.padeiro}</td>
<td>{$row.arquitecto}</td>
<td>{$row.engenheiro}</td>
<td>{$row.medico}</td>
<td>Edit | <a href="?del={$row.id}">Delete</td>
</tr>
{/foreach}
</table>
OK till there I get it and I make it work with the help of the tutorial and giving my variables
Now what I really want to know is how can I do the same without the help of external template managers, everyone will say that is easier with this and that, but I'm looking to learn php, not to run pre-made scripts, if you know what I mean
Right now I don't trust on external code... to trust it I need to understand it
So if they made it work with smarty I have to make it work by myself right?
Is there need of other languages out of php to do it? like java or something similar?
Or can I do it just with php and html coding?
If is possible to do it just with php and html coding can anyone tell me for example how will I make that column with the foreach php function work? And how do I transmit to the html the variables info?
Really sorry for that huge post, but I didn't see anything like what I'm asking in here and on google i just see people and forums talking about template managers, and that's not what I'm looking for ;)
Thank you very much for the answers last post got a positive one ;)
Upvotes: 1
Views: 434
Reputation: 5193
Trying to write your own template engine is a hard task. But you can do it in plain PHP, not need for other language.
Basically, your template engine will need 2 things in order to work:
trabalhos.html
file is a template. It's a HTML file that contains special placeholders (like {$d.id}
) that have a particular meaning for your template engine.$a->setId($_POST['id']);
).Now what you need to do is write some code that is able to parse your template files (recognize the placeholders you defined) and work with them. This is quite easy to do if you only need to replace placeholders with the value of a variable but will get tricky as soon as you start to implement features a little more complex like loops.
In addition to that, you also need to write some code that enables you to bind values to your variables as well as objects to store these.
This is a huge job but if your aim is to learn PHP, it's a really good way. Good luck!
Upvotes: 2
Reputation: 493
You can use file write function.
$file = fopen("trabalhos.html","w");
fwrite($file,$lista);
fclose($file);
Upvotes: 0