Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

How to work with the Smarty Template Engine for beginners

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Smarty Development
View previous topic :: View next topic  
Author Message
0rientsieg
Smarty Rookie


Joined: 08 Jan 2014
Posts: 24

PostPosted: Fri Feb 14, 2014 5:03 pm    Post subject: How to work with the Smarty Template Engine for beginners Reply with quote

1. General
Some months ago I got knowledge about the Smarty Template Engine and I started to think about how to embed Smarty useful into my Internet pages. I programmed previously some Internet pages for the 3 languages Thai, German, and English and up to that date I made for each language an own .html or .php page. That means I had to copy each page in the language I programmed it originally and afterwards I had to change the language of all phrases that should be displayed and then save 3 language-specific files for one item to be displayed in the Internet e.g. lilawadi-en.html for English, lilawadi-ger.html for German, and lilawadi-th.html for Thai language. This was a very boring procedure, also modifications to the web page had to be made 3-fold. In between I tried to put the language in separate language files, one for each language, and I inserted the specific language at runtime with php instructions, but sometimes php created problems, especially if I wanted to change language dependent sequences in JavaScript files. So the promises of Smarty came at the right time to try something different for the language implementation at runtime.
This description is intended to help newcomers to Smarty to get on their feet. It is not intended for Smarty users that work already successfully with Smarty. As a beginner I struggled very hard getting the Smarty demo running in order to find a way, using the benefits of Smarty in my WEB pages. Getting the demo running engaged me for weeks. In order, to help beginners with similar problems I wrote this thread.
2. The Official Smarty Demo
So I went to the official page ‘http://www.smarty.net/’ and downloaded Smarty 3.1.15 which includes the demo program. I installed Smarty as required on my WEB space and tried to start the demo. Doing that I experienced the first 2 hurdles:
Hurdle 1:
At the demo file index.php it is required to ‘include’ or ‘require’ the file ‘libs/Smarty.class.php'. I tried to address this class file like I do it regularly in html by inserting the html path. This did not work, the file was not found.
At the Smarty documentation is stated that the application will find the include path automatically and set it in the constant ‘SMARTY_DIR’ but that did not work automatically, too. Because of the include path did also not work with http syntax I found the following hint in the Smarty documentation:
// windows style
define (‘SMARTY_DIR’, ‘c:/webroot/libs/SMARTY-v.e.r/libs/’);
So there must be somewhat like a ‘webroot’ path about whom I did not know anything and I started to experiment with the above term, retrieved from the official documentation.
I ended up with the following php routine which was able to determine the ‘webroot’:
<?php
$mypath = dirname(__FILE__);
echo $mypath;
?>
This small routine uses the Magic Constant __FILE__ of PHP and in conjunction with the function ‘dirname()’ it delivers, what I call here the ‘webroot’
I put the routine like above into a file I named ‘checkpath’ and placed it into the directory ‘/libs’ of Smarty, that contains the ‘Smarty.class.php’ file. After running that php-file in the explorer I got as result of the ‘echo’ string the following term:
/home/www/virtual/moon-and-sun.com/htdocs/Smarty_3.1/libs
So I inserted in the Smarty Demo the following PHP constant:
$SMARTY_PATH = "/home/www/virtual/moon-and-sun.com/htdocs/Smarty_3.1/libs/";
Important is the slash ‘/’ following of ‘/libs’. The slash is mandatory because it is required for building the complete path to the ‘Smarty.class.php’ file.
I tried again the official Smarty demo and my disappointment was huge as it still did not work. The file ‘Smarty.class.php’ was loaded. That I could clearly realize because I included at the beginning and at the end of this file a php echo instruction with a ‘Hello world 1’ at the beginning and a ‘Hello world 2’ at the end. Both greetings to the world came after starting the demo file ‘index.php’ in the Internet Explorer and subsequently in Firefox, but the related demo template file ‘index.tpl’ was not displayed in the explorer.
Hurdle 2:
So I started again to study the official Smarty documentation, tried this and that until I found out that the path to the directories ‘templates’, ‘templates_c’, ‘configs’, and ‘cache’ mandatorily have to be included into the ‘index.php’ file. So I created again a variable that I called $SMARTY_DIRS containing the path like below and afterwards I assigned the paths of the 4 subdirectories to Smarty ‘index.php’ as follows:
$SMARTY_DIRS = "/home/www/virtual/moon-and-sun.com/htdocs/resort/comptpl/";
$smarty->template_dir = $SMARTY_DIRS . 'templates/';
$smarty->compile_dir = $SMARTY_DIRS . 'templates_c/';
$smarty->config_dir = $SMARTY_DIRS . 'configs/';
$smarty->cache_dir = $SMARTY_DIRS . 'cache/';
After I inserted this additional extension to the official Smarty demo file ‘index.php’, the demo worked perfectly.
3. Making use of Smarty
Now I started to integrate Smarty into my WEB presentations. I took one of the html files, which existed at that time 3-fold, one for English, one for Thai, and one for German, and modified it to one single template file. Additionally I build for the English text of my entire templates one English language file, for my entire German texts for all templates one German language file and same for my entire Thai texts one Thai language file. If I build a new template I add only text in the now existing 3 language files for the 3 languages as required at the new template.
Meanwhile I built 6 templates and at all 3 language files, that means in total 9 files instead of 3 x 6 Internet pages (for the 3 languages). In the model, I used before, I had to build 18 files for the 18 Internet pages, because each language required 1 own Internet page. With Smarty I need for a presentation in 3 languages only one Internet template with Smarty placeholders for the language to be inserted (a variable name which refers to a line in one of the 3 language files) and the server of my host inserts the correct language at runtime in the template at the download from the host server.
4. How to pass the template name and the required language to Smarty
How do I manage to tell at the time of the call of a specific page which language is required? If there was previously no specific language chosen (as at my entrance page), the called internet page will be opened with my standard language, which is Thai (how, I describe later). If the caller is already on a language-specific page of my WEB, all links open in the already chosen language. The example file, I refer to, opens in the basic language Thai. It contains the 3 national flags of Thailand, Germany, and the United Kingdom, where the caller can change the language by clicking on his national flag.
The problem is now, that the Smarty template shall remain the same. The only item, that shall change, is the language. To achieve that, the same template has to be called again but with a different language. So the template name and the language has to be passed to the Smarty starter file which I named ‘input.php’ instead of ‘index.php’ as named in the demo program, to initiate a new Smarty compilation of the same template in a different language. This goal I achieve by using the query string of the URL.
A URL (Uniform Resource Locator) consists of 3 significant parts:
- The service root URL, where the called domain name is inserted
- The resource path, where the path inside the domain is identified
- The query options which the creator of a WEB page may use at his convenience
I use the query options to mark, which Smarty template shall be compiled and which language is required to be inserted by the server. A valid call to my WEB page with the flags to change the language as required reads:
http://www.moon-and-sun.com/conftpl/input.php?tpl=newsjan14&pasa=th
The service root URL is basically my domain name ‘moon-and-sun.com’, the resource path is ‘/conftpl/input.php’, and the ‘?’ marks the beginning of the query options, where I included the template tpl=newsjan14, that shall be loaded and the language pasa=th (pasa in Thai means language). If the query options contain more than 1 parameter, the additional parameters mandatorily have to be separated by a ‘&’.
If somebody clicks at the link above he will get the page, built with the Smarty template ‘newsjan14.tpl’ in the language Thai. The same scheme is used, if at the downloaded internet page is clicked on one of the national flags, only that the parameter ‘pasa=’ changes to ‘en’ for English or to ‘de’ for German. If the mouse hovers over a national flag, the new URL becomes visible in the status line of the explorer. For all 3 languages the URL is identically with the exception of the second query option which can be ’&pasa=th’ on the Thai flag, or ‘&pasa=en’ on the Union Jack, or ‘&pasa=de’ on the German flag. That means, the 3 calls load the same template ‘newsjan14.tpl’ but each link requests a different language.
Note:
Each text in the query string, left and right of the equal signs can be free chosen.
How does the Smarty file ‘input.php’ now identify the contents of the query string and act as expected? For this task I added a small php routine at the top of the file which looks like follows:
<?php
$tpl = trim($_GET['tpl']);
$sprache = $_GET['pasa'];
$iden = $tpl.'-'.$sprache;
$tplfile = $tpl.'.tpl';

