What are the downsides of using Smarty Templates?

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
nutkenz
Forum Contributor
Posts: 155
Joined: Tue Jul 19, 2005 12:25 pm

What are the downsides of using Smarty Templates?

Post by nutkenz »

There sure are a lot of advantages when the designer and the programmer are two (or more) different people, but I'm both programmer and designer. I'm considering using Smarty templates because it produces cleaner code and does have some nice features like built-in functions for formatting data and layout. However, I'm going to need to use different languages (English, Dutch, ...) and I don't see a good way to implement this using Smarty: using several templates would be bothersome because the HTML would have to be edited twice in case of changes. Using seperate variables for each sentence might do the trick, but then again that would also be easier without Smarty because I could simply use a translate("Text") function or something.

Does anyone have any ideas?
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Store your content in the different languages you support in your database, and retrieve the language the user has chosen to view.

That way no multiple templates.
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Post by jmut »

Generally the downside is speed if using smarty...nothing else if you ask me.

You can easily implment different languages using config files
Other than that smarty have really good build in functions...easily extendable...good support.
I like it...speed is no such concern for me.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

plus you can speed things up w/caching
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

/quick pitch for template lite...

There are numeroud ways to do what you want using a template engine. Right now I have a site that uses a template system that literally uses three templates for a site that has a possible 40 pages. As for language changes, you can either set up your language in a database, use language text files (like arrays, etc) or use Smarty/Template lite to include a particular template based on a selected var value. There are a lot of options.
User avatar
AKA Panama Jack
Forum Regular
Posts: 878
Joined: Mon Nov 14, 2005 4:21 pm

Post by AKA Panama Jack »

Your best bet is to use language files. These files are basically PHP files you include into the PHP program that is executing.

An example...

Code: Select all

<?php
session_start();

//define ('TEMPLATE_DIR', "backends/Smarty/libs/");
define ('TEMPLATE_LITE_DIR', "backends/template_lite/src/");

// Define the template class location:
//define('TEMPLATE_CLASS', TEMPLATE_DIR . "Smarty.class.php");   
define('TEMPLATE_CLASS', TEMPLATE_LITE_DIR . "class.template.php");   

require_once (TEMPLATE_CLASS);
$template_object = new Template_Lite;
//$template_object = new Smarty;
$template_object->template_dir = "./templates/";
$template_object->compile_dir = "./templates_c/";

include ("language/" . $_SESSION['language'] . ".inc");

$template_object->display("mypage.tpl");
?>
The $_SESSION['language'] variable is set to english. So the english.inc file is loaded from the language directory. Inside the english.inc you have something like this...

Code: Select all

<?php
$template_object->assign("title", "This is my page title");
$template_object->assign("name", "Please enter your name");
$template_object->assign("submit_button", "Click to Submit");
?>
And the mypage.tpl could look something like...

Code: Select all

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
		<meta http-equiv="Content-Style-Type" content="text/css">
		<title>{$title}</title>
	</head>
	<body>
		<table>
			<tr>
				<td>
					<form action="name.php" method="post">
					{$name} <input type="text" name="name" width="30" size="40" value=""><br>
					<input type="Submit" value=" {$submit_button} ">
					</form>
				</td>
			</tr>
		</table>
	</body>
</html>

That's one of many ways to have language specific pages using a template engine.
Post Reply