The new Smarty Forums are now online!
I have to admit I prefer email discussion lists but these things can be useful as well. Maybe I’m just getting old too.
Category Archives: PHP
Simon discusses Smarty
Simon talks about Smarty Templates. He’s the founder of the Smarty Wiki, and lists some links developers will find useful if they’re thinking of using Smarty.
Smarty 2.5.0 is out!
The latest release of Smarty is finally out! Check the site for more info and be aware of the upgrade instructions – leaving old compiled templates around will upset things!
Installing PHP4, mod_ssl and Apache
If you’re installing PHP4, with an ssl enabled Apache you may run into the same problem I just ran into.
The lines LoadModule php4_module libexec/libphp4.so
and AddModule mod_php4.c
are inserted by the php installer automatically. Unfortunately, mod_ssl also adds in code to load mod_ssl.
From what I saw, it looks like the PHP installer adds the php loadmodule commands after the last load module. That’s fine in most cases, but unfortunately here, the last load module command had an if
statement around it. When I visited a non-https url php wouldn’t work!
For some reason it didn’t add the AddType commands either but those were easily copied from another httpd.conf!
Smarty : Template Engine
Yes! Finally Smarty 8.0 has been released! This release has an ASP/JSP/Java/Python/C# code interpretor and lots of other enhancements! Pity the upgrade instructions were lost when the author’s modem died. Anyone tried it?
hehe.
SmartPHP.net – Smart Template
The Smart Template Engine at SmartPHP looks impressive. It uses more traditional templating conventions than Smarty (ie. begin … end) which is *much* simpler for non-techies to understand.
This is one library I’m going to keep an eye on. I wonder how hard it’d be to support both in b2-smarty as the PHP code used to run them is very similar.
SWIG and PHP4
Via the Linux.ie webdev list, SWIG and PHP4. Someone asked if you could interface PHP code to a C++ API. Looks like this could be a winner!
PHP, MVC – continued
I’ve thought about this for a while after posting my previous attempt at using MVC to design a registration form.
So far, I’ve removed the need for the fields array, and simply used the Smarty “include” function to include html code for text boxes and checkboxes.
I also changed the controller so I could pass the names of different model and view classes to it. The controller should really create different view and model classes for each view IMO but that might be overkill for this. It’ll be a challenge when it comes to more non-linear apps where it’ll make sense to put code into seperate files.
Thank you Bruno for your comments. I think having as minimal a view class as possible is good and desirable considering the power of Smarty templates! The Template becomes the view class in other words.
Here’s a good description of the different components of the MVC architecture. There’s also a good page on Controller design.
Using Smarty to implement the MVC design pattern in PHP
Here’s my first go trying to create a registration page using the Model View Controller design.
Before you delve into the code, here’s some background not included in the code:
- You’ll need to know something about Smarty before reading this. Their crash course is useful for this.
- Here’s a Google Search to find more MVC information and tutorials.
- My template dir is “./templates” and for every template file I also have a php file which defines information used by the template. In my example, that includes a list of fields for the registration page. (so I use a Smarty foreach loop to create the form)
- I only have one model and view despite the fact a registration page could be two pages: initial registration page, and a thank you page. The one “view” class displays both by using different Smarty templates.
- I haven’t included the HTML templates as they’re not as interesting as the code.
- In Java land they tend to use page redirects to load alternate pages (ie. send a “Location” HTTP header or use meta tags) but I’ve hijacked the view controller by redefining the view template so it points at “thankyou.tpl” instead of “registration.tpl”. See the switch statement in RegistrationController::main()
I posted some more information to the webdev mailing list too.
I’d appreciate any comments and feedback!
<?php
require_once( “Smarty.class.php” );class registrationView
{
var $smarty;
var $model;function registrationView( &$model, $template )
{
$this->smarty = new Smarty;
$this->model =& $model;
$this->setTemplate( $template );
}function setTemplate( $formTplName = ‘registration’ )
{
$this->formTplName = $formTplName . “.tpl”;
if( is_file( “templates/” . $formTplName . “.php” ) )
{
include_once( “templates/” . $formTplName . “.php” );
$this->fields = $fields;
}
else
{
$this->fields = array();
}}
function display()
{
$this->smarty->assign( “fields”, $this->fields );
$this->smarty->assign( “form”, $this->model->getFormValues() );
$this->smarty->assign( “error”, $this->model->getErrorValues() );
$this->smarty->display( $this->formTplName );
}}
class registrationModel
{
var $form;
var $db;function registrationModel( &$form )
{
$this->db = new DB_Tsc;
$this->form = $form;
}function getFormValues()
{
return $this->form;
}function getErrorValues()
{
return $this->error;
}function validateForm()
{
$ret = true;
if( is_array( $this->form ) )
{
reset( $this->form );
foreach( $this->form as $key => $val )
{
if( $val == ” )
{
$this->error[ $key ] = true;
$ret = false;
}
}
}
else
{
$this->error[ ‘warning’ ] = “Please complete the form”;
$ret = false;
}
return $ret;
}function saveForm()
{
}
}class RegistrationController
{
var $view;
var $model;
var $page;function RegistrationController( $page, &$form )
{
$this->page = $page;
$this->model = new registrationModel( $form );
$this->view = new registrationView( $this->model, “registration” );
}function main()
{
switch( $this->page )
{
case “validate”:
if( $this->model->validateForm() )
{
$this->model->saveForm();
$this->view->setTemplate( ‘thankyou’ );
}
$this->view->display();
break;
default:
$this->view->display();
break;
}
}
}$t = new RegistrationController( $page, $form );
$t->main();
?>
www.logemann.info – why to use template-engines
I was looking at this yesterday with the view to using MVC and Smarty for an app. This introductory tutorial has disappeared in 404 land so grab it quick from the Google Cache while it’s there still.