Tag Archives: flexy template engine

Flexy Template Engine Tutorial

Okay this is for anyone still interested in Flexy Templates. I know it's a little outdated, but there are still thousands of websites using it at least and chances are you will get a job that requires the modification of Flexy Templates sooner or later if you are a developer.

Installing Flexy

Stay away from the documentation on Flexy from the Pear site because it sucks! I had to figure this out on my own. But do go to http://pear.php.net/package/HTML_Template_Flexy/redirected and download the source code.

Do not use the installers because they install everything in the wrong damn directorys. Sorry I don't usually cuss in my blog, but this pissed me off because it took me hours to come to the conclusions on this page because they didn't document it well or install it correctly from their installers. It may have something to do with the fact that the Flexy Engine hasn't been update since 2009 though, so I hold no grudges.

Installing Flexy

Check that you have pear installed first by typing "pear" at the command prompt. If you have pear installed it will show a list of pear related packages or something like that. Use Putty to get a command prompt(Google "Putty download") if you don't have direct access to your server.

If you don't have pear installed install it with:

wget http://pear.php.net/go-pear.phar
php go-pear.phar

Download the Flexy Files from:

http://download.pear.php.net/package/HTML_Template_Flexy-1.3.12.tgz

You will need 7-zip or similar program to extract it into a tar and then into a folder. Download 7zip from:

http://downloads.sourceforge.net/sevenzip/7z920.exeYou will have to right click on the file and select  "7 zip/extract  files" and it will add a new folder which you then have to open and extract the file inside the same way to finally get the package completely unzipped.

Locate the main folder and rename it from HTML_Template_Flexy-1... to simply "Flexy" to make things easier.

Then upload the Flexy folder to your working web directory. Mine was var/www/html. That is what the installer doesn't do. It installs from the installer to a directory below the web root which I found to be useless, but I am a rookie at this right now, however, for all you other rookies, this is how to get going.

Now disregard all the tests in the test folder as I found them to be useless and deleted them. More aggravation! Try this simple hello world script to get going with Flexy:

Live Flexy Example

First make sure your directory structure is set up properly. You should have your web root folder which is usually named something like html or htdocs. Then inside that is where you should have uploaded your Flexy folder to. Okay good.

Open the Flexy folder and create a directory called templates and another called compiled.

Now open a text editor and enter the following into it which is going to be your hello_world.tmpl file:

<html>
  <head></head>
  <body>
    <h2>{intro}</h2>
    <h3>{testing}</h3>
  </body>
</html>

Now save the file as hello_world.tmpl. Upload the file to the Flexy/templates directory. Now open a new file we will call hello_world.php and insert the following text into it:

<?php
// change error reporting level
error_reporting(E_ALL & ~E_NOTICE);

// include class
require ('HTML/Template/Flexy.php');

// set class configuration
$options = array(
    'templateDir'   => './templates',
    'compileDir'    => './compile',
);

// initialize template engine
$flexy = new HTML_Template_Flexy($options);
 
// define object and properties
// corresponding to template variables
$flexy->setData('intro', 'Hello World!');
$flexy->setData('testing', 'This is just one thing Flexy can do...');

// compile template and interpolate object
// display result
$flexy->compile("hello_world.tmpl");
$flexy->output();
?>

Save this file right inside the Flexy directory, then go to your browser and enter something like your_site.com/Flexy/hello_world.php and you should see your script in action. However, if you didn't think to make the compile directory writable, you may get an error that looks like this:

can not write to 'compileDir', which is './compile'
Please give write and enter-rights to it

...in which case, simply use your FTP editor such as FileZilla to change the permissions of the compile directory you created earlier inside the main Flexy folder. Change to 777 for now but you may want to tighten up security later when in production use.

If it works, you should see the following text on your screen when you go to your-site.com/Flexy/hello_world.php:

Hello World!

This is just one thing Flexy can do...

Short and sweet the way a hello world example should be! However, I did add a little more detail later on at one of my readers request, so I hope this is enough to get you going now.  I couldn't find anything like this until I looked for two days online, so hopefully this helps someone. Please comment below if it does so I feel better:-)