Jump to content
php.lv forumi

Template: Include.


daGrevis

Recommended Posts

Veidoju template sistēmu. ^__^

 

Tagad ir tā, ka katram failam, kurš ir domāts kā fails, kuru redzēs lietotājs, ir template fails ar HTML paplašinājumu. Tajā ir tikai HTML un, pagaidām, iespēja pirms tam (PHP failā) saglabāt kaut ko un tad izvadīt + komentāri.

 

Vēlos izveidot ko šādu...

 

{include "header.html"}

<p>Sveiks, {$username}!</p>

{* Komentārs, kuru klients neredzēs. *}

{include "footer.html"}

 

Lielās problēmas sagādā {include "header.html"}.

 

Tātad Esmu ticis tik tālu, ka šeit ir pattern's, kurš "noķer" visus {include "foo"}...

 

$pattern = '/{include "(.*)"}/';

 

Tālāk netieku... =(

Link to comment
Share on other sites

101111, tas tavs ritenis ir kantains.

Php jau ir templeitu valoda. Nah jātaisa ir vel viens layer'is pa virsu?

 

 

$user = new User( $_SESSION['uid'] );
$page = new Template( '/path/to/template.whateve.r');
$page->assign( 'username' , $user->get_username() );
$page->assign( 'title' , 'Muahahahah !' );

echo $page->render();

 

un fails /path/to/template.whateve.r satur

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="utf-8" />
	<title><?php echo $title; ?></title>
</head>

<body>
	<h1>Hi, <?php echo $username; ?>, welcome to the hell</h1>
</body>
</html>

 

Pati klase Template tāda kā šeit: http://php.lv/f/topic/17701-html-atdalits-no-php/page__view__findpost__p__138431

Link to comment
Share on other sites

Liels paldies azazul un codez! ^__^

 

Kamēr vakar vakarā nebija ko darīt, salaboju arī Savu versiju (ķipa {include "foo.html"}), kuru gan tomēr neizmantošu. =P

 

class template {

public $tags;
public $output;


public function __construct() {}


public function set( $key, $data ) {

	$this->tags[ $key ] = $data;

}

public function get( $key ) {

	return $this->tags[ $key ];

}

public function output( $template ) {

	$this->output = file_get_contents( "styles/default/templates/{$template}" );


	$this->output = preg_replace_callback(

		'/{include "(.*)"}/',

		create_function(

			'$matches',
			'return file_get_contents( "styles/default/templates/" . $matches[ 1 ] );'

		),

		$this->output

	);

	$this->output = preg_replace( '/{\*(.*?)\*}/', '', $this->output );

	forEach( $this->tags as $tag => $data ) {

		$this->output = str_replace( '{$' . $tag . '}', $data, $this->output );

	}


	return $this->output;

}

}

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...