vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB3 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=15)
-   -   Replace variable in template (https://vborg.vbsupport.ru/showthread.php?t=129605)

error_22 10-21-2006 05:35 AM

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

nico_swd 10-21-2006 07:11 AM

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'); 


error_22 10-22-2006 07:10 AM

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

Thank you so much for helping!! =)

nico_swd 10-22-2006 11:25 AM

No problem. If you still have questions on this, just ask.

error_22 10-22-2006 07:08 PM

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

nico_swd 10-22-2006 07:15 PM

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');




All times are GMT. The time now is 07:10 PM.

Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.

X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.01149 seconds
  • Memory Usage 1,758KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)bbcode_code_printable
  • (1)bbcode_html_printable
  • (7)bbcode_php_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (6)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.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/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.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
  • printthread_start
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete