WebProWorld Part of WebProNews.com
Page One Link To Us Edit Profile Private Messages Archives FAQ RSS Feeds  
 

Go Back   WebProWorld > Webmaster, IT and Security Discussion > Web Programming Discussion Forum
Subscribe to the Newsletter FREE!


Register FAQ Members List Calendar Arcade Chatbox Mark Forums Read

Web Programming Discussion Forum Working with an API? Developing a plugin? Writing a Mod or script for your favorite blog, Web 2.0 site or Forum? Welcome.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 10-07-2004, 05:58 PM
WebProWorld New Member
 

Join Date: Sep 2004
Posts: 6
cristiu RepRank 0
Default smarty template engine

What do you think about smarty?Exists other template engine?
Reply With Quote
  #2 (permalink)  
Old 10-07-2004, 10:50 PM
mikmik's Avatar
WebProWorld 1,000+ Club
 

Join Date: Aug 2003
Location: Edmonton, AB, Canada
Posts: 3,406
mikmik RepRank 1
Default

Here is an article about template engines:
http://www.sitepoint.com/article/beyond-template-engine
__________________
What I am is what I am, are you what you are, or what.
Eddie Brickel
Reply With Quote
  #3 (permalink)  
Old 10-08-2004, 03:36 AM
WebProWorld Pro
 

Join Date: May 2004
Location: United Kingdom
Posts: 176
php~pro RepRank 0
Default

Too restrictive. You will never beat bespoke coding.
Reply With Quote
  #4 (permalink)  
Old 10-08-2004, 07:27 AM
Easywebdev's Avatar
WebProWorld Veteran
 

Join Date: Apr 2004
Location: Donegal, Ireland.
Posts: 322
Easywebdev RepRank 1
Default

I looked at smarty once, my eyes have never been the same. Horribly bloated class. If I need to use a templating engine for a personal site I use Nathan Codding of the phpbb group's class. Anyone familiar with hacking phpbb will take to it in no time, imho its an excellent piece of code.

Most people just want a simple way of keeping html seperate from php (a good coding practice, so I dont agree with you php~pro, templating can be and is good) through variable replacement, ie placing {SOME_VAR} in their html and have it parsed and replaced with the data assigned to SOME_VAR.

With that in mind I'll post a little template class that does just that and an example php and html page.

save this as template.php
Code:
<?php
	class template
	{
		var $template;

		function template($tpl_file) 
		{
			if (file_exists($tpl_file))
			{
				$this->template = join('', file($tpl_file));
			}
			else
			{
				die("Template file $tpl_file not found.");
			}
		}

		function replace_vars($vars = array()) 
		{
			if (sizeof($vars) > 0)
			{
				foreach ($vars as $var => $data) {
					$this->template = eregi_replace('{' . $var . '}', $data, $this->template);
				}
			}
			else
			{
				die('No variables designated for replacement.');
			}
		}

		function output() 
		{
			echo($this->template);
		}
		
		function destroy()
		{
			$this->template = array();
		}
	}
?>
save as template_test.html
Code:
<html>
<head>
<title>Template Example</title>
</head>
<body>
You should see the words Testing Template Var 1 on the next line.

{template_test_1}


You should see the words Testing Template Var 2 on the next line.

{template_test_2}
</body>
</html>
save as index.php
Code:
<?php
    require_once('template.php');
  
    $template_variables = array();

    $template_variables["$template_test_1"] = "Testing Template Var 1";
    $template_variables["$template_test_2"] = "Testing Template Var 2";
    
    /* Alternatively you can assign variables when you create  the array.
    
    $template_variables = array(
        "template_test_1" => "Testing Template Var 1",
        "template_test_2" => "Testing Template Var 2"
        );
    */
 
    // Create a template class to hold our html content 
    $content = new template('template_test.html');
 
    // Replace any {variables} in our html content 
    $content->replace_vars($template_variables);
 
    // Output our content  
    $content->output();
 
    // Destroy the template class, ready for a new one.   
    $content->destroy();

?>
Hopefully its easy enough for php beginners to understand.
Hack it slap it, do what you want with it. Hope it saves someone some time plodding through smarty when all they want is to seperate html from php.
__________________
"I have not failed. I have found 10,000 ways that don't work" - Thomas Edison.
"The secret to creativity is knowing how to hide your sources" - Albert Einstein.
Reply With Quote
  #5 (permalink)  
Old 10-08-2004, 04:31 PM
WebProWorld New Member
 

Join Date: Sep 2004
Posts: 6
cristiu RepRank 0
Default

Thanks for your posts; I choose Smarty,it has a good documentation and I test some scripts and it's great...
Reply With Quote
  #6 (permalink)  
Old 10-08-2004, 11:08 PM
WebProWorld Member
 

Join Date: Jul 2004
Location: ---| here |---
Posts: 85
vivekar RepRank 0
Default

I use Smarty. It is versatile.

Other option which is lightweight, but functional, is
XTemplate

http://sourceforge.net/projects/xtpl/
__________________
Submit URL - Website Directory Web TemplatesWeb hosting firms list
Reply With Quote
Reply

  WebProWorld > Webmaster, IT and Security Discussion > Web Programming Discussion Forum
Tags: , ,



Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Search Engine Optimization by vBSEO 3.2.0