Jump to content
  • 0

Шаблонизатор


vasvas7775
 Share

Question

Ребят, Такой вопрос.

Пытаюсь разобраться в шаблонизаторе, вот PHP код, который заставляет шаблонизатор работать:

<?php

class content
{

var $vars = array();
var $content;

function set($name, $val) {
$this->vars[$name] = $val;
}

function out_content($tpl) {
$this->content = file_get_contents($tpl);

preg_match_all('/\[notlogged\](.*?)\[\/notlogged\]/is', $this->content, $notlogged);
preg_match_all('/\[logged\](.*?)\[\/logged\]/is', $this->content, $logged);

if ($_GET['user'] == 'logged')
{
foreach($logged[1] as $key => $val)
{
$this->content = str_replace('[logged]'.$val.'[/logged]', $val, $this->content);
}
foreach($notlogged[1] as $key => $val)
{
$this->content = str_replace('[notlogged]'.$val.'[/notlogged]', '', $this->content);
}
}
elseif($_GET['user'] == 'notlogged') {
foreach($notlogged[1] as $key => $val)
{
$this->content = str_replace('[notlogged]'.$val.'[/notlogged]', $val, $this->content);
}
foreach($logged[1] as $key => $val)
{
$this->content = str_replace('[logged]'.$val.'[/logged]', '', $this->content);
}
}

preg_match_all('/([a-zA-Z0-9\/\-\_]*)\.([a-z]*)/i', $this->content, $arr);

if($arr[2][0] == 'css')
{
foreach($arr[0] as $key => $val)
{
$this->content = str_replace('['.$val.']', '<link rel="stylesheet" href="'.$val.'" type="text/css" />', $this->content);
}
}

if($arr[2][0] == 'js')
{
foreach($arr[0] as $key => $val)
{
$this->content = str_replace('['.$val.']', '<script src="'.$val.'"></script>', $this->content);
}
}

if(preg_match_all('/\[rss\=([a-zA-z0-9\/\:\.\-\{\}]*)\]/i', $this->content, $arr))
{
foreach($arr[1] as $key => $val)
{
$this->content = str_replace('[rss='.$val.']', '<link rel="alternate" type="application/rss+xml" title="RSS" href="'.$val.'" />', $this->content);
}
}

foreach($this->vars as $key => $val)
{
$this->content = str_replace($key, $val, $this->content);
}

echo $this->content;
}

}

$content = new content();

?>

Вот этот участок, работает по принципу, зарегистрированный пользователь, не зарегистрированный пользователь:

preg_match_all('/\[notlogged\](.*?)\[\/notlogged\]/is', $this->content, $notlogged);
preg_match_all('/\[logged\](.*?)\[\/logged\]/is', $this->content, $logged);

if ($_GET['user'] == 'logged')
{
foreach($logged[1] as $key => $val)
{
$this->content = str_replace('[logged]'.$val.'[/logged]', $val, $this->content);
}
foreach($notlogged[1] as $key => $val)
{
$this->content = str_replace('[notlogged]'.$val.'[/notlogged]', '', $this->content);
}
}
elseif($_GET['user'] == 'notlogged') {
foreach($notlogged[1] as $key => $val)
{
$this->content = str_replace('[notlogged]'.$val.'[/notlogged]', $val, $this->content);
}
foreach($logged[1] as $key => $val)
{
$this->content = str_replace('[logged]'.$val.'[/logged]', '', $this->content);
}
}

Подскажите пожалуйста, как сделать, что бы когда загружается страница index и подгружается шаблон, если пользователь не зарегестрированный, он видел notlogged а если зарегистрированный logged.

Я понимаю надо сделать что то типа сессий, но как не знаю.

Link to comment
Share on other sites

6 answers to this question

Recommended Posts

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   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.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue. See more about our Guidelines and Privacy Policy