Felhasználói eszközök

Eszközök a webhelyen


oktatas:programozas:php:php_template:simple_php

< PHP template

Simple PHP template

Miért jó templatehasználat

Ha egy kódban keverednek a HTML és a PHP kódok nagyobb fejlesztéseknél bosszantó és átláthatatlanná teheti a kódot. Ekkor használjunk templetet.

A load_template_file függvény

list.php
<?php
 
include("template.php");
 
$array = array('nev' => 'Nagy József',
		'telepules' => 'Szolnok',
		'fizetes' => 870000);
 
echo load_template_file("list.tpl", $array);
 
?>
list.tpl
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Dolgozók</title>
 
</head>
<body>
 
Név: {nev}<br>
Település: {telepules}<br>
Fizetés: {fizetes}
 
</body>
</html>
template.php
<?php
 
function load_template_file($filename, $array){
	$f = fopen($filename, "r");	
	$content = fread($f, filesize($filename));
	fclose($f);
 
	$content = addslashes($content);	
	$content = preg_replace("/{(.*?)}/si", "{\$array[\"\\1\"]}" , $content);
	eval("\$content=\"$content\";");
	return $content;
}
 
?>

Több részes template

index.php
<?php
include 'helper.php';
 
echo load('head');
echo load('index');
echo load('foot');
include/helper.php
<?php
 
function load($template) {
	$templatePath = "templates/".$template.".tpl";
	if(!file_exists($templatePath)) {
		echo "A $template nem létezik";			
	}
	return file_get_contents($templatePath);		
}
templates/head.tpl
<!doctype html>
<html lang="hu">
<head>
	<meta charset="utf-8">
	<title>Valami</title>
 
</head>
<body>
templates/foot.tpl
</body>
</html>
templates/index.tpl
<h1>Valami</h1>

Változók cseréje a template fájlban

index.php
<?php
include 'include/helper.php';
 
echo load('head');
$page = load('index');
 
$page = str_replace('{title}', 'Teszt', $page);
$page = str_replace('{des}', 'Ez egy tesztweblap', $page);
$page = str_replace('{city}', 'Szolnok', $page);
echo $page;
 
 
echo load('foot');
templates/index.tpl
<h1>{title}</h1>
<p>{des}</p>
<p>{city}</p>

Stringek tömbben

index.php
<?php
include 'include/helper.php';
 
echo load('head');
$page = load('index');
 
$contentStrings = array(    
    '{title}'  	=> 'Teszt',
    '{des}'	=> 'Ez egy tesztweblap',
    '{city}'	=> 'Szolnok',    
);
foreach ($contentStrings as $key => $string) {
	$page = str_replace("{$key}", $string, $page);
}
 
echo $page;
 
echo load('foot');
templates/index.tpl
<h1>{title}</h1>
<p>{des}</p>
<p>{city}</p>

Több nyelvű oldal és template

Többnyelvű oldal esetén a HTML része feliratait biztosan cserélni szeretnénk, az adott nyelv szövegére. Erre látunk egy rövid példát az alábbiakban.

index.php
<?php
require_once('include/config.php');
require_once('include/helper.php');
require_once("languages/lang_".$app['lang'].".php");
 
//Betöltjük az index.tpl template fájlt:
$page = load('index');
 
//Minden szöveget cserélünk:
foreach ($lang as $key => $string) {
	$page = str_replace("{$key}", $string, $page);
}
 
 
//Mehet minden a képernyőre:
echo load('head');
echo $page;
echo load('foot');

A felhasználói felület által használt nyelv a config.php fájlban van meghatározva:

include/config.php
<?php
 
$app['lang'] = "hu";

Az angol nyelvi fájl:

languages/lang_en.php
<?php
 
$lang = array (
	'{title}' => "Title",
	'{description}' => "Description",
	'{city}' => "City"
);

A magyar nyelvi fájl:

languages/lang_hu.php
<?php
 
$lang = array (
	'{title}' => "Cím",
	'{description}' => "Leírás",
	'{city}' => "Város"
);

Template fájlok:

templates/index.tpl
<h1>{title}</h1>
<p>{description}</p>
<p>{city}</p>
templates/head.tpl
<!doctype html>
<html lang="hu">
<head>
	<meta charset="utf-8">
	<title>Valami</title>
 
</head>
<body>
templates/foot.tpl
</body>
</html>
oktatas/programozas/php/php_template/simple_php.txt · Utolsó módosítás: 2020/06/08 23:31 szerkesztette: admin