PHP Classes

cTemplate

Recommend this page to a friend!

      cTemplate  >  All threads  >  cTemplate  >  (Un) Subscribe thread alerts  
Subject:cTemplate
Summary:problem with creating a template from a string
Messages:5
Author:Darren Conyard
Date:2013-01-08 18:45:10
Update:2013-01-09 13:51:38
 

 


  1. cTemplate   Reply   Report abuse  
Picture of Darren Conyard Darren Conyard - 2013-01-08 18:45:11
Hi,

This seems like a good class for introducing templates, but I creating templates from a string does not work, I had a look but I am not sure why it does not display the template correctly.

Best Regards

Darren

  2. Re: cTemplate   Reply   Report abuse  
Picture of Archzilon Eshun-Davies Archzilon Eshun-Davies - 2013-01-08 22:21:21 - In reply to message 1 from Darren Conyard
Hi Darren

Good to hear from you, I just notice I made a funny typo

so here's a quick fix for you

<?php
require_once("cTemplate.php");

$tpl = new cTemplate();

# String Template
$templatestring = "Hello {NAME}, You have {NUMBERMAILS} new messages";

$tpl->setvar("NAME", "laudarch");
$tpl->setvar("NUMBERMAILS", "10");

# show template from string
print $tpl->gettpl($templatestring);
?>

I'll effect it in the class.

Thanks and Best Regards

  3. Re: cTemplate   Reply   Report abuse  
Picture of Darren Conyard Darren Conyard - 2013-01-09 03:41:50 - In reply to message 2 from Archzilon Eshun-Davies
Hi arch,

If your refering to templatestrign as the typo, then i have more bad news i fixed that typo and i still couldnt generate a template from a string. it was odd caus it looks like it should work.

best regards

Darren

  4. Re: cTemplate   Reply   Report abuse  
Picture of Archzilon Eshun-Davies Archzilon Eshun-Davies - 2013-01-09 13:30:56 - In reply to message 3 from Darren Conyard
Hi Darren

Can you please share the code you used?

Thanks and Best Regards.

  5. cTemplate   Reply   Report abuse  
Picture of Darren Conyard Darren Conyard - 2013-01-09 13:51:38 - In reply to message 4 from Archzilon Eshun-Davies
Hi Arch,

Yeah I was thinking of the same thing last night as you would need to see exactly whats going on. Ok, so just so you know the files dashboard.tpl and cTemplate.php remain the same with no changes.

In your example.php file you use two methods to construct templates, one that shows the template from getting the file itself and the other from writing a string.

This is the code from example.php:
------------------------------------------------------------------------
<?php
ini_set("display_errors",1);
/*
* This is the example script of how to use cTemplate.php
* this has two exapmles, one is to show how to make a templte
* from string and the second is to show how to use a template file
*/
require_once("cTemplate.php");

$tpl = new cTemplate();

# String Template
$templatestring = "Hello {NAME}, You have {NUMBERMAILS} new messages";

# Template in a file
$templatefile = "dashboard.tpl";

$tpl->setvar("NAME", "laudarch");
$tpl->setvar("NUMBERMAILS", "10");

# show template from string
print $tpl->gettpl($templatestring);

# show template from file
print $tpl->gettpl_file($templatefile);
?>
------------------------------------------------------------------------
The following text is displayed on my browser:
------------------------------------------------------------------------
Hello {NAME}, You have 10 new messages
Inbox(10) | Log out
Hello laudarch, You have 10 new messages
------------------------------------------------------------------------
So as you can see I was wondering why {NAME} was being printed when NUMBERMAILS converts successfully to 10 in the string method example.

from the example, the following function call creates the template from a string:

$tpl->gettpl($templatestring);

And this is the function from cTemplate.php:

public function gettpl($strtpl) {
$tpl = "";
foreach($this->vars as $n => $v) {
$tpl = str_replace('{' . $n . '}', $v, $strtpl);
}
return ($tpl);
}

However I do not know why the variable numbermails works and name does not with obtaining the template from a string.

I hope this helps.

Best Regards

Darren