⬆️ ⬇️

Using SMARTY templates with PEAR HTML_QuickForm

1. Introduction.



What is this article.



This article is a simple introduction to using Smarty templates with PEAR HTML_QuickForm classes. The article is not an exhaustive guide, and in fact represents only a small part of the functionality of the Smarty templates. However, for a newbie to use Smarty templates this article will be a useful base.







Translator's note: Since Habr parses HTML, in some places the code is presented in the form of screenshots.

')

Prerequisites.



The implication is that you have already installed PHP and PEAR and that you are familiar with PHP and HTML. You should also be familiar with using the PEAR HTML_QuickForm class. You can get help with PEAR on the PEAR website, help with PHP can be found on the PHP website and an introduction to using HTML_QuickForm can be found in my other article .



This article is written from a practical point of view. If you want to get the most out of it, you will have to copy the code for the examples from here and run it yourself. Among other things, templates are used to represent information and examples of data output are not presented in this article. If you want to see templates in action, you will have to run the code yourself. Finally, the code examples presented in this article are intended to represent the use of Smarty templates only. In other words, functions such as data validation were omitted to more clearly represent the function of the templates.



2. Basic forms.



Short review.



Let's start with a simple form that does not use templates. Later we will change it to use templates.

Create the following file and place in the folder to which your web server has access and display the file in the browser:







You should see the form, enter the information and send the data. After that, the page will reload with information in a static form. If this does not work or you do not understand why it works, then please look at the following article .

Note the following about the code above:





3. A bit of theory.





What are templates used for?



As we saw in the example above, by default, templates created by the PEAR HTML_QuickForm classes are displayed in an extremely simple way - a shortcut with a subsequent form and with elements displayed under each other.

Being able to work these forms lack some of the strokes for a professional application.



One of the problems in generating software-controlled HTML is the separation of the displayed elements from the program code, and this is what the PEAR classes are doing quite successfully.



Templates enable the designer to create much more attractive and complex pages and precisely determine the display positions of form elements. The designer works with HTML files, the programmer works with PHP code and PEAR classes and changing one of them does not require changing the second one.



How do Smarty templates work with HTML_QuickForm?





From the example above, the line $ form → display (); calls the standard HTML_QuickForms renderer. Replacing this line can achieve greater control over the displayed HTML. It will also give us the opportunity to divide the code into 2 parts: the PHP / PEAR code and the template file itself.



The template file contains HTML code and some additional tags. At this simple level, these tags are places to display information. The role of the visualizer is to take these forms and transfer them to a format that can be used by the template object. The role of the template object is to parse the template file and replace the additional tags with data from the form.



Creating and rendering a form using a template is a multi-step process.



  1. A form is created using PEAR classes in the same way as before.
  2. A template object is created,
  3. A visualization object is created,
  4. The visualizer generates HTML fragments for each form element and these fragments, together with the rest of the information about the forms, are collected in one large array.
  5. The form data array is passed to the template object,
  6. Next, the template object parses the template file, replaces tags with data from an array and displays HTML.




4. A simple form template.





Simple example





