First we need to create Smarty object. I do it in index.php after including Zend.php:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | require('Zend.php'); include 'smarty/Smarty.class.php'; $smarty = new Smarty(); $smarty->debugging = false; $smarty->force_compile = true; $smarty->caching = false; $smarty->compile_check = true; $smarty->cache_lifetime = -1; $smarty->template_dir = 'resources/templates'; $smarty->compile_dir = 'resources/templates_c'; $smarty->plugins_dir = array( SMARTY_DIR . 'plugins', 'resources/plugins'); |
I don’t like global variables therefor I’ve added Smarty object into Zend Framework’s registry:
1 | Zend::register('smarty', $smarty); |
Using is pretty simple. Just initialize Smarty variables in your Controller class and display template:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | class IndexController extends Zend_Controller_Action { function index() { $smarty = Zend::registry('smarty'); $smarty->assign('title', 'Test'); $smarty->display('index.tpl'); } function noRoute() { $smarty = Zend::registry('smarty'); $smarty->display('error404.tpl'); } } |
As you can see Smarty integration with Zend Framework is very simple task. Zend_View has ability to use your own filters and helper functions, but with Smarty you don’t need them because Smarty has its own plugins, filters, modifiers. Just forget about Zend_View and use best template engine for PHP in the world!
Most of Web-forms has variuos validation mechanisms. Simplest way is to process form on the server and parse result on client, where return data can be in following format:
1 2 | true;User was registered successfully;http://example.com/login/ false;Please enter your name;Entered passwords doesn't match |
Result string can be splitted on client and results can be shown in some part of page. Process function may looks like following (it’s part of smarty_ajax):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | var SmartyAjax = { onSubmit: function(originalRequest) { var results = originalRequest.responseText.split(";"); if (results[0] == "true") { SmartyAjax.Messages.set(results[1], SmartyAjax.Messages.MT_WARNING) } else { SmartyAjax.Messages.clear(); SmartyAjax.Messages.setType(SmartyAjax.Messages.MT_ERROR); for (var i = 1; i < results.length; i++) { SmartyAjax.Messages.add(results[i]); } } } } |
This function uses several functions from SmartyAjax.Messages object which depends on following HTML elements:
1 2 3 4 | <div id="messages"> <p id="messages-title"></p> <ul id="messages-list"></ul> </div> |
To manage messages I’ve created Smarty template parts/warnings.tpl. You can use it in your PHP file in simple way:
1 2 3 4 | // is messages contains warning (or error) $t->assign('messages_warning', true); // array of messages $t->assign('messages', array('Please provide your account information')); |
Another thing we need to examine is message which informs user about processing AJAX request. There is SmartyAjax.Process object in the sample which has two methods: hide() and show() which can be used to hide and display message. They are depends on HTML-element with id=”ajax-process”. It’s necessary to add “position: absolute” style because of this messages will be shown in top-right corner of screen independing on scroll position.
ajax_form is simple to use:
1 2 3 | {ajax_form method="post" id="form_register"} Any form-element can be placed here {/ajax_form} |
Possible parameters:
Example can be found here, and full sources can be downloaded here.
The post AJAX-enabled Smarty plugins Part 2: ajax_form first appeared on Dmytro Shteflyuk's Home.]]>
In my practice I need several things from AJAX: to update some nodes in DOM, to send forms without post-back, to retrieve some values or to perform server-side calculation (maybe using database or other server-side resources). It’s necessary to write tons of JavaScript to implement my requirements in spite of using cool JavaScript library Prototype.
I decided to integrate Smarty with AJAX. There are three Smarty plugins has been created: ajax_update, ajax_call, ajax_form. Below all this plugins will be described.
This Smarty function can be used for update some parts of web-page.
Sample usage:
1 2 | <a href="#" onclick="{ajax_update update_id='intro_content' function='update_intro' params='page=about'}">About</a> |
Possible parameters:
This Smarty function can be used for call PHP function on server side and retrieve its output.
Sample usage:
1 2 | <a href="#" onclick="{ajax_call function='calculate' params_func='calc_params' callback='calc_cb'}">Calculate</a> |
Possible parameters:
This Smarty block can be used for submit Web-forms without post-back.
Sample usage:
1 2 3 | {ajax_form method="post" id="form_register"} Any form-element can be placed here {/ajax_form} |
Possible parameters:
These plugins are quite simple and I think everyone can create even better than mine. I’ve just wanted to show you idea how integration can be done. Working examples can be found here. Also you can download full sources.
The post AJAX-enabled Smarty plugins first appeared on Dmytro Shteflyuk's Home.]]>