Go Back   vb.org Archive > vBulletin 3 Discussion > vB3 Programming Discussions
FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools Display Modes
  #1  
Old 10-21-2006, 05:35 AM
error_22 error_22 is offline
 
Join Date: Nov 2004
Location: Stockholm, Sweden
Posts: 108
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default Replace variable in template

Hello.

I am currently trying to make myself a simple template engine, for the sake of learning.

I have a table i my db with the following tables:
Code:
t_id, t_name, t_content
to fetch a template, all I have to do is to use a simple query in a function:

PHP Code:
// this code is placed in a file called templates.php

        
function template ($template_name) {

            
$sql "SELECT `t_content` FROM `templates` WHERE `t_name` = '$template_name'";
            
$result mysql_query($sql) or die(mysql_error());
            while (
$row mysql_fetch_object($result))
            {
                echo 
$row->t_content;
            }
        } 
So wherever I wanna display one of my templates I simply have to include the template file and then put in this simple code:

PHP Code:
template(template-name-here); 
Nice and easy. My problem is though; in my templates there are several variables, such as $title, $date, $user, $content etc.. To replace these variable with its actual content I use:

PHP Code:
str_replace() 
but... the content of these variables are called from other tables, in some cases a table called "news" (i have a news script), and in other cases from other tables. So to summarize....Depending on which template i want to use, different tables should be called and replace the variables in the template.

Is there a good way to do this? I cant for the world understand how this would work. Im hoping for some sort of automated script which replaces the right stuff in the right template, calling the right tables etc. just by printing a function with some values or something. Any help would be greatly appreciated!

Thanks in advance
Niklas
Reply With Quote
  #2  
Old 10-21-2006, 07:11 AM
nico_swd's Avatar
nico_swd nico_swd is offline
 
Join Date: Dec 2005
Location: Spain
Posts: 170
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

I wrote a mini-template class a while back. I modified it a bit for your needs. Give it a try if you want.

PHP Code:
class swd_template
{
    
    var 
$variables = array();
    
    
    function 
parse($template)
    {
        
extract($this->variables);
        
        eval(
'$template = stripslashes("'$this->fetch_template($template) .'");');
        
        echo 
$template;
    }
    

    function 
fetch_template($template_name)
    {
        
$sql "SELECT `t_content` FROM `templates` WHERE `t_name` = '$template_name' LIMIT 1";
        
$result mysql_query($sql) or die(mysql_error());
        
$template mysql_fetch_array($result);
    
        return 
addslashes($template['t_content']);
    }
    
    
    function 
assign($key$value false)
    {
        if (
is_array($key))
        {
            
$this->variables array_merge($this->variables$key);
        }
        else
        {
            
$this->variables[$key] = $value;
        }
    }

Usage example
PHP Code:
$template = new swd_template;

$template->assign(array(
    
'welcome' => 'hello you'// Replaces $welcome, {$welcome} with "hello you"
    
'date'      => date('dS M Y, H:i:s'), // Replaces $date with the current date
    
'var'      => 'Value' // And $var with value
));

$template->parse('template_name_here'); 
Reply With Quote
  #3  
Old 10-22-2006, 07:10 AM
error_22 error_22 is offline
 
Join Date: Nov 2004
Location: Stockholm, Sweden
Posts: 108
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Just what I was looking for, finally I understand!!

Thank you so much for helping!! =)
Reply With Quote
  #4  
Old 10-22-2006, 11:25 AM
nico_swd's Avatar
nico_swd nico_swd is offline
 
Join Date: Dec 2005
Location: Spain
Posts: 170
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

No problem. If you still have questions on this, just ask.
Reply With Quote
  #5  
Old 10-22-2006, 07:08 PM
error_22 error_22 is offline
 
Join Date: Nov 2004
Location: Stockholm, Sweden
Posts: 108
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Ok I actually have one more question.

Your script works perfect, but there's something I dont really know.

I have a a tempalte aclled "index". In the index template theres a variable called "$news". The $news veriable should be replace with a tempalte called "news". That template looks something like this:
HTML Code:
<blockquote class="go">
	<p>
		<h3>$title</h3> - <h5>posted: $date</h5>
	</p>
	<p>
		$content
	</p>
</blockquote>
Now.... $title, $date, and $content should be replaced with the content from three columns (with the same names) from a table called "news". I dont know if this is the right way to do it, but I just assumed that I had to have a news template since that one is gonna be looped over and over (total amount of times depends on how many news entries there are). If I put those three variables directly in the index template, everything in there will be looped. How would I go on doing this?

Thank yuo so much for taking the time!

Niklas
Reply With Quote
  #6  
Old 10-22-2006, 07:15 PM
nico_swd's Avatar
nico_swd nico_swd is offline
 
Join Date: Dec 2005
Location: Spain
Posts: 170
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Well it's just a simple template system. There's no real good way to do it. One of them would be something like this.

PHP Code:

$query 
mysql_query("SELECT * FROM news... whatever...");

while (
$news mysql_fetch_array($query))
{
    
$template->assign($news);

    
$template->parse('news');

EDIT:
Assuming the fields in the database are called "news", "date", and "content". Otherwise try this

PHP Code:

$query 
mysql_query("SELECT * FROM news... whatever...");

while (
$news mysql_fetch_array($query))
{
    
$template->assign(array(
         
'title'   => $news['title'],
         
'date'    => $news['date'],
         
'content' => $news['content']
    ));

    
$template->parse('news');

Reply With Quote
Reply


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 On
HTML code is Off

Forum Jump


All times are GMT. The time now is 03:44 PM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2024, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.08087 seconds
  • Memory Usage 2,240KB
  • Queries Executed 11 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (1)ad_showthread_firstpost
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)bbcode_code
  • (1)bbcode_html
  • (7)bbcode_php
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (6)post_thanks_box
  • (6)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (6)post_thanks_postbit_info
  • (6)postbit
  • (6)postbit_onlinestatus
  • (6)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • fetch_musername
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete