Beginner's Guide to Creating a PHP Website with a Template

Do you know of any site that can help me start by creating sites with templates in PHP?

-1
source share
4 answers

Depending on your needs, you can create your own template system with several files included.

+1
source

First I will go ahead and start with Smarty, since I used it quite a lot. Since this is a community wiki, feel free to include examples of your own template template if you choose.

Smarty Installation

Templating HTML HTML- . HTML-, .

, Smarty. , Smarty. Smarty. Smarty 3.0.7, . .tar.gz .zip . Windows Mac ( , MAMP XAMPP), .zip, , , .tar.gz. Linux * BSD .tar.gz.

:

COPYING.lib
demo/ 
libs/
|__ Smarty.class.php
|__ ... some other files and folders ...
README
SMARTY2_BC_NOTES

Smarty.class.php , Smarty. libs. "libs" - , Smarty. , :

Smarty/
|__ Smarty.class.php
|__ ... some other files and folders ...

, Smarty, php script, , . -, , templates, . mypage.php mypage.tpl templates:

mypage.php
templates/
|__ mypage.tpl
Smarty/
|__ Smarty.class.php
|__ ... some other files and folders ...

mypage.php

<?php
require_once('Smarty/Smarty.class.php');

$smarty = new Smarty();
$smarty->assign("MYVAR", "Hello World");
$smarty->display("templates/mypage.tpl");
?>

/mypage.tpl

<html>
<head>
  <title>My Sample Page</title>
</head>
<body>
<h1>{$MYVAR}</h1>
</body>
</html>

, mypage.php, http://localhost:8000/mypage.php, "Hello World" . ? :

require_once('Smarty/Smarty.class.php');

Smarty. Smarty.

$smarty->assign("MYVAR", "Hello World");

. "Hello World" "MYVAR". :

<h1>{$MYVAR}</h1>

{$MYVAR}. {} Smarty, . . . $MYVAR, .

$smarty->display("templates/mypage.tpl");

, Smarty , , echo HTML PHP. ? (.. db), , .

Looping Through Arrays

. :

mypage.php

<?php
require_once('Smarty/Smarty.class.php');

$myarray = array(
"John",
"Jane",
"Henry",
"Nancy",
"Gorilla"
);

$smarty = new Smarty();
$smarty->assign("MYARRAY", $myarray);
$smarty->display("templates/mypage.tpl");
?>

/mypage.tpl

<html>
<head>
  <title>My Sample Page</title>
</head>
<body>
<h1>Results</h1>

<ul>
{foreach $MYARRAY as $myvalue}
  <li>{$myvalue}</li>
{/foreach}
</ul>
</body>
</html>

, - :

Result of code

:

$smarty->assign("MYARRAY", $myarray);

, . Smarty, {foreach}:

<ul>
{foreach $MYARRAY as $myvalue}
  <li>{$myvalue}</li>
{/foreach}
</ul>

foreach Smarty foreach PHP. Smarty , $MYVALUE, , $MYVALUE . {$myvalue} . , . , .

HTML . Smarty , . , , . :

mypage.php

<?php
require_once('Smarty/Smarty.class.php');

$myarray = array(
"John",
"Jane",
"Henry",
"Nancy",
"Gorilla"
);

$smarty = new Smarty();
$smarty->assign("MYARRAY", $myarray);
$content = $smarty->fetch("templates/mycontent.tpl");

$smarty->assign("MYCONTENT", $content);
$smarty->display("templates/mypage.tpl");
?>

/mypage.tpl

<html>
<head>
  <title>My Sample Page</title>
</head>
<body>
<h1>Results</h1>

{$MYCONTENT}

</body>
</html>

mycontent.tpl

<ul>
{foreach $MYARRAY as $myvalue}
  <li>{$myvalue}</li>
{/foreach}
</ul>

, , , . mycontent.tpl . , . .

? , :

$content = $smarty->fetch("templates/mycontent.tpl");

fetch() , , HTML. , $MYARRAY mycontent.tpl, , display(). !

Smarty . Smarty Documentation , smarty. , , !

+3

Smarty for PHP is an adequate template system. You can try and use this, as well as your documentation, to help develop a template-driven website.

+2
source

Symfony , in addition to a good structure, offers Twig .

0
source

All Articles