We will now change the example above to use the template. We will need a couple of lines of require_once at the beginning of the file, and we will also replace the line $ form → display (); pattern code. We can also remove all HTML code. The result is shown below:



  1. <?php require_once "HTML/QuickForm.php" ; require_once 'HTML/QuickForm/Renderer/ArraySmarty.php' ; require_once 'Smarty.class.php' ; $form = new HTML_QuickForm ( 'frmTest' , 'get' ) ; $form -> addElement ( 'header' , 'hdrTesting' , ' Smarty' ) ; $form -> addElement ( 'text' , 'txtFirstName' , '?' ) ; $form -> addElement ( 'text' , 'txtLastName' , '?' ) ; $form -> addElement ( 'text' , 'txtAge' , '?' ) ; $form -> addElement ( 'text' , 'txtTelephone' , ' ?' ) ; $form -> addElement ( 'reset' , 'btnClear' , '' ) ; $form -> addElement ( 'submit' , 'btnSubmit' , '' ) ; if ( $form -> validate ( ) ) { # , $form -> freeze ( ) ; } // $tpl =& new Smarty ; $tpl -> template_dir = '.' ; $tpl -> compile_dir = '/tmp' ; // $renderer =& new HTML_QuickForm_Renderer_ArraySmarty ( $tpl ) ; // HTML $form -> accept ( $renderer ) ; // $tpl -> assign ( 'form_data' , $renderer -> toArray ( ) ) ; // $tpl -> display ( 'smarty1.tpl' ) ; ?>
  2. <?php require_once "HTML/QuickForm.php" ; require_once 'HTML/QuickForm/Renderer/ArraySmarty.php' ; require_once 'Smarty.class.php' ; $form = new HTML_QuickForm ( 'frmTest' , 'get' ) ; $form -> addElement ( 'header' , 'hdrTesting' , ' Smarty' ) ; $form -> addElement ( 'text' , 'txtFirstName' , '?' ) ; $form -> addElement ( 'text' , 'txtLastName' , '?' ) ; $form -> addElement ( 'text' , 'txtAge' , '?' ) ; $form -> addElement ( 'text' , 'txtTelephone' , ' ?' ) ; $form -> addElement ( 'reset' , 'btnClear' , '' ) ; $form -> addElement ( 'submit' , 'btnSubmit' , '' ) ; if ( $form -> validate ( ) ) { # , $form -> freeze ( ) ; } // $tpl =& new Smarty ; $tpl -> template_dir = '.' ; $tpl -> compile_dir = '/tmp' ; // $renderer =& new HTML_QuickForm_Renderer_ArraySmarty ( $tpl ) ; // HTML $form -> accept ( $renderer ) ; // $tpl -> assign ( 'form_data' , $renderer -> toArray ( ) ) ; // $tpl -> display ( 'smarty1.tpl' ) ; ?>
  3. <?php require_once "HTML/QuickForm.php" ; require_once 'HTML/QuickForm/Renderer/ArraySmarty.php' ; require_once 'Smarty.class.php' ; $form = new HTML_QuickForm ( 'frmTest' , 'get' ) ; $form -> addElement ( 'header' , 'hdrTesting' , ' Smarty' ) ; $form -> addElement ( 'text' , 'txtFirstName' , '?' ) ; $form -> addElement ( 'text' , 'txtLastName' , '?' ) ; $form -> addElement ( 'text' , 'txtAge' , '?' ) ; $form -> addElement ( 'text' , 'txtTelephone' , ' ?' ) ; $form -> addElement ( 'reset' , 'btnClear' , '' ) ; $form -> addElement ( 'submit' , 'btnSubmit' , '' ) ; if ( $form -> validate ( ) ) { # , $form -> freeze ( ) ; } // $tpl =& new Smarty ; $tpl -> template_dir = '.' ; $tpl -> compile_dir = '/tmp' ; // $renderer =& new HTML_QuickForm_Renderer_ArraySmarty ( $tpl ) ; // HTML $form -> accept ( $renderer ) ; // $tpl -> assign ( 'form_data' , $renderer -> toArray ( ) ) ; // $tpl -> display ( 'smarty1.tpl' ) ; ?>
  4. <?php require_once "HTML/QuickForm.php" ; require_once 'HTML/QuickForm/Renderer/ArraySmarty.php' ; require_once 'Smarty.class.php' ; $form = new HTML_QuickForm ( 'frmTest' , 'get' ) ; $form -> addElement ( 'header' , 'hdrTesting' , ' Smarty' ) ; $form -> addElement ( 'text' , 'txtFirstName' , '?' ) ; $form -> addElement ( 'text' , 'txtLastName' , '?' ) ; $form -> addElement ( 'text' , 'txtAge' , '?' ) ; $form -> addElement ( 'text' , 'txtTelephone' , ' ?' ) ; $form -> addElement ( 'reset' , 'btnClear' , '' ) ; $form -> addElement ( 'submit' , 'btnSubmit' , '' ) ; if ( $form -> validate ( ) ) { # , $form -> freeze ( ) ; } // $tpl =& new Smarty ; $tpl -> template_dir = '.' ; $tpl -> compile_dir = '/tmp' ; // $renderer =& new HTML_QuickForm_Renderer_ArraySmarty ( $tpl ) ; // HTML $form -> accept ( $renderer ) ; // $tpl -> assign ( 'form_data' , $renderer -> toArray ( ) ) ; // $tpl -> display ( 'smarty1.tpl' ) ; ?>
  5. <?php require_once "HTML/QuickForm.php" ; require_once 'HTML/QuickForm/Renderer/ArraySmarty.php' ; require_once 'Smarty.class.php' ; $form = new HTML_QuickForm ( 'frmTest' , 'get' ) ; $form -> addElement ( 'header' , 'hdrTesting' , ' Smarty' ) ; $form -> addElement ( 'text' , 'txtFirstName' , '?' ) ; $form -> addElement ( 'text' , 'txtLastName' , '?' ) ; $form -> addElement ( 'text' , 'txtAge' , '?' ) ; $form -> addElement ( 'text' , 'txtTelephone' , ' ?' ) ; $form -> addElement ( 'reset' , 'btnClear' , '' ) ; $form -> addElement ( 'submit' , 'btnSubmit' , '' ) ; if ( $form -> validate ( ) ) { # , $form -> freeze ( ) ; } // $tpl =& new Smarty ; $tpl -> template_dir = '.' ; $tpl -> compile_dir = '/tmp' ; // $renderer =& new HTML_QuickForm_Renderer_ArraySmarty ( $tpl ) ; // HTML $form -> accept ( $renderer ) ; // $tpl -> assign ( 'form_data' , $renderer -> toArray ( ) ) ; // $tpl -> display ( 'smarty1.tpl' ) ; ?>
  6. <?php require_once "HTML/QuickForm.php" ; require_once 'HTML/QuickForm/Renderer/ArraySmarty.php' ; require_once 'Smarty.class.php' ; $form = new HTML_QuickForm ( 'frmTest' , 'get' ) ; $form -> addElement ( 'header' , 'hdrTesting' , ' Smarty' ) ; $form -> addElement ( 'text' , 'txtFirstName' , '?' ) ; $form -> addElement ( 'text' , 'txtLastName' , '?' ) ; $form -> addElement ( 'text' , 'txtAge' , '?' ) ; $form -> addElement ( 'text' , 'txtTelephone' , ' ?' ) ; $form -> addElement ( 'reset' , 'btnClear' , '' ) ; $form -> addElement ( 'submit' , 'btnSubmit' , '' ) ; if ( $form -> validate ( ) ) { # , $form -> freeze ( ) ; } // $tpl =& new Smarty ; $tpl -> template_dir = '.' ; $tpl -> compile_dir = '/tmp' ; // $renderer =& new HTML_QuickForm_Renderer_ArraySmarty ( $tpl ) ; // HTML $form -> accept ( $renderer ) ; // $tpl -> assign ( 'form_data' , $renderer -> toArray ( ) ) ; // $tpl -> display ( 'smarty1.tpl' ) ; ?>
  7. <?php require_once "HTML/QuickForm.php" ; require_once 'HTML/QuickForm/Renderer/ArraySmarty.php' ; require_once 'Smarty.class.php' ; $form = new HTML_QuickForm ( 'frmTest' , 'get' ) ; $form -> addElement ( 'header' , 'hdrTesting' , ' Smarty' ) ; $form -> addElement ( 'text' , 'txtFirstName' , '?' ) ; $form -> addElement ( 'text' , 'txtLastName' , '?' ) ; $form -> addElement ( 'text' , 'txtAge' , '?' ) ; $form -> addElement ( 'text' , 'txtTelephone' , ' ?' ) ; $form -> addElement ( 'reset' , 'btnClear' , '' ) ; $form -> addElement ( 'submit' , 'btnSubmit' , '' ) ; if ( $form -> validate ( ) ) { # , $form -> freeze ( ) ; } // $tpl =& new Smarty ; $tpl -> template_dir = '.' ; $tpl -> compile_dir = '/tmp' ; // $renderer =& new HTML_QuickForm_Renderer_ArraySmarty ( $tpl ) ; // HTML $form -> accept ( $renderer ) ; // $tpl -> assign ( 'form_data' , $renderer -> toArray ( ) ) ; // $tpl -> display ( 'smarty1.tpl' ) ; ?>
  8. <?php require_once "HTML/QuickForm.php" ; require_once 'HTML/QuickForm/Renderer/ArraySmarty.php' ; require_once 'Smarty.class.php' ; $form = new HTML_QuickForm ( 'frmTest' , 'get' ) ; $form -> addElement ( 'header' , 'hdrTesting' , ' Smarty' ) ; $form -> addElement ( 'text' , 'txtFirstName' , '?' ) ; $form -> addElement ( 'text' , 'txtLastName' , '?' ) ; $form -> addElement ( 'text' , 'txtAge' , '?' ) ; $form -> addElement ( 'text' , 'txtTelephone' , ' ?' ) ; $form -> addElement ( 'reset' , 'btnClear' , '' ) ; $form -> addElement ( 'submit' , 'btnSubmit' , '' ) ; if ( $form -> validate ( ) ) { # , $form -> freeze ( ) ; } // $tpl =& new Smarty ; $tpl -> template_dir = '.' ; $tpl -> compile_dir = '/tmp' ; // $renderer =& new HTML_QuickForm_Renderer_ArraySmarty ( $tpl ) ; // HTML $form -> accept ( $renderer ) ; // $tpl -> assign ( 'form_data' , $renderer -> toArray ( ) ) ; // $tpl -> display ( 'smarty1.tpl' ) ; ?>
  9. <?php require_once "HTML/QuickForm.php" ; require_once 'HTML/QuickForm/Renderer/ArraySmarty.php' ; require_once 'Smarty.class.php' ; $form = new HTML_QuickForm ( 'frmTest' , 'get' ) ; $form -> addElement ( 'header' , 'hdrTesting' , ' Smarty' ) ; $form -> addElement ( 'text' , 'txtFirstName' , '?' ) ; $form -> addElement ( 'text' , 'txtLastName' , '?' ) ; $form -> addElement ( 'text' , 'txtAge' , '?' ) ; $form -> addElement ( 'text' , 'txtTelephone' , ' ?' ) ; $form -> addElement ( 'reset' , 'btnClear' , '' ) ; $form -> addElement ( 'submit' , 'btnSubmit' , '' ) ; if ( $form -> validate ( ) ) { # , $form -> freeze ( ) ; } // $tpl =& new Smarty ; $tpl -> template_dir = '.' ; $tpl -> compile_dir = '/tmp' ; // $renderer =& new HTML_QuickForm_Renderer_ArraySmarty ( $tpl ) ; // HTML $form -> accept ( $renderer ) ; // $tpl -> assign ( 'form_data' , $renderer -> toArray ( ) ) ; // $tpl -> display ( 'smarty1.tpl' ) ; ?>
  10. <?php require_once "HTML/QuickForm.php" ; require_once 'HTML/QuickForm/Renderer/ArraySmarty.php' ; require_once 'Smarty.class.php' ; $form = new HTML_QuickForm ( 'frmTest' , 'get' ) ; $form -> addElement ( 'header' , 'hdrTesting' , ' Smarty' ) ; $form -> addElement ( 'text' , 'txtFirstName' , '?' ) ; $form -> addElement ( 'text' , 'txtLastName' , '?' ) ; $form -> addElement ( 'text' , 'txtAge' , '?' ) ; $form -> addElement ( 'text' , 'txtTelephone' , ' ?' ) ; $form -> addElement ( 'reset' , 'btnClear' , '' ) ; $form -> addElement ( 'submit' , 'btnSubmit' , '' ) ; if ( $form -> validate ( ) ) { # , $form -> freeze ( ) ; } // $tpl =& new Smarty ; $tpl -> template_dir = '.' ; $tpl -> compile_dir = '/tmp' ; // $renderer =& new HTML_QuickForm_Renderer_ArraySmarty ( $tpl ) ; // HTML $form -> accept ( $renderer ) ; // $tpl -> assign ( 'form_data' , $renderer -> toArray ( ) ) ; // $tpl -> display ( 'smarty1.tpl' ) ; ?>
  11. <?php require_once "HTML/QuickForm.php" ; require_once 'HTML/QuickForm/Renderer/ArraySmarty.php' ; require_once 'Smarty.class.php' ; $form = new HTML_QuickForm ( 'frmTest' , 'get' ) ; $form -> addElement ( 'header' , 'hdrTesting' , ' Smarty' ) ; $form -> addElement ( 'text' , 'txtFirstName' , '?' ) ; $form -> addElement ( 'text' , 'txtLastName' , '?' ) ; $form -> addElement ( 'text' , 'txtAge' , '?' ) ; $form -> addElement ( 'text' , 'txtTelephone' , ' ?' ) ; $form -> addElement ( 'reset' , 'btnClear' , '' ) ; $form -> addElement ( 'submit' , 'btnSubmit' , '' ) ; if ( $form -> validate ( ) ) { # , $form -> freeze ( ) ; } // $tpl =& new Smarty ; $tpl -> template_dir = '.' ; $tpl -> compile_dir = '/tmp' ; // $renderer =& new HTML_QuickForm_Renderer_ArraySmarty ( $tpl ) ; // HTML $form -> accept ( $renderer ) ; // $tpl -> assign ( 'form_data' , $renderer -> toArray ( ) ) ; // $tpl -> display ( 'smarty1.tpl' ) ; ?>
  12. <?php require_once "HTML/QuickForm.php" ; require_once 'HTML/QuickForm/Renderer/ArraySmarty.php' ; require_once 'Smarty.class.php' ; $form = new HTML_QuickForm ( 'frmTest' , 'get' ) ; $form -> addElement ( 'header' , 'hdrTesting' , ' Smarty' ) ; $form -> addElement ( 'text' , 'txtFirstName' , '?' ) ; $form -> addElement ( 'text' , 'txtLastName' , '?' ) ; $form -> addElement ( 'text' , 'txtAge' , '?' ) ; $form -> addElement ( 'text' , 'txtTelephone' , ' ?' ) ; $form -> addElement ( 'reset' , 'btnClear' , '' ) ; $form -> addElement ( 'submit' , 'btnSubmit' , '' ) ; if ( $form -> validate ( ) ) { # , $form -> freeze ( ) ; } // $tpl =& new Smarty ; $tpl -> template_dir = '.' ; $tpl -> compile_dir = '/tmp' ; // $renderer =& new HTML_QuickForm_Renderer_ArraySmarty ( $tpl ) ; // HTML $form -> accept ( $renderer ) ; // $tpl -> assign ( 'form_data' , $renderer -> toArray ( ) ) ; // $tpl -> display ( 'smarty1.tpl' ) ; ?>
  13. <?php require_once "HTML/QuickForm.php" ; require_once 'HTML/QuickForm/Renderer/ArraySmarty.php' ; require_once 'Smarty.class.php' ; $form = new HTML_QuickForm ( 'frmTest' , 'get' ) ; $form -> addElement ( 'header' , 'hdrTesting' , ' Smarty' ) ; $form -> addElement ( 'text' , 'txtFirstName' , '?' ) ; $form -> addElement ( 'text' , 'txtLastName' , '?' ) ; $form -> addElement ( 'text' , 'txtAge' , '?' ) ; $form -> addElement ( 'text' , 'txtTelephone' , ' ?' ) ; $form -> addElement ( 'reset' , 'btnClear' , '' ) ; $form -> addElement ( 'submit' , 'btnSubmit' , '' ) ; if ( $form -> validate ( ) ) { # , $form -> freeze ( ) ; } // $tpl =& new Smarty ; $tpl -> template_dir = '.' ; $tpl -> compile_dir = '/tmp' ; // $renderer =& new HTML_QuickForm_Renderer_ArraySmarty ( $tpl ) ; // HTML $form -> accept ( $renderer ) ; // $tpl -> assign ( 'form_data' , $renderer -> toArray ( ) ) ; // $tpl -> display ( 'smarty1.tpl' ) ; ?>
  14. <?php require_once "HTML/QuickForm.php" ; require_once 'HTML/QuickForm/Renderer/ArraySmarty.php' ; require_once 'Smarty.class.php' ; $form = new HTML_QuickForm ( 'frmTest' , 'get' ) ; $form -> addElement ( 'header' , 'hdrTesting' , ' Smarty' ) ; $form -> addElement ( 'text' , 'txtFirstName' , '?' ) ; $form -> addElement ( 'text' , 'txtLastName' , '?' ) ; $form -> addElement ( 'text' , 'txtAge' , '?' ) ; $form -> addElement ( 'text' , 'txtTelephone' , ' ?' ) ; $form -> addElement ( 'reset' , 'btnClear' , '' ) ; $form -> addElement ( 'submit' , 'btnSubmit' , '' ) ; if ( $form -> validate ( ) ) { # , $form -> freeze ( ) ; } // $tpl =& new Smarty ; $tpl -> template_dir = '.' ; $tpl -> compile_dir = '/tmp' ; // $renderer =& new HTML_QuickForm_Renderer_ArraySmarty ( $tpl ) ; // HTML $form -> accept ( $renderer ) ; // $tpl -> assign ( 'form_data' , $renderer -> toArray ( ) ) ; // $tpl -> display ( 'smarty1.tpl' ) ; ?>
  15. <?php require_once "HTML/QuickForm.php" ; require_once 'HTML/QuickForm/Renderer/ArraySmarty.php' ; require_once 'Smarty.class.php' ; $form = new HTML_QuickForm ( 'frmTest' , 'get' ) ; $form -> addElement ( 'header' , 'hdrTesting' , ' Smarty' ) ; $form -> addElement ( 'text' , 'txtFirstName' , '?' ) ; $form -> addElement ( 'text' , 'txtLastName' , '?' ) ; $form -> addElement ( 'text' , 'txtAge' , '?' ) ; $form -> addElement ( 'text' , 'txtTelephone' , ' ?' ) ; $form -> addElement ( 'reset' , 'btnClear' , '' ) ; $form -> addElement ( 'submit' , 'btnSubmit' , '' ) ; if ( $form -> validate ( ) ) { # , $form -> freeze ( ) ; } // $tpl =& new Smarty ; $tpl -> template_dir = '.' ; $tpl -> compile_dir = '/tmp' ; // $renderer =& new HTML_QuickForm_Renderer_ArraySmarty ( $tpl ) ; // HTML $form -> accept ( $renderer ) ; // $tpl -> assign ( 'form_data' , $renderer -> toArray ( ) ) ; // $tpl -> display ( 'smarty1.tpl' ) ; ?>
  16. <?php require_once "HTML/QuickForm.php" ; require_once 'HTML/QuickForm/Renderer/ArraySmarty.php' ; require_once 'Smarty.class.php' ; $form = new HTML_QuickForm ( 'frmTest' , 'get' ) ; $form -> addElement ( 'header' , 'hdrTesting' , ' Smarty' ) ; $form -> addElement ( 'text' , 'txtFirstName' , '?' ) ; $form -> addElement ( 'text' , 'txtLastName' , '?' ) ; $form -> addElement ( 'text' , 'txtAge' , '?' ) ; $form -> addElement ( 'text' , 'txtTelephone' , ' ?' ) ; $form -> addElement ( 'reset' , 'btnClear' , '' ) ; $form -> addElement ( 'submit' , 'btnSubmit' , '' ) ; if ( $form -> validate ( ) ) { # , $form -> freeze ( ) ; } // $tpl =& new Smarty ; $tpl -> template_dir = '.' ; $tpl -> compile_dir = '/tmp' ; // $renderer =& new HTML_QuickForm_Renderer_ArraySmarty ( $tpl ) ; // HTML $form -> accept ( $renderer ) ; // $tpl -> assign ( 'form_data' , $renderer -> toArray ( ) ) ; // $tpl -> display ( 'smarty1.tpl' ) ; ?>
  17. <?php require_once "HTML/QuickForm.php" ; require_once 'HTML/QuickForm/Renderer/ArraySmarty.php' ; require_once 'Smarty.class.php' ; $form = new HTML_QuickForm ( 'frmTest' , 'get' ) ; $form -> addElement ( 'header' , 'hdrTesting' , ' Smarty' ) ; $form -> addElement ( 'text' , 'txtFirstName' , '?' ) ; $form -> addElement ( 'text' , 'txtLastName' , '?' ) ; $form -> addElement ( 'text' , 'txtAge' , '?' ) ; $form -> addElement ( 'text' , 'txtTelephone' , ' ?' ) ; $form -> addElement ( 'reset' , 'btnClear' , '' ) ; $form -> addElement ( 'submit' , 'btnSubmit' , '' ) ; if ( $form -> validate ( ) ) { # , $form -> freeze ( ) ; } // $tpl =& new Smarty ; $tpl -> template_dir = '.' ; $tpl -> compile_dir = '/tmp' ; // $renderer =& new HTML_QuickForm_Renderer_ArraySmarty ( $tpl ) ; // HTML $form -> accept ( $renderer ) ; // $tpl -> assign ( 'form_data' , $renderer -> toArray ( ) ) ; // $tpl -> display ( 'smarty1.tpl' ) ; ?>
  18. <?php require_once "HTML/QuickForm.php" ; require_once 'HTML/QuickForm/Renderer/ArraySmarty.php' ; require_once 'Smarty.class.php' ; $form = new HTML_QuickForm ( 'frmTest' , 'get' ) ; $form -> addElement ( 'header' , 'hdrTesting' , ' Smarty' ) ; $form -> addElement ( 'text' , 'txtFirstName' , '?' ) ; $form -> addElement ( 'text' , 'txtLastName' , '?' ) ; $form -> addElement ( 'text' , 'txtAge' , '?' ) ; $form -> addElement ( 'text' , 'txtTelephone' , ' ?' ) ; $form -> addElement ( 'reset' , 'btnClear' , '' ) ; $form -> addElement ( 'submit' , 'btnSubmit' , '' ) ; if ( $form -> validate ( ) ) { # , $form -> freeze ( ) ; } // $tpl =& new Smarty ; $tpl -> template_dir = '.' ; $tpl -> compile_dir = '/tmp' ; // $renderer =& new HTML_QuickForm_Renderer_ArraySmarty ( $tpl ) ; // HTML $form -> accept ( $renderer ) ; // $tpl -> assign ( 'form_data' , $renderer -> toArray ( ) ) ; // $tpl -> display ( 'smarty1.tpl' ) ; ?>
  19. <?php require_once "HTML/QuickForm.php" ; require_once 'HTML/QuickForm/Renderer/ArraySmarty.php' ; require_once 'Smarty.class.php' ; $form = new HTML_QuickForm ( 'frmTest' , 'get' ) ; $form -> addElement ( 'header' , 'hdrTesting' , ' Smarty' ) ; $form -> addElement ( 'text' , 'txtFirstName' , '?' ) ; $form -> addElement ( 'text' , 'txtLastName' , '?' ) ; $form -> addElement ( 'text' , 'txtAge' , '?' ) ; $form -> addElement ( 'text' , 'txtTelephone' , ' ?' ) ; $form -> addElement ( 'reset' , 'btnClear' , '' ) ; $form -> addElement ( 'submit' , 'btnSubmit' , '' ) ; if ( $form -> validate ( ) ) { # , $form -> freeze ( ) ; } // $tpl =& new Smarty ; $tpl -> template_dir = '.' ; $tpl -> compile_dir = '/tmp' ; // $renderer =& new HTML_QuickForm_Renderer_ArraySmarty ( $tpl ) ; // HTML $form -> accept ( $renderer ) ; // $tpl -> assign ( 'form_data' , $renderer -> toArray ( ) ) ; // $tpl -> display ( 'smarty1.tpl' ) ; ?>
  20. <?php require_once "HTML/QuickForm.php" ; require_once 'HTML/QuickForm/Renderer/ArraySmarty.php' ; require_once 'Smarty.class.php' ; $form = new HTML_QuickForm ( 'frmTest' , 'get' ) ; $form -> addElement ( 'header' , 'hdrTesting' , ' Smarty' ) ; $form -> addElement ( 'text' , 'txtFirstName' , '?' ) ; $form -> addElement ( 'text' , 'txtLastName' , '?' ) ; $form -> addElement ( 'text' , 'txtAge' , '?' ) ; $form -> addElement ( 'text' , 'txtTelephone' , ' ?' ) ; $form -> addElement ( 'reset' , 'btnClear' , '' ) ; $form -> addElement ( 'submit' , 'btnSubmit' , '' ) ; if ( $form -> validate ( ) ) { # , $form -> freeze ( ) ; } // $tpl =& new Smarty ; $tpl -> template_dir = '.' ; $tpl -> compile_dir = '/tmp' ; // $renderer =& new HTML_QuickForm_Renderer_ArraySmarty ( $tpl ) ; // HTML $form -> accept ( $renderer ) ; // $tpl -> assign ( 'form_data' , $renderer -> toArray ( ) ) ; // $tpl -> display ( 'smarty1.tpl' ) ; ?>
  21. <?php require_once "HTML/QuickForm.php" ; require_once 'HTML/QuickForm/Renderer/ArraySmarty.php' ; require_once 'Smarty.class.php' ; $form = new HTML_QuickForm ( 'frmTest' , 'get' ) ; $form -> addElement ( 'header' , 'hdrTesting' , ' Smarty' ) ; $form -> addElement ( 'text' , 'txtFirstName' , '?' ) ; $form -> addElement ( 'text' , 'txtLastName' , '?' ) ; $form -> addElement ( 'text' , 'txtAge' , '?' ) ; $form -> addElement ( 'text' , 'txtTelephone' , ' ?' ) ; $form -> addElement ( 'reset' , 'btnClear' , '' ) ; $form -> addElement ( 'submit' , 'btnSubmit' , '' ) ; if ( $form -> validate ( ) ) { # , $form -> freeze ( ) ; } // $tpl =& new Smarty ; $tpl -> template_dir = '.' ; $tpl -> compile_dir = '/tmp' ; // $renderer =& new HTML_QuickForm_Renderer_ArraySmarty ( $tpl ) ; // HTML $form -> accept ( $renderer ) ; // $tpl -> assign ( 'form_data' , $renderer -> toArray ( ) ) ; // $tpl -> display ( 'smarty1.tpl' ) ; ?>
  22. <?php require_once "HTML/QuickForm.php" ; require_once 'HTML/QuickForm/Renderer/ArraySmarty.php' ; require_once 'Smarty.class.php' ; $form = new HTML_QuickForm ( 'frmTest' , 'get' ) ; $form -> addElement ( 'header' , 'hdrTesting' , ' Smarty' ) ; $form -> addElement ( 'text' , 'txtFirstName' , '?' ) ; $form -> addElement ( 'text' , 'txtLastName' , '?' ) ; $form -> addElement ( 'text' , 'txtAge' , '?' ) ; $form -> addElement ( 'text' , 'txtTelephone' , ' ?' ) ; $form -> addElement ( 'reset' , 'btnClear' , '' ) ; $form -> addElement ( 'submit' , 'btnSubmit' , '' ) ; if ( $form -> validate ( ) ) { # , $form -> freeze ( ) ; } // $tpl =& new Smarty ; $tpl -> template_dir = '.' ; $tpl -> compile_dir = '/tmp' ; // $renderer =& new HTML_QuickForm_Renderer_ArraySmarty ( $tpl ) ; // HTML $form -> accept ( $renderer ) ; // $tpl -> assign ( 'form_data' , $renderer -> toArray ( ) ) ; // $tpl -> display ( 'smarty1.tpl' ) ; ?>
  23. <?php require_once "HTML/QuickForm.php" ; require_once 'HTML/QuickForm/Renderer/ArraySmarty.php' ; require_once 'Smarty.class.php' ; $form = new HTML_QuickForm ( 'frmTest' , 'get' ) ; $form -> addElement ( 'header' , 'hdrTesting' , ' Smarty' ) ; $form -> addElement ( 'text' , 'txtFirstName' , '?' ) ; $form -> addElement ( 'text' , 'txtLastName' , '?' ) ; $form -> addElement ( 'text' , 'txtAge' , '?' ) ; $form -> addElement ( 'text' , 'txtTelephone' , ' ?' ) ; $form -> addElement ( 'reset' , 'btnClear' , '' ) ; $form -> addElement ( 'submit' , 'btnSubmit' , '' ) ; if ( $form -> validate ( ) ) { # , $form -> freeze ( ) ; } // $tpl =& new Smarty ; $tpl -> template_dir = '.' ; $tpl -> compile_dir = '/tmp' ; // $renderer =& new HTML_QuickForm_Renderer_ArraySmarty ( $tpl ) ; // HTML $form -> accept ( $renderer ) ; // $tpl -> assign ( 'form_data' , $renderer -> toArray ( ) ) ; // $tpl -> display ( 'smarty1.tpl' ) ; ?>
  24. <?php require_once "HTML/QuickForm.php" ; require_once 'HTML/QuickForm/Renderer/ArraySmarty.php' ; require_once 'Smarty.class.php' ; $form = new HTML_QuickForm ( 'frmTest' , 'get' ) ; $form -> addElement ( 'header' , 'hdrTesting' , ' Smarty' ) ; $form -> addElement ( 'text' , 'txtFirstName' , '?' ) ; $form -> addElement ( 'text' , 'txtLastName' , '?' ) ; $form -> addElement ( 'text' , 'txtAge' , '?' ) ; $form -> addElement ( 'text' , 'txtTelephone' , ' ?' ) ; $form -> addElement ( 'reset' , 'btnClear' , '' ) ; $form -> addElement ( 'submit' , 'btnSubmit' , '' ) ; if ( $form -> validate ( ) ) { # , $form -> freeze ( ) ; } // $tpl =& new Smarty ; $tpl -> template_dir = '.' ; $tpl -> compile_dir = '/tmp' ; // $renderer =& new HTML_QuickForm_Renderer_ArraySmarty ( $tpl ) ; // HTML $form -> accept ( $renderer ) ; // $tpl -> assign ( 'form_data' , $renderer -> toArray ( ) ) ; // $tpl -> display ( 'smarty1.tpl' ) ; ?>
  25. <?php require_once "HTML/QuickForm.php" ; require_once 'HTML/QuickForm/Renderer/ArraySmarty.php' ; require_once 'Smarty.class.php' ; $form = new HTML_QuickForm ( 'frmTest' , 'get' ) ; $form -> addElement ( 'header' , 'hdrTesting' , ' Smarty' ) ; $form -> addElement ( 'text' , 'txtFirstName' , '?' ) ; $form -> addElement ( 'text' , 'txtLastName' , '?' ) ; $form -> addElement ( 'text' , 'txtAge' , '?' ) ; $form -> addElement ( 'text' , 'txtTelephone' , ' ?' ) ; $form -> addElement ( 'reset' , 'btnClear' , '' ) ; $form -> addElement ( 'submit' , 'btnSubmit' , '' ) ; if ( $form -> validate ( ) ) { # , $form -> freeze ( ) ; } // $tpl =& new Smarty ; $tpl -> template_dir = '.' ; $tpl -> compile_dir = '/tmp' ; // $renderer =& new HTML_QuickForm_Renderer_ArraySmarty ( $tpl ) ; // HTML $form -> accept ( $renderer ) ; // $tpl -> assign ( 'form_data' , $renderer -> toArray ( ) ) ; // $tpl -> display ( 'smarty1.tpl' ) ; ?>
  26. <?php require_once "HTML/QuickForm.php" ; require_once 'HTML/QuickForm/Renderer/ArraySmarty.php' ; require_once 'Smarty.class.php' ; $form = new HTML_QuickForm ( 'frmTest' , 'get' ) ; $form -> addElement ( 'header' , 'hdrTesting' , ' Smarty' ) ; $form -> addElement ( 'text' , 'txtFirstName' , '?' ) ; $form -> addElement ( 'text' , 'txtLastName' , '?' ) ; $form -> addElement ( 'text' , 'txtAge' , '?' ) ; $form -> addElement ( 'text' , 'txtTelephone' , ' ?' ) ; $form -> addElement ( 'reset' , 'btnClear' , '' ) ; $form -> addElement ( 'submit' , 'btnSubmit' , '' ) ; if ( $form -> validate ( ) ) { # , $form -> freeze ( ) ; } // $tpl =& new Smarty ; $tpl -> template_dir = '.' ; $tpl -> compile_dir = '/tmp' ; // $renderer =& new HTML_QuickForm_Renderer_ArraySmarty ( $tpl ) ; // HTML $form -> accept ( $renderer ) ; // $tpl -> assign ( 'form_data' , $renderer -> toArray ( ) ) ; // $tpl -> display ( 'smarty1.tpl' ) ; ?>
  27. <?php require_once "HTML/QuickForm.php" ; require_once 'HTML/QuickForm/Renderer/ArraySmarty.php' ; require_once 'Smarty.class.php' ; $form = new HTML_QuickForm ( 'frmTest' , 'get' ) ; $form -> addElement ( 'header' , 'hdrTesting' , ' Smarty' ) ; $form -> addElement ( 'text' , 'txtFirstName' , '?' ) ; $form -> addElement ( 'text' , 'txtLastName' , '?' ) ; $form -> addElement ( 'text' , 'txtAge' , '?' ) ; $form -> addElement ( 'text' , 'txtTelephone' , ' ?' ) ; $form -> addElement ( 'reset' , 'btnClear' , '' ) ; $form -> addElement ( 'submit' , 'btnSubmit' , '' ) ; if ( $form -> validate ( ) ) { # , $form -> freeze ( ) ; } // $tpl =& new Smarty ; $tpl -> template_dir = '.' ; $tpl -> compile_dir = '/tmp' ; // $renderer =& new HTML_QuickForm_Renderer_ArraySmarty ( $tpl ) ; // HTML $form -> accept ( $renderer ) ; // $tpl -> assign ( 'form_data' , $renderer -> toArray ( ) ) ; // $tpl -> display ( 'smarty1.tpl' ) ; ?>
  28. <?php require_once "HTML/QuickForm.php" ; require_once 'HTML/QuickForm/Renderer/ArraySmarty.php' ; require_once 'Smarty.class.php' ; $form = new HTML_QuickForm ( 'frmTest' , 'get' ) ; $form -> addElement ( 'header' , 'hdrTesting' , ' Smarty' ) ; $form -> addElement ( 'text' , 'txtFirstName' , '?' ) ; $form -> addElement ( 'text' , 'txtLastName' , '?' ) ; $form -> addElement ( 'text' , 'txtAge' , '?' ) ; $form -> addElement ( 'text' , 'txtTelephone' , ' ?' ) ; $form -> addElement ( 'reset' , 'btnClear' , '' ) ; $form -> addElement ( 'submit' , 'btnSubmit' , '' ) ; if ( $form -> validate ( ) ) { # , $form -> freeze ( ) ; } // $tpl =& new Smarty ; $tpl -> template_dir = '.' ; $tpl -> compile_dir = '/tmp' ; // $renderer =& new HTML_QuickForm_Renderer_ArraySmarty ( $tpl ) ; // HTML $form -> accept ( $renderer ) ; // $tpl -> assign ( 'form_data' , $renderer -> toArray ( ) ) ; // $tpl -> display ( 'smarty1.tpl' ) ; ?>
  29. <?php require_once "HTML/QuickForm.php" ; require_once 'HTML/QuickForm/Renderer/ArraySmarty.php' ; require_once 'Smarty.class.php' ; $form = new HTML_QuickForm ( 'frmTest' , 'get' ) ; $form -> addElement ( 'header' , 'hdrTesting' , ' Smarty' ) ; $form -> addElement ( 'text' , 'txtFirstName' , '?' ) ; $form -> addElement ( 'text' , 'txtLastName' , '?' ) ; $form -> addElement ( 'text' , 'txtAge' , '?' ) ; $form -> addElement ( 'text' , 'txtTelephone' , ' ?' ) ; $form -> addElement ( 'reset' , 'btnClear' , '' ) ; $form -> addElement ( 'submit' , 'btnSubmit' , '' ) ; if ( $form -> validate ( ) ) { # , $form -> freeze ( ) ; } // $tpl =& new Smarty ; $tpl -> template_dir = '.' ; $tpl -> compile_dir = '/tmp' ; // $renderer =& new HTML_QuickForm_Renderer_ArraySmarty ( $tpl ) ; // HTML $form -> accept ( $renderer ) ; // $tpl -> assign ( 'form_data' , $renderer -> toArray ( ) ) ; // $tpl -> display ( 'smarty1.tpl' ) ; ?>




We also need a template file, which is shown below. The file should be placed in the same folder as the PHP file presented above.







The form shown by the code above, of course, will not receive a prize for the best design, but it looks much better than in the previous example.



If you have problems with this, check the following:



  1. Do the file names match what is written in this article?
  2. Are both files in the same folder?
  3. Does your web server process have file reading permissions?
  4. Does the web server process have permissions to write to the / tmp folder?


Step by step: PHP source code



Note the following differences from the first version:



Let's analyze the files starting with the PHP code. In addition to the additional lines of require_once , the first change was made when we introduced the Smarty template object:

  1. // Create a template object
  2. $ tpl = & new Smarty ;
  3. $ tpl -> template_dir = '.' ;
  4. $ tpl -> compile_dir = '/ tmp' ;


After initialization, we tell the template object where it can find the template file (in the specific case it is the same folder) and in which folder the compiled version is to be downloaded (in the specific case it is the / tmp folder). You can use any folders if the web server process has read permissions to the template folder and permissions to write to the folder for the compiled version.

Next, we initialize the visualizer, passing the template object as a parameter:



  1. // Create a visualization object
  2. $ renderer = & new HTML_QuickForm_Renderer_ArraySmarty ( $ tpl ) ;




The next step is to create an array containing the form information and HTML fragments for each element. This is done in the following line:



  1. // Create HTML for the form
  2. $ form -> accept ( $ renderer ) ;




The next step is to pass the data array to the template:



  1. // assign the form data to the array
  2. $ tpl -> assign ( 'form_data' , $ renderer -> toArray ( ) ) ;




The assignment expression creates a form_data variable in the template and fills it with data from a previously defined array.

Finally, we call the display method of the template object, which parses the template file and replaces the data display points with direct data for these places taken from the array. Next HTML is displayed:



  1. // visualize and display the template
  2. $ tpl -> display ( 'smarty1.tpl' ) ;




Data array





Before we look at the template file, let's take a closer look at the dataset created by the visualizer. It consists of a number of elements, some of which are also arrays. The code further shows the structure of the array:







If we want to study this array, being filled with data, we can add an additional line to the PHP file, as shown below:







Now that we are familiar with the format of the data array, we can study the template file in detail.



Step by step: template file





The template file mainly consists of plain HTML and does not contain any interesting things for this article. However, the various tags and commands for Smarty are bracketed. Let's explore them.



The first {literal} command appears at the beginning of the file:







The {literal} tag, closed with the {/ literal} tag, simply means that the text that is between these two tags should be taken in the same form, should not be interpreted by the Smarty template. This is mandatory in this case, since the text is a style definition, which itself must use brackets. Without the {literal} tag, Smarty would interpret text-align: right; as a smarty tag.

The following command {$ form_data.header.hdrTesting} forms the text of the second level header. Calling it before, we passed a form data array to the template and then saved it to the form_data variable with the following line:



  1. $ tpl -> assign ( 'form_data' , $ renderer -> toArray ( ) ) ;




In this array there is an element called header , which is also an array, the keys of which are the names of the header elements and data components that compare the text of the header. From this it follows that the string {$ form_data.header.hdrTesting} represents the header text called hdrTesting , which was passed to the template in the form_data array.

The remaining tags in the template file simply take the different elements of the form_data array. Each form element other than headers has its own array in the form_data array, which means that, for example, the txtFirstName element can be represented in the form_data array as follows:



  1. [ "txtFirstName" ] =>
  2. array ( 8 ) {
  3. [ "name" ] =>
  4. string ( 12 ) "txtFirstName"
  5. [ "value" ] =>
  6. string ( 5 ) "Keith"
  7. [ "type" ] =>
  8. string ( 4 ) "text"
  9. [ "frozen" ] =>
  10. bool ( true )
  11. [ "label" ] =>
  12. string ( 11 ) "First name?"
  13. [ "required" ] =>
  14. bool ( false )
  15. [ "error" ] =>
  16. Null
  17. [ "html" ] =>
  18. string ( 62 ) "Keith"
  19. }




In this example, form_data.txtFirstName.label will be “First name?” And form_data.txtFirstName.html will be “Keith”.

Finally, the template may contain static information. In the example above, copyright information will always be displayed verbatim, as it is embedded in the template file as static HTML.



5. Intellectual processing of the template.





Introduction





Without trying to write a comprehensive guide to using Smarty templates (which was very successfully done on the Smarty website), in this part we will explore a few simple functions of Smarty templates that will make the templates much more powerful.



“If-then-else” type processing.





Smarty templates support if-then-else processing. We can modify the original template file as follows:







The variable $ form_data.frozen is a logical one that is equal to one if the form is frozen. When the form is displayed for the first time, the heading will be the value of the hdrTesting variable, as provided by the code from the PHP file. After the data has been submitted, the form will be “frozen” and displayed in red.



A more practical example would be the display of form buttons, if it is not frozen. This we can do as follows:







Note that {if} closes {/ if} . Smarty templates also support the {else} and {elseif} keywords, which work as expected.



Local variables.





You can embed local variables in the template. The following example sets the label style based on whether the form is frozen or not. Although this is not a very useful function, it demonstrates well the use of local variables. The template file is the same as before, with some changes:







Comments in the template.





Comments can be included in the template by placing them between {* and *} . Example:



  1. {* This is a comment *}




5. Practical considerations.





Introduction





The example above was compiled in a simple way to clearly show the things made. However, in real life, there are some additional things to add there. For example, in the example above, there was no tag for users which fields are required. There are also other points that we will have to take care of.



Hidden fields and javascript





Hidden fields are used in forms to store contextual information. Many forms use javascript to perform various functions. As you know, neither hidden fields nor JavaScript code will be displayed on the page, but this information is in an array that is passed to the template object. We will need to make the following changes in order to use this data:







Required fields





In the default visualizer, the required fields are marked with a red asterisk, and a note stating that these fields are required is placed at the bottom of the form.



When creating your own templates, we must include code to mark fields as required (if we need this functionality). Let's make our Age field mandatory by making the following changes to our PHP file.



  1. $ form -> addElement ( 'submit' , 'btnSubmit' , 'Submit' ) ;
  2. $ form -> addRule ( 'txtAge' , 'Age field is required' , 'required' ) ;
  3. if ( $ form -> validate ( ) ) {
  4. # If the form is verified, freeze the data
  5. $ form -> freeze ( ) ;
  6. }




After displaying this form, we will see that there is no mark on the form stating that the Age field is mandatory. And if we leave the Age field empty, the form will not send the data, but it will not tell us why. To prevent these problems, we will need to make some changes to the visualizer using the setRequiredTemplate method. It takes a template as a text line argument, although, of course, we can read the external template file using the PHP file_get_contents () function. For simplicity, we will directly specify the pattern.



The Smarty template recognizes 3 variables:







It will be clearer after familiarizing with the following example. Change the PHP code as shown below:







From the code above it can be seen that when an error occurs, the label of the element should be shown in red. If there is no error, display the shortcut as usual. And if it is a required field, place a small red star in it.

If we now display the form, we will see that the Age field has a small red star. And if we send the data, leaving the Age field empty, the form will appear with a red label for the Age field. However, the form does not tell us what the red asterisk means and because of what the data is not sent when the Age field is empty.



Error messages





Like the setRequiredTemplate method, the visualizer also has a setErrorTemplate method. In addition to the three Smarty variables for setRequiredTemplate , the setErrorTemplate method also supports variables. It contains the text of the error associated with the element that is defined in the second argument of the addRule method.



Next, change the PHP code as follows:







The code says that in case of an error, the error message should be shown in a small orange font. The element itself is always displayed. The whole effect is that all form elements will have orange error messages above them.



Finally, we need to explain to the user exactly what the red asterisk means. We do this by adding the requirednote field to the template. Modify the template file as follows:







This will display the standard text "requirednote", but we can change it in the PHP file itself.







6. Conclusions.





This guide only superficially presented what Smarty can do. However, most often the most difficult is the beginning, and I hope that this guide has helped you take this first step. The second useful step for you is to read the Smarty documentation in order to understand what else Smarty can do.

Source: https://habr.com/ru/post/68263/



All Articles