Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: wlasne Template
Forum PHP.pl > Forum > Przedszkole
kicaj
Proboje napisac wlasna klase do obslugi szablonow, jakos mi to idzie, (dopiero zaczynam zabawe z klasami) i jest o pewnien problem.

Mam cos takiego:[php:1:4c0c784fef]<?php
class Templates {

function LoadFile($file_id, $file_name, $file_path) {

if(file_exists($file_path.'/'.$file_name)) {

$file_open = fopen($file_path.'/'.$file_name, "r");
$file_content = fread($file_open, filesize($file_path.'/'.$file_name));
fclose($file_open);

$this -> dane = $file_content;

} else {
return false;
}

}

function Content($name, $value) {

$file_display = str_replace('{'.$name.'}', $value, $this->dane);

//W jaki sposob (chyba bedzie potrzeba petla) zrobic zeby
//kazda blok byl podmieniany??

$this -> Replace = $file_display;

}

function Display() {
return $this-> Replace;
}



}

$template = new Templates;
$template -> LoadFile('start', 'start.tpl', '../templates');
$template -> Content('title', 'Tytul Strony');
$template -> Content('body', 'fksdufdut8');
$template -> Content('itp', '<font color=red>gdsg</font>');
echo $template -> Display();
?>[/php:1:4c0c784fef]
hwao
[php:1:0cee5d68c0]<?php
function Content($name, $value) {

$file_display = str_replace('{'.$name.'}', $value, $this->dane);

//W jaki sposob (chyba bedzie potrzeba petla) zrobic zeby
//kazda blok byl podmieniany??

$this -> Replace = $file_display;

}
[/php:1:0cee5d68c0]

Może nie wykonuj tego tak w "locie" to nie bedzie potrzebna pętla smile.gif
[php:1:0cee5d68c0]<?php
function Content($name, $value)
{ // Dodaj sobie te 2 var 'y i ustal jakies sensowne nazwy winksmiley.jpg
$this->array[] = $name;
$this->array2[] = $value;
}
[/php:1:0cee5d68c0]
Tu wrzucasz sobie dane co ma na co zamienic nastepnie dodajesz 2 funckje do tpl'owania.
[php:1:0cee5d68c0]<?php
function Tpl()
{
/* Bedzie dzialac pod warunkiem ze $this->dane to jest zmienna
* ktora zawiera ciag (kodu htm) jezeli to tablica to ja poprostu
* polacz
*/
return str_replace('{'.$this->array.'}', $this->array2, $this->dane);
}
?>[/php:1:0cee5d68c0]
Powinno działać
kicaj
Kod
Notice: Array to string conversion in wwwfirstclassestemplates.class.php on line 29

wiersz 29:
[php:1:edaa7a3869]<?php
return str_replace('{'.$this->array.'}', $this->array2, $this->dane);
?>[/php:1:edaa7a3869]
Co dalej?
scanner
Cytat
Kod
Notice: Array to string conversion in wwwfirstclassestemplates.class.php on line 29

wiersz 29:
[php:1:1b2c452986]<?php
return str_replace('{'.$this->array.'}', $this->array2, $this->dane);
?>[/php:1:1b2c452986]
Co dalej?
No tutaj jest bład logiczny.
$this->aray trzevba potraktować pętlą i do pierwszego param,etyru dawac wartosci tablicy a nie cala tablice
albo cala tablice potraktowac czyms takim:[php:1:1b2c452986]<?php
foreach( $this->array as $mixValue )
{
$arrTemp[] = '{'.$mixValue.'}';
}
$this->array = $arrTemp;
?>[/php:1:1b2c452986]
kicaj
wlasnie!!

Wiedzialem ze musi byc koniecznie petla hehe
thx scanner smile.gif
Wankster
Nie musi być pętla winksmiley.jpg
W metodzie Content() możesz dać tak:[php:1:b6c4a98e90]<?php

function Content($name, $value)
{ // Dodaj sobie te 2 var 'y i ustal jakies sensowne nazwy
$this->array[] = '{' . $name . '}';
$this->array2[] = $value;
}

?>[/php:1:b6c4a98e90]
A później tylko[php:1:b6c4a98e90]<?php

return $this->dane = str_replace( $this->array, $this->array2, $this->dane );

?>[/php:1:b6c4a98e90]
kicaj
hehe rzeczywiscie smile.gif

trzeba bylo tylko przestawic nawiasy klamrowe smile.gif

thx
Ozzy
albo tak:

[php:1:9ad72f476a]<?php
public function assign($assignment, $value) {

$this->assignments[chr(123) . $assignment . chr(125)] = $value;

}


public function compile($print = FALSE) {

$tpl_output = str_replace(array_keys($this->assignments),
array_values($this->assignments), $this->tpl_contents);

if($print) {
print $tpl_output;
} else {
return $tpl_output;
}

}
?>[/php:1:9ad72f476a]

zastanawiam się jeszcze, czy nie prościej by było:

[php:1:9ad72f476a]<?php
$this -> dane = file_get_contents($file_path.'/'.$file_name);
?>[/php:1:9ad72f476a]
kicaj
...moze i tak ale dopiero zaczynam zabawe z klasami wiec ten przyklad jeszcze narazie odpada smile.gif
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.