• 0

Template Class Issue


Question

Hello,

I'm having a issue with my template class. I need to output a while statement from user.functions.php but when I do the following.

$template = new tpl();
$template->set_template('user_profile.phtml');
$template->write('feed', randomUserFeedFunction());
$template->render();

Heres the issue

Instead of Outputting :

 
Feed message one 1 
Feed message two 2 
etc. 

in the .feed div it out put it at the top of the template :-(. Why?

<?PHP

class template{
    protected $file;
    protected $values = array();


    public function __construct(){    }

    # Loads and sets template file
    public function set_template($file){
        $this->file = $file;
    }
    # Set 
    public function set($key, $value){
        $this->values[$key] = $value;
    }
    # Output
    public function output(){
        if(!file_exists($this->file)){
            return '<fieldset style="background:#FFF;padding:5px;border:1px solid #CCC;"><legend><strong style="color:red;">ERROR</strong></legend>File ('.$this->file.') doesn\'t exist.</fieldset>';
        }
        $output = file_get_contents($this->file);

        foreach($this->values as $key => $value){
            $tagToReplace = "{".$key."}"; # Find {KEY}
            $output = str_replace($tagToReplace, $value, $output);
        }
        return $output;
    }
    # Render Template
    public function render(){
        print $this->output();
    }
}

Link to comment
Share on other sites

19 answers to this question

Recommended Posts

  • 0

Can you post the full Template class? You're calling Template::write in your code and this method does not exist in the posted source.

my class is there

<?PHP

class template{
	protected $file;
	protected $values = array();


	public function __construct(){	}

	# Loads and sets template file
	public function set_template($file){
		$this->file = $file;
	}
	# Set 
	public function set($key, $value){
		$this->values[$key] = $value;
	}
	# Output
	public function output(){
		if(!file_exists($this->file)){
			return '<fieldset style="background:#FFF;padding:5px;border:1px solid #CCC;"><legend><strong style="color:red;">ERROR</strong></legend>File ('.$this->file.') doesn\'t exist.</fieldset>';
		}
		$output = file_get_contents($this->file);

		foreach($this->values as $key => $value){
			$tagToReplace = "{".$key."}"; # Find {KEY}
			$output = str_replace($tagToReplace, $value, $output);
		}
		return $output;
	}
	# Render Template
	public function render(){
		print $this->output();
	}

Link to comment
Share on other sites

  • 0

$template = new tpl();
$template->set_template('user_profile.phtml');
$template->write('feed', randomUserFeedFunction());
$template->render();

If you're using that class, then you would be writing new template(), you're not; you're writing new tpl(). How many templating libraries are you using?

Link to comment
Share on other sites

  • 0

If you're using that class, then you would be writing new template(), you're not; you're writing new tpl(). How many templating libraries are you using?

the new tpl() is just a demo I used for this post. Thats not the problem.

Link to comment
Share on other sites

  • 0

Well, feel free to make up anything else I have to work with. I like games, I mean, Christmas is full of them. :laugh:

In all seriousness though, you're asking me to understand/debug code which I cannot see. As you do not understand what's going on, I think full disclosure would be best here; we'll decide where the problem lies. ;)

Link to comment
Share on other sites

  • 0

Well heres my blog structure.

blog.php

require 'template.class.php';

$post_id = $_GET['post_id'];

$tpl = new tpl();

switch($post_id)
{

    default:
      echo ' this is by default the main page ';
    break;

   case 'post':
	$tpl->set_template('profile-new.phtml');
	$tpl->set('post_id', $post->id);
        $tpl->set('post_title', $post->title);
        $tpl->set('post_text', $post->content);
        $tpl->set('post_tags', getPostTags($post->id)); <============== this is the problem
	$tpl->render();
   break;
}

When ever I try to output this function into the $tpl->set() function it will output it to the top of the template. How can I fix it so that it'll out put at the place where it {blog.tags} was called to.

blogFunctions.php

function getPostTags($pid)
{
	global $dbh;	#	Set Global

	#	SELECT UPDATES FROM BLOG
	$fsql = 'SELECT * FROM `blog` WHERE `id` ="'. $uid .'" ORDER BY `id` DESC LIMIT 10';
	$fq = $dbh->query($fsql);
	$fq->setFetchMode(PDO::FETCH_OBJ);

	while($bp= $fq->fetch()):
		echo $bp->tags;
	endwhile;
}

template.class.php

<?PHP
	# Cupcake Template Engine {key}.
	# Handles Template parsing.
	# 2010 v1

class template{
	protected $file;
	protected $values = array();


	public function __construct(){	}

	# Loads and sets template file
	public function set_template($file){
		$this->file = $file;
	}
	# Set 
	public function set($key, $value){
		$this->values[$key] = $value;
	}
	# Output
	public function output(){
		if(!file_exists($this->file)){
			return '<fieldset style="background:#FFF;padding:5px;border:1px solid #CCC;"><legend><strong style="color:red;">ERROR</strong></legend>File ('.$this->file.') doesn\'t exist.</fieldset>';
		}
		$output = file_get_contents($this->file);

		foreach($this->values as $key => $value){
			$tagToReplace = "{".$key."}"; # Find {KEY}
			$output = str_replace($tagToReplace, $value, $output);
		}
		return $output;
	}
	# Render Template
	public function render(){
		print $this->output();
	}
}

Link to comment
Share on other sites

  • 0

I'm still a little unsure of what you need, but give this a whirl. ;)

function getPostTags($pid)
{
  global $dbh;	#Set Global

  #SELECT UPDATES FROM BLOG
  $fsql = 'SELECT tags FROM `blog` WHERE `id` ="'. $pid .'" ORDER BY `id` DESC LIMIT 10';
  $fq = $dbh->query($fsql);
  $fq->setFetchMode(PDO::FETCH_OBJ);

  $tags = array();
  while($bp= $fq->fetch()){
	array_push($tags, $bp->tags);
  }
  return implode(', ', $tags);
}

Link to comment
Share on other sites

  • 0

You need to return the data, not echo it.

function getPostTags($pid)
{
        global $dbh;    #       Set Global

        #       SELECT UPDATES FROM BLOG
        $fsql = 'SELECT * FROM `blog` WHERE `id` ="'. $uid .'" ORDER BY `id` DESC LIMIT 10';
        $fq = $dbh->query($fsql);
        $fq->setFetchMode(PDO::FETCH_OBJ);
        $buf ='';
        while($bp= $fq->fetch()):
                $buf .= $bp->tags;
        endwhile;
        return $buf;
}

Link to comment
Share on other sites

  • 0

You need to return the data, not echo it.

function getPostTags($pid)
{
        global $dbh;    #       Set Global

        #       SELECT UPDATES FROM BLOG
        $fsql = 'SELECT * FROM `blog` WHERE `id` ="'. $uid .'" ORDER BY `id` DESC LIMIT 10';
        $fq = $dbh->query($fsql);
        $fq->setFetchMode(PDO::FETCH_OBJ);
        $buf ='';
        while($bp= $fq->fetch()):
                $buf .= $bp->tags;
        endwhile;
        return $buf;
}

yours worked fine,but it only outputted one of the status messages that I'm trying to get back instead of returning all of them.

Link to comment
Share on other sites

  • 0

yours worked fine,but it only outputted one of the status messages that I'm trying to get back instead of returning all of them.

Status messages? The method is called getPostTags, surely it's returning post tags.

Edit: regardless, either you're performing a query against the wrong table, or you can only have a single tag per post.

SELECT * FROM `blog` WHERE `id` =

seems completely wrong.

Link to comment
Share on other sites

  • 0

Did you try the function I posted? There was a typo in your original one that 'Kudos' accidentally copied.

Status messages? The method is called getPostTags, surely it's returning post tags.

Sadly, very few 'developers' have actually read an Uncle Bob, GoF or Martin Fowler book.

Clarity, is something a lot of non/new developers struggle with.

Edit: regardless, either you're performing a query against the wrong table, or you can only have a single tag per post.
SELECT * FROM `blog` WHERE `id` =

seems completely wrong.

Whilst I'm sure you're 99% right, you're assuming id is unique.

Link to comment
Share on other sites

  • 0

Sorry posted the wrong function. This is what I'm trying to receive.

function profileFeed($uid)
{
	global $dbh;	#	Set Global
	#	SELECT UPDATES FROM FEEDS
	$fsql = 'SELECT * FROM `feed` WHERE `uid` ="'. $uid .'" ORDER BY `id` DESC LIMIT 10';
	$fq = $dbh->query($fsql);
	$fq->setFetchMode(PDO::FETCH_OBJ);

	while($feed = $fq->fetch()):
		$buf = '<li type="feed"><img src="/resources/imgs/beacon.png"> <strong>'. $feed->type .' <small>/</small></strong> <u>'. $feed->date .'</u> @'. $feed->time .' '. ucwords($feed->content) .'</li><BR />';
	endwhile;
	return $buf;
}

Link to comment
Share on other sites

  • 0

Sorry posted the wrong function. This is what I'm trying to receive.

function profileFeed($uid)
{
	global $dbh;	#	Set Global
	#	SELECT UPDATES FROM FEEDS
	$fsql = 'SELECT * FROM `feed` WHERE `uid` ="'. $uid .'" ORDER BY `id` DESC LIMIT 10';
	$fq = $dbh->query($fsql);
	$fq->setFetchMode(PDO::FETCH_OBJ);

	while($feed = $fq->fetch()):
		$buf = '<li type="feed"><img src="/resources/imgs/beacon.png"> <strong>'. $feed->type .' <small>/</small></strong> <u>'. $feed->date .'</u> @'. $feed->time .' '. ucwords($feed->content) .'</li><BR />';
	endwhile;
	return $buf;
}

function profileFeed($uid)
{
	global $dbh;	#	Set Global
	#	SELECT UPDATES FROM FEEDS
	$fsql = 'SELECT * FROM `feed` WHERE `uid` ="'. $uid .'" ORDER BY `id` DESC LIMIT 10';
	$fq = $dbh->query($fsql);
	$fq->setFetchMode(PDO::FETCH_OBJ);
	$buf = '';
	while($feed = $fq->fetch()):
		$buf .= '<li type="feed"><img src="/resources/imgs/beacon.png"> <strong>'. $feed->type .' <small>/</small></strong> <u>'. $feed->date .'</u> @'. $feed->time .' '. ucwords($feed->content) .'</li><BR />';
	endwhile;
	return $buf;
}

Note the .=

Link to comment
Share on other sites

  • 0
/*
This is only going to return the last instance of the variable, as it is renaming each.
*/
$var = '1';
$var = '2';
$var = '3';

//Returns 3
echo $var

/*
This will return all 3 variables as one, because then .= continues the variable instead of renaming it per say.
*/
$var .= '1';
$var .= '2';
$var .= '3';

//Returns 123
echo $var;

Link to comment
Share on other sites

  • 0

This:

<?php
$a = "Hello";
$a .= " world";
// $a == "Hello world"
?>

is a shorter way of writing:

<?php
$a = "Hello";
$a = $a . " world";
// $a == "Hello world"
?>

The dot operator is for string concatenation, it joins two strings together. In other languages, this would be the + operator (e.g. C) or the & operator (e.g. Visual Basic).

Most of the combined operators like += or *= work in a similar way but instead of concatenating they add or multiply respectively.

It amazes me that you're already doing database communications in PHP when you haven't mastered the basic operators yet. Just my thoughts though... :p

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.