Current location: Hot Scripts Forums » Programming Languages » PHP » Condition in templates


Condition in templates

Reply
  #1 (permalink)  
Old 08-20-08, 12:18 AM
Hamed Hamed is offline
Wannabe Coder
 
Join Date: Jan 2007
Posts: 187
Thanks: 2
Thanked 0 Times in 0 Posts
Condition in templates

Hello
I want to add conditions into my template system.All templates stores in database (Like VB and MyBB) and I get it with this function
PHP Code:

    public function get($name$templatename)

    {
        global 
$db;
        if(
$this->check($name$templatename))
        {
            
$query $db->read("SELECT * FROM $templatename WHERE temname='$name' LIMIT 1");
            
$tem $db->fetch($query);
            
$code $tem['codes'];
            
$name $tem['temname'];
            
$code "<!-- start: $name -->\n$code\n<!-- end: $name -->";
            
$code str_replace("\\'""'"addslashes($code));
            return 
$code;
        }
        else
        {
            return 
false;
        }
    } 
Note $this->check($name, $templatename) check that template exit.
Now I want to make conditions in template for example
Code:
{if $loginset}Hello user.
{else}
Hello visitor.
{/if}
or

{if $loginset == '0' || $loginset != '1'}
Hello User.
{else}
Hello Visitor
{/if}
and is it possible that I get LOOP in my template system?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 08-20-08, 03:42 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,074
Thanks: 11
Thanked 88 Times in 83 Posts
This is very complex, and I'm pretty sure no one will take the time to do this for you.

But you can use Smarty.
www.smarty.net/template.resources
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 08-20-08, 06:11 AM
Hamed Hamed is offline
Wannabe Coder
 
Join Date: Jan 2007
Posts: 187
Thanks: 2
Thanked 0 Times in 0 Posts
I think I can do it with preg_replace();
But my problem is one thing and it is my class I have two way for this job
1-Use it in get() function
2-Use other function to do this
Which one is better and faster?

I have one more question is it possible that I add sth (e.g: other template or var) to start or end of a template or var (ESP:var)?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 08-20-08, 07:05 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,074
Thanks: 11
Thanked 88 Times in 83 Posts
Yes you can do it with preg_replace(), but it's still a complex task. The if/else thing isn't too complicated, you could do it like this:
PHP Code:

$foo true;

$text '
    This is an example...
    
    {if $foo}
        foo is true!
    {else}
        foo is false!
    {/if}
    
    This is the end.'
;

$text preg_replace('~\{if ([^\}]+)\}(.*?)((:?\{else\})(.*?))?\{/if\}~sie''($1 ? "$2" : ("$5" ? "$5" : null))'$text); 
... but the loops will be more difficult.

Quote:
Which one is better and faster?
Well you're only providing your own example. But I think the "check" is redundant. I'm guessing it queries the database to see if it's there? So you're querying the database twice for the same thing?

Why not just:
PHP Code:



public function get($name$templatename)
{
    global 
$db;    
    
$query $db->read("SELECT * FROM $templatename WHERE temname='$name' LIMIT 1");

    if (!
$tem $db->fetch($query))
    {
        return 
false;
    }
    
    
$code $tem['codes'];
    
$name $tem['temname'];
    
$code "<!-- start: $name -->\n$code\n<!-- end: $name -->";
    
$code str_replace("\\'""'"addslashes($code));
    
    return 
$code;

...?


Quote:
I have one more question is it possible that I add sth (e.g: other template or var) to start or end of a template or var (ESP:var)?
I have no idea what you just said. Do you mean you want to merge templates?!


But anyway, why don't you just use Smarty? It does already what you want to do.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 08-20-08, 07:17 AM
Hamed Hamed is offline
Wannabe Coder
 
Join Date: Jan 2007
Posts: 187
Thanks: 2
Thanked 0 Times in 0 Posts
Thanks about your help.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #6 (permalink)  
Old 08-20-08, 12:15 PM
Hamed Hamed is offline
Wannabe Coder
 
Join Date: Jan 2007
Posts: 187
Thanks: 2
Thanked 0 Times in 0 Posts
I get new problem
As I told you I want to add other template to start or end of the other one I make two function for this job look

PHP Code:

    public function addstart($tname$whatadd)
    {
        
$wanted_codes $this->get($whatadd'theme');
        eval(
'$wanted_codes  = "' $wanted_codes '";');
        
$tname $wanted_codes.$tname;
    }
    public function 
addend($tname$whatadd)
    {
        
$wanted_codes $this->get($whatadd'theme');
        eval(
'$wanted_codes  = "' $wanted_codes '";');
        
$tname $tname.$wanted_codes;
    } 
But they do not work
This is simple usage
PHP Code:

    eval('$page  = "' $tem->get('register',$MyTheme) . '";');
    eval(
'$index  = "' $tem->get('index',$MyTheme) . '";');
    
$tem->addend($page'register');
    echo 
$index
One impotant thing I can not change vars with running functions how can I do this jobs.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #7 (permalink)  
Old 08-20-08, 12:24 PM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,074
Thanks: 11
Thanked 88 Times in 83 Posts
Because these functions add the templates to variables in the scope of the function. They're not returning or doing anything outside of it.

Try working with references.
PHP Code:

public function addend(&$tname$whatadd
... note the ampersand sign.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #8 (permalink)  
Old 08-20-08, 01:16 PM
Hamed Hamed is offline
Wannabe Coder
 
Join Date: Jan 2007
Posts: 187
Thanks: 2
Thanked 0 Times in 0 Posts
Thanks about this code it works well but it has one problem when I use ' or " in my conditions I will get
Code:
Warning: Unexpected character in input: ''' (ASCII=39) state=1 in C:\xampp\htdocs\Rose\inc\template.class.php(34) : regexp code on line 1
is there any way to slove it or no I can not use ' or "
and one more thing when I use two vars in condition, condetion become ture although it is false.
PHP Code:

$code preg_replace("~\{if ([^\}]+)\}(.*?)((:?\{else\})(.*?))?\{/if\}~sie"'($1 ? "$2" : ("$5" ? "$5" : null))'$code); 

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #9 (permalink)  
Old 08-20-08, 01:22 PM
Hamed Hamed is offline
Wannabe Coder
 
Join Date: Jan 2007
Posts: 187
Thanks: 2
Thanked 0 Times in 0 Posts
Yes addend function won't work
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #10 (permalink)  
Old 08-20-08, 01:22 PM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,074
Thanks: 11
Thanked 88 Times in 83 Posts
Wrap the condition in parenthesis. This works for me:
PHP Code:

{if ($foo == true OR $bar == true)} 

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Smarty Caching Database Templates jimmy_B PHP 1 12-30-06 03:26 PM
Web Templates As Low As $5 madmatt911 General Advertisements 1 01-26-05 12:05 PM
Free Popular Php-Nuke Themes & phpBB themes forum templates trushkin General Advertisements 0 10-22-04 03:42 PM
Custom Graphic Design, Logo Templates, WebSite Templates, Royalty Free Server Images. A1future General Advertisements 1 06-20-04 06:50 AM


All times are GMT -5. The time now is 08:54 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.