Felhasználói eszközök

Eszközök a webhelyen


oktatas:programozas:php:php_template:simple_php

Különbségek

A kiválasztott változat és az aktuális verzió közötti különbségek a következők.

Összehasonlító nézet linkje

Előző változat mindkét oldalon Előző változat
Utolsó változat Következő változat mindkét oldalon
oktatas:programozas:php:php_template:simple_php [2019/05/15 22:48]
oktatas:programozas:php:php_template:simple_php [2019/08/22 10:38]
admin létrehozva
Sor 1: Sor 1:
 +[[oktatas:​programozás:​php:​php_template|<​ PHP template]]
 +
 +====== Simple PHP template ======
 +  * **Szerző:​** Sallai András
 +  * Copyright (c) Sallai András, 2014, 2019
 +  * [[https://​creativecommons.org/​licenses/​by-sa/​4.0/​|CC Attribution-Share Alike 4.0 International]]
 +  * Web: http://​szit.hu
 +===== 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 =====
 +
 +
 +<code php list.php>​
 +<?php
 +
 +include("​template.php"​);​
 +
 +$array = array('​nev'​ => 'Nagy József',​
 + '​telepules'​ => '​Szolnok',​
 + '​fizetes'​ => 870000);
 +
 +echo load_template_file("​list.tpl",​ $array);
 +
 +?>
 +</​code>​
 +
 +
 +<code html 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>​
 +</​code>​
 +
 +
 +<code php 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;
 +}
 +
 +
 +?>
 +
 +</​code>​
 +
 +===== Több részes template =====
 +
 +<code php index.php>​
 +<?php
 +include '​helper.php';​
 +
 +echo load('​head'​);​
 +echo load('​index'​);​
 +echo load('​foot'​);​
 +
 +</​code>​
 +
 +
 +<code php 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);​
 +}
 +
 +</​code>​
 +
 +
 +<code html templates/​head.tpl>​
 +<​!doctype html>
 +<html lang="​hu">​
 +<​head>​
 + <meta charset="​utf-8">​
 + <​title>​Valami</​title>​
 +
 +</​head>​
 +<​body>​
 +
 +</​code>​
 +
 +
 +<code html templates/​foot.tpl>​
 +</​body>​
 +</​html>​
 +
 +</​code>​
 +
 +
 +
 +<code html templates/​index.tpl>​
 +<​h1>​Valami</​h1>​
 +
 +</​code>​
 +
 +
 +===== Változók cseréje a template fájlban =====
 +
 +<code php 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'​);​
 +
 +</​code>​
 +
 +
 +<code php templates/​index.tpl>​
 +<​h1>​{title}</​h1>​
 +<​p>​{des}</​p>​
 +<​p>​{city}</​p>​
 +
 +</​code>​
 +
 +
 +===== Stringek tömbben =====
 +
 +<code php 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'​);​
 +
 +</​code>​
 +
 +
 +<code php templates/​index.tpl>​
 +<​h1>​{title}</​h1>​
 +<​p>​{des}</​p>​
 +<​p>​{city}</​p>​
 +
 +</​code>​
 +
 +
 +===== 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.
 +
 +<code php 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'​);​
 +
 +</​code>​
 +
 +A felhasználói felület által használt nyelv a config.php fájlban van meghatározva:​
 +
 +<code php include/​config.php>​
 +<?php
 +
 +$app['​lang'​] = "​hu"; ​
 +
 +</​code>​
 +
 +Az angol nyelvi fájl:
 +
 +<code php languages/​lang_en.php>​
 +<?php
 +
 +$lang = array (
 + '​{title}'​ => "​Title",​
 + '​{description}'​ => "​Description",​
 + '​{city}'​ => "​City"​
 +);
 +
 +</​code>​
 +
 +
 +A magyar nyelvi fájl:
 +
 +<code php languages/​lang_hu.php>​
 +<?php
 +
 +$lang = array (
 + '​{title}'​ => "​Cím",​
 + '​{description}'​ => "​Leírás",​
 + '​{city}'​ => "​Város"​
 +);
 +
 +</​code>​
 +
 +Template fájlok:
 +
 +<code html templates/​index.tpl>​
 +<​h1>​{title}</​h1>​
 +<​p>​{description}</​p>​
 +<​p>​{city}</​p>​
 +</​code>​
 +
 +
 +<code html templates/​head.tpl>​
 +<​!doctype html>
 +<html lang="​hu">​
 +<​head>​
 + <meta charset="​utf-8">​
 + <​title>​Valami</​title>​
 +
 +</​head>​
 +<​body>​
 +
 +</​code>​
 +
 +
 +<code html templates/​foot.tpl>​
 +</​body>​
 +</​html>​
 +
 +</​code>​
  
oktatas/programozas/php/php_template/simple_php.txt · Utolsó módosítás: 2020/06/08 23:31 szerkesztette: admin