Templating system help

Status
Not open for further replies.

Dzetki

The Prodigal Son Returns
Jul 16, 2010
990
220
Well, hello. I have a tempalting system, but i need all my most crutial codes into a $content tag or something similar to it. Only way i could figure, was to use an include and include a file that contains all the codes i have, but that'd slow the site down wouldn't it?

Could anyone help? Please? ;]
 

Kryptos

prjRev.com
Jul 21, 2010
2,205
1,252
You make no sense, but from the shoutbox I know what you want to do, I think.

index.php
PHP:
<?php
include 'config.php';
include 'template_sys.php';

getFile('templates/default/index.html');

templates/default/index.html
PHP:
<html>
<head><title>{$config->title}</title></head>
<body>

Hey, welcome to {$config->siteName}

</body>
</html>

config.php
PHP:
<?php
$CONFIG = array();

$CONFIG['title'] = 'Index!!!';

$CONFIG['siteName'] = 'YABS: Yet another boring software';
?>

template_sys.php
PHP:
function filter($str)
{
    foreach($CONFIG as $key => $value)
    {
          $str = str_ireplace('{$config->' . $key . '}', $value, $str);
    }

    return $str;
}

function getFile($file)
{
    return filter(file_get_contents($file));
}

Damn this post was big, I wrote all of this in this form, should work. But probably has silly typo.

Anywhore, enjoy!
 

Dzetki

The Prodigal Son Returns
Jul 16, 2010
990
220
Thank you Manuel, once again. But what my main problem is, is that i need alot of code into {$config->content}. Like, all my content persists of
PHP:
$fps = mysql_query("SELECT * FROM posts ORDER BY id DESC LIMIT 5");
                    $id = $_GET['id'];

                    while($row = mysql_fetch_array($fps)) {
                      echo "<strong><div style='border:1px solid #FFF;width:100%;'>" .$row['title']. "</div></strong>" . $row['content'] . "<br /><div style='font-weight:italic;color:silver;font-size:12px;'>Author: " . $author . "</div>";
                      echo "" . $row['content'] . "<br /><div style='font-weight:italic;color:silver;font-size:12px;'>Author: " . $author . "</div>";
                        }
                        if (!$fps)
                        {

                         echo "Could not find any posts/there was an error getting the posts. Sorry!";

                        }
how do i get all of that into {$config->content}?
 

Kryptos

prjRev.com
Jul 21, 2010
2,205
1,252
Sorry to burst your bubble, but you can't do that.

First of all, why would you even want to do that?
Second of all, again, you can't.

These 'tags' aren't used for this. They are used to store HTML, not PHP.
 

Dzetki

The Prodigal Son Returns
Jul 16, 2010
990
220
well, mainly i needed help figuring out how to get all of that, into an easier function. Probably weren't clear enough it seems. Instead of putting all that into a separate file, and include that file into the templating system, what should i do then? As after what i've experienced, including the content all the time leads to a slowdown in the script.
 

Kryptos

prjRev.com
Jul 21, 2010
2,205
1,252
I don't think it'd a slowdown, unless either 1) Your computer is total shit. or 2) The script is huge and it issloppy (Script is VERY simple). or 3) Script has an issue.

PHP:
<?php
function query($SQL)
{
    return mysql_query($SQL);
}

function filter($var)
{
    return mysql_real_escape_string($var);
}

$fps = query("SELECT * FROM posts ORDER BY id DESC LIMIT 5");

if(mysql_num_rows($fp) > 0)
{
    while($row = mysql_fetch_array($fps))
    {
          echo "<strong><div style='border:1px solid #FFF;width:100%;'>" .$row['title']. "</div></strong>" . $row['content'] . "<br /><div style='font' weight:italic;color:silver;font-size:12px;'>Author: " . $row['author'] . "</div>";

          echo "" . $row['content'] . "<br /><div style='font-weight:italic;color:silver;font-size:12px;'>Author: " . $row['author'] . "</div>";
    }
}
else
{
    echo "Could not find any posts/there was an error getting the posts. Sorry!";
}
?>

What you did wasn't coded properly, that is. I also added a little function incase you feel inspired and do some functions of your own to shorten a few lines fo code.
 

Dzetki

The Prodigal Son Returns
Jul 16, 2010
990
220
I don't think it'd a slowdown, unless either 1) Your computer is total shit. or 2) The script is huge and it is sloppy (Script is VERY simple).

PHP:
<?php
function query($SQL)
{
    return mysql_query($SQL);
}

function filter($var)
{
    return mysql_real_escape_string($var);
}

$fps = query("SELECT * FROM posts ORDER BY id DESC LIMIT 5");

if(mysql_num_rows($fp) > 0)
{
    while($row = mysql_fetch_array($fps))
    {
          echo "<strong><div style='border:1px solid #FFF;width:100%;'>" .$row['title']. "</div></strong>" . $row['content'] . "<br /><div style='font' weight:italic;color:silver;font-size:12px;'>Author: " . $row['author'] . "</div>";

          echo "" . $row['content'] . "<br /><div style='font-weight:italic;color:silver;font-size:12px;'>Author: " . $row['author'] . "</div>";
    }
}
else
{
    echo "Could not find any posts/there was an error getting the posts. Sorry!";
}
?>

What you did wasn't coded properly, that is. I also added a little function incase you feel inspired and do some functions of your own to shorten a few lines fo code.
Probably the first as a problem, thinkpad lenovo have been shit since it were released.

Anyhow, thanks again man. I'll start doing that aswell now. ^^

Also, i got an error with the
PHP:
function query($SQL)

Fatal error: Cannot redeclare query() (previously declared in C:\xampp\htdocs\abcpost\show_post.php:3) in C:\xampp\htdocs\abcpost\show_post.php on line 6
 

langer6

Member
Dec 2, 2010
43
3
Your query call could be something like this
PHP:
function query($table, $additional){
    global $query;
    $query = mysql_query("SELECT * FROM news_comments ORDER BY id $order $additional");
    return $query;
}
I do not know if that is what Krptos meant. I find that useful in my scripts :p Usually with a few more lines in it
 
Status
Not open for further replies.

Users who are viewing this thread

Top