if ($sprache == "de")
{ $pasa = "sprache-deS.php";}
elseif ($sprache == "th")
{ $pasa = "sprache-thS.php";}
elseif ($sprache == "en")
{$pasa = "sprache-enS.php";}
?>
(‘<?php’ and ‘?>’ is not required because the template is a php file. I put it only in to remind that the instructions are php).
With the php function ‘$_GET[‘tpl’]’ I retrieve from the query options of the URL the parameter I named in the URL ‘tpl=newsjan14’ so $tpl contains now the template name, but without ‘.tpl’ at the end which will be added later. With the same php function ‘$_GET[pasa]’ I load into the self-defined variable ‘$sprache’ the language ‘pasa=th’ so that the variable contains then ‘th’. After retrieving the template name and the language I build an identification ‘$iden’ of this smarty call by merging in the template name and the language in this example to ‘newsjan14-th’. The identification is required because Smarty does not make in all cases a new compilation if the ‘.tpl’ file name of 2 consequently calls is the same. But if identification is generated for each file, the compilation will be performed if the identification, generated in 2 consecutive calls is different. The identification Smarty gets to know through the instruction
$smarty->compile_id = $iden;
Where ‘$iden’ is the identification I generated earlier by merging the template name and the language abbreviation. THANKS AT THIS SPOT TO ‘U. Tews’ and ‘mohrt’ FROM THE SMARTY FORUM FOR THEIR PATIENCE AND FOR THEIR VALUABLE ADVICES.
The above routine continues by identifying the language handed over and to load the variable ‘$pasa’ with the correct file name of the requested language file. The language file will later be included in my Smarty starter ‘input.php’ file with the instructions
$SPRACHE_PATH = "/home/www/virtual/moon-and-sun.com/htdocs/resort/sprache/";
include $SPRACHE_PATH . $pasa;
and the call to the template file looks like
$smarty->display($tplfile);
Because I merged before into the variable ‘$tplfile’ the terms ‘newsjan14’ and ‘.tpl’ or whatever template file name is required at runtime.
This is how I integrated Smarty as a very useful tool into my WEB pages making life simpler by having to perform modifications to my pages only at one template file instead of 3 language dependent files of the same nature.
There are definitely other methods, which might be better, but my way works and is simple to implement. If somebody wants to use this way or wants to use a different way is up to him.
This description is the result of some weeks struggle with Smarty, but now I create templates and language file extensions very relaxed. The reason, why I use for all my templates only one language file for English, one language file for Thai, and one language file for German is easily explained: Doing so I can call for all my different templates the same Smarty starter ‘input.php’ without being forced to write a comprehensive routine to assign the correct language file. The routine above does it very simply. The including of the language file into the ‘input.php’ reads:
$SPRACHE_PATH = "/home/www/virtual/moon-and-sun.com/htdocs/resort/sprache/";
include $SPRACHE_PATH . $pasa;
If there are additional questions, comments, etc. feel free to reply to this thread.
With regards
Siegfried from Thailand
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Smarty Development All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP