Forum Moderators: coopster

Message Too Old, No Replies

problem with short open tag with my template class

It prints unecessary php tags inside my template

         

paperso

4:12 am on Sep 20, 2010 (gmt 0)

10+ Year Member



Hello guys im here again \(^_^)/

I had this very simple template engine:
<?PHP
if (!defined('DIRECT_ACCESS')) { die('Forbidden!'); }

class PSOUP_template {

public $PSOUP_template;

/*
* Loads a template file
*/
public function PSOUP_template($template) {

if (file_exists($template)) {
$this->PSOUP_template = file_get_contents($template);
} else {
die ("Couldn't load template file {$template}!");
}

}

/*
* Replaces and assigns tags for each template file
*/
public function assign_vars($vars = array()) {

if (sizeof($vars) > 0) {

foreach ($vars as $tag => $content) {
$replace_tag = "{" .$tag. "}";
$this->PSOUP_template = str_replace($replace_tag, $content, $this->PSOUP_template);
}

} else {
die ("You didn't specify any tags!");
}

}

/*
* Displays the template and enables PHP tags
*/
public function display() {
global $title, $_SERVER, $PSOUP_CONTENT_DIR, $PSOUP_SETTINGS, $pagecat, $postcat, $i, $error, $post, $PSOUP_sessions, $post_comment, $post_featured, $post_published, $PSOUP_load_tpl, $homepage, $users_hideemail, $comments, $user;
$output = $this->PSOUP_template;

eval ("php?>".$output."<?");
}

}
?>


It enables me to use .tpl files. It also enables me to include PHP functions. Oky those were fine and working not until I disable php's short_open_tag. The template class now prints php tags inside my template!

I can get through this problem when I delete the php tags in the below code:
eval ("php?>".$output."<?");


But if I do that, I can no longer include php functions inside my template.

I need help guys!

paperso

4:40 am on Sep 20, 2010 (gmt 0)

10+ Year Member



Found an answer @ php manual for eval();

I don't know if this is the right way to do it but it worked. Please tell me if i'm doing it right.

I changed the eval from the above code into:

// Adding a space after the open tag fixes it
eval ('?>' . $output . '<?php ');

You can see space at the opening tag.

coopster

10:45 am on Oct 5, 2010 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



If you want to execute PHP code within a file, use include [php.net] and friends. No need to invoke eval().

paperso

5:22 am on Oct 10, 2010 (gmt 0)

10+ Year Member



No i'd rather not use include. I basically wanted to print php tags on my templates. Example is I had template.tpl and I can print <?PHP echo 'It worked!' ?>