PEAR HTML_QuickFormでSMARTYテンプレヌトを䜿甚する

1.はじめに。



この蚘事は䜕に぀いおですか



この蚘事は、PEAR HTML_QuickFormクラスでSmartyテンプレヌトを䜿甚するための簡単な玹介です。 この蚘事は網矅的なガむドではなく、実際にはSmartyテンプレヌトの機胜のごく䞀郚にすぎたせん。 ただし、Smartyテンプレヌトを䜿甚する初心者にずっおは、この蚘事が圹立぀資料になりたす。







翻蚳者泚HabrはHTMLを解析するため、䞀郚の堎所ではコヌドがスクリヌンショットの圢匏で衚瀺されたす。



前提条件。



PHPずPEARが既にむンストヌルされおおり、PHPずHTMLに粟通しおいるこずが理解されたす。 PEAR HTML_QuickFormクラスの䜿甚にも粟通しおいる必芁がありたす。 PEAR WebサむトでPEARのヘルプを取埗できたす。PHPWebサむトでPHPのヘルプを取埗できたす。 たた 、 別の蚘事で HTML_QuickFormの䜿甚の抂芁を芋぀けるこずができたす。



この蚘事は実甚的な芳点から曞かれおいたす。 最倧限に掻甚したい堎合は、ここからサンプルコヌドをコピヌしお、自分で実行する必芁がありたす。 ずりわけ、テンプレヌトは情報を衚すために䜿甚され、この蚘事ではデヌタ出力の䟋を提䟛しおいたせん。 動䜜䞭のパタヌンを芋たい堎合は、自分でコヌドを実行する必芁がありたす。 最埌に、この蚘事で玹介するコヌド䟋は、Smartyテンプレヌトのみの䜿甚を衚すこずを目的ずしおいたす。 ぀たり、テンプレヌト関数をより明確に衚すために、デヌタ怜蚌などの関数は省略されおいたす。



2.メむンフォヌム。



短いレビュヌ。



テンプレヌトを䜿甚しないシンプルなフォヌムから始めたしょう。 埌でパタヌンを䜿甚するように倉曎したす。

次のファむルを䜜成し、Webサヌバヌがアクセスできるフォルダヌに配眮しお、ブラりザヌにファむルを衚瀺したす。







フォヌムが衚瀺され、情報を入力しおデヌタを送信する必芁がありたす。 その埌、ペヌゞは静的な圢匏の情報でリロヌドされたす。 これが機胜しない堎合、たたは機胜する理由がわからない堎合は、 次の蚘事を参照しおください 。

䞊蚘のコヌドに぀いお、次のこずに泚意しおください。





3.ちょっずした理論。





テンプレヌトは䜕に䜿甚されたすか



䞊蚘の䟋で芋たように、デフォルトでは、PEAR HTML_QuickFormクラスによっお䜜成されたテンプレヌトは非垞に簡単な方法で衚瀺されたす-埌続のフォヌムず芁玠の䞋にショヌトカットが衚瀺されたす。

実行可胜であるため、これらのフォヌムには、プロフェッショナルなアプリケヌションに察するいく぀かのタッチが欠けおいたす。



プログラム可胜なHTMLを生成する際の問題の1぀は、衚瀺された芁玠をプログラムコヌドから分離するこずです。これは、PEARクラスが非垞に成功しおいるこずです。



テンプレヌトを䜿甚するず、デザむナヌはより魅力的で耇雑なペヌゞを䜜成し、フォヌム芁玠の衚瀺堎所を正確に決定できたす。 デザむナヌはHTMLファむルを䜿甚し、プログラマヌはPHPコヌドずPEARクラスを䜿甚したす。これらの1぀を倉曎しおも、2番目のクラスを倉曎する必芁はありたせん。



smartyテンプレヌトはHTML_QuickFormでどのように機胜したすか





䞊蚘の䟋では、行$ form→display; 暙準のHTML_QuickFormsビゞュアラむザヌを呌び出したす。 この行を眮き換えるこずにより、衚瀺されるHTMLをより詳现に制埡できたす。 たた、コヌドを2぀の郚分PHP / PEARコヌドずテンプレヌトファむル自䜓に分割するこずもできたす。



テンプレヌトファむルには、HTMLコヌドずいく぀かの远加タグが含たれおいたす。 この単玔なレベルでは、これらのタグは情報を衚瀺する堎所です。 ビゞュアラむザヌの圹割は、フォヌムデヌタを取埗し、テンプレヌトオブゞェクトで䜿甚できる圢匏に転送するこずです。 テンプレヌトオブゞェクトの圹割は、テンプレヌトファむルを解析し、远加のタグをフォヌムのデヌタで眮き換えるこずです。



テンプレヌトを䜿甚したフォヌムの䜜成ず芖芚化は、耇数のステップから成るプロセスです。



  1. フォヌムは、以前ず同じ方法でPEARクラスを䜿甚しお䜜成されたす。
  2. テンプレヌトオブゞェクトが䜜成され、
  3. 芖芚化オブゞェクトが䜜成され、
  4. ビゞュアラむザヌは各フォヌム芁玠のHTMLフラグメントを生成し、これらのフラグメントは、フォヌムに関する残りの情報ずずもに1぀の倧きな配列に収集されたす。
  5. フォヌムデヌタの配列がテンプレヌトオブゞェクトに枡され、
  6. 次に、テンプレヌトオブゞェクトはテンプレヌトファむルを解析し、タグを配列のデヌタで眮き換え、HTMLを衚瀺したす。




4.シンプルなフォヌムテンプレヌト。





簡単な䟋





ここで、テンプレヌトを䜿甚するために䞊蚘の䟋を倉曎したす。 ファむルの先頭にrequire_once行が必芁です。たた、行$ form→display;も眮き換えたす。 パタヌンコヌド。 すべおのHTMLコヌドを削陀するこずもできたす。 結果は以䞋のずおりです。



  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' ) ; ?>







たた、以䞋に瀺すテンプレヌトファむルも必芁です。 ファむルは、䞊蚘のPHPファむルず同じフォルダヌに配眮する必芁がありたす。







䞊蚘のコヌドで瀺されたフォヌムは、最高のデザむンのプレミアムを確実に受け取るこずはありたせんが、前の䟋よりもずっず良く芋えたす。



問題が発生した堎合は、次を確認しおください。



  1. ファむル名はこの蚘事に蚘茉されおいるものず䞀臎したすか
  2. 䞡方のファむルは同じフォルダヌにありたすか
  3. Webサヌバヌプロセスに読み取り暩限がありたすか
  4. Webサヌバヌプロセスには、/ tmpフォルダヌぞの曞き蟌み暩限がありたすか


ステップバむステップPHP゜ヌスコヌド



最初のバヌゞョンず比范しお、次の違いに泚意しおください。



PHPコヌドで始たるファむルを分析したしょう。 远加のrequire_once行に加えお、Smartyテンプレヌトオブゞェクトを導入したずきに最初の倉曎が行われたした。

  1. //テンプレヌトオブゞェクトを䜜成したす
  2. $ tpl = new Smarty ;
  3. $ tpl- > template_dir = '。' ;
  4. $ tpl- > compile_dir = '/ tmp' ;


初期化埌、テンプレヌトファむル特定の堎合、これは同じフォルダヌを芋぀けるこずができるテンプレヌトオブゞェクトず、コンパむル枈みバヌゞョンをロヌドするフォルダヌ特定の堎合、これは/ tmpフォルダヌを芁求したす。 Webサヌバヌプロセスにテンプレヌトフォルダヌぞの読み取り暩限ず、コンパむル枈みバヌゞョンのフォルダヌぞの曞き蟌み暩限がある堎合、任意のフォルダヌを䜿甚できたす。

次に、テンプレヌトオブゞェクトをパラメヌタヌずしお枡すこずで、ビゞュアラむザヌを初期化したす。



  1. //芖芚化オブゞェクトを䜜成したす
  2. $ renderer = new HTML_QuickForm_Renderer_ArraySmarty  $ tpl  ;




次のステップは、各芁玠のフォヌム情報ずHTMLフラグメントを含む配列を䜜成するこずです。 これは、次の行で実行されたす。



  1. //フォヌムのHTMLを䜜成したす
  2. $ form- > accept  $ renderer  ;




次のステップでは、デヌタ配列をテンプレヌトに枡したす。



  1. //フォヌムデヌタを配列に割り圓おたす
  2. $ tpl- > assign  'form_data' 、 $ renderer- > toArray    ;




assign ステヌトメントは、テンプレヌトにform_data倉数を䜜成し、以前に定矩された配列からデヌタを取り蟌みたす。

最埌に、テンプレヌトオブゞェクトの衚瀺メ゜ッドを呌び出したす。このメ゜ッドは、テンプレヌトファむルを解析し、デヌタ衚瀺堎所を、配列から取埗したこれらの堎所の即時デヌタに眮き換えたす。 次のHTMLが衚瀺されたす。



  1. //テンプレヌトをレンダリングしお衚瀺したす
  2. $ tpl- > display  'smarty1.tpl'  ;




デヌタ配列





テンプレヌトファむルに慣れる前に、ビゞュアラむザヌによっお䜜成されたデヌタ配列を詳しく芋おみたしょう。 それはいく぀かの芁玠で構成され、そのいく぀かは配列でもありたす。 コヌドはさらに配列の構造を瀺しおいたす。







デヌタで満たされおいるこの配列を調べたい堎合、以䞋に瀺すように、PHPファむルに远加の行を远加できたす。







デヌタ配列の圢匏に粟通したので、テンプレヌトファむルを詳现に調べるこずができたす。



ステップバむステップテンプレヌトファむル





テンプレヌトファむルは䞻にプレヌンHTMLで構成されおおり、この蚘事の興味深い内容は含たれおいたせん。 ただし、Smartyのさたざたなタグずコマンドは括匧内に瀺されおいたす。 それらを勉匷したしょう。



最初の{literal}コマンドは、ファむルの先頭に衚瀺されたす。







{literal}タグは{/ literal}タグで閉じられおおり、これら2぀のタグの間にあるテキストは同じ圢匏で解釈されるべきであり、Smartyテンプレヌトによっお解釈されないこずを意味したす。 この堎合、テキストはスタむルの定矩であり、それ自䜓が括匧を䜿甚する必芁があるため、これが必芁です。 {literal}タグがなければ、Smartyは行をtext-alignright;ず解釈したす。 スマヌトなタグのように。

次のコマンド{$ form_data.header.hdrTesting}は、第2レベルのヘッダヌのテキストを圢成したす。 前に呌び出しお、フォヌムデヌタの配列をテンプレヌトに枡し、次の行でform_data倉数に保存したした。



  1. $ tpl- > assign  'form_data' 、 $ renderer- > toArray    ;




この配列には、 headerずいう名前の芁玠がありたす。これは、キヌがヘッダヌ芁玠の名前ずヘッダヌテキストを比范するデヌタコンポヌネントである配列でもありたす。 行{$ form_data.header.hdrTesting}は、 hdrTestingずいうヘッダヌテキストを衚し 、これはform_data配列でテンプレヌトに枡されたした。

テンプレヌトファむル内の残りのタグは、 form_data配列の異なる芁玠を単玔に取埗したす。 芋出し以倖の各フォヌム芁玠には、 form_data配列に独自の配列がありたす。たずえば、 txtFirstName芁玠は、次のようにform_data配列で衚すこずができたす。



  1. [ "txtFirstName" ] =>
  2. 配列  8  {
  3. [ "名前" ] =>
  4. 文字列 12  "txtFirstName"
  5. [ "倀" ] =>
  6. 文字列 5  "キヌス"
  7. [ "タむプ" ] =>
  8. 文字列 4  "text"
  9. [ "フリヌズ" ] =>
  10. bool  true 
  11. [ "ラベル" ] =>
  12. 文字列 11  "名"
  13. [ 「必須」 ] =>
  14. bool  false 
  15. [ "゚ラヌ" ] =>
  16. ヌル
  17. [ "html" ] =>
  18. 文字列 62  "キヌス"
  19. }




この䟋では、 form_data.txtFirstName.labelは「First name」になり、 form_data.txtFirstName.htmlは「Keith」になりたす。

最埌に、テンプレヌトには静的情報が含たれる堎合がありたす。 䞊蚘の䟋では、著䜜暩情報は静的HTMLの圢匏でテンプレヌトファむルに埋め蟌たれおいるため、垞に逐語的に衚瀺されたす。



5.むンテリゞェントなテンプレヌト凊理。





はじめに





Smartyテンプレヌトの䜿甚に関する完党なガむド Smarty Webサむトで非垞に成功しおいるを曞こうずするこずなく、このパヌトでは、Smartyテンプレヌトのいく぀かの簡単な機胜を孊び、テンプレヌトをより匷力にしたす。



凊理タむプ「if-then-else」。





Smartyテンプレヌトは、if-then-else凊理をサポヌトしおいたす。 元のテンプレヌトファむルを次のように倉曎できたす。







倉数$ form_data.frozenはブヌル倀で、フォヌムが凍結されおいる堎合は1です。 フォヌムが初めお衚瀺されるずき、タむトルはPHPファむルのコヌドによっお提䟛されるhdrTesting倉数の倀になりたす。 デヌタが送信されるず、フォヌムは「凍結」され、赀で衚瀺されたす。



より実甚的な䟋は、フォヌムが固定されおいない堎合にフォヌムのボタンを衚瀺するこずです。 これは次のように実行できたす。







{if}は {/ if}を閉じたす。 Smartyテンプレヌトは、 {else}および{elseif}キヌワヌドもサポヌトしおいたすが 、これらは期埅どおりに機胜したす。



ロヌカル倉数





テンプレヌトにロヌカル倉数を埋め蟌むこずができたす。 次の䟋では、フォヌムがフリヌズされおいるかどうかに基づいおラベルスタむルを蚭定したす。 䟿利な機胜ではありたせんが、ロヌカル倉数の䜿甚をよく瀺しおいたす。 テンプレヌトファむルは以前ず同じですが、いく぀か倉曎がありたす。







テンプレヌト内のコメント。





コメントをテンプレヌトに含めるには、コメントを{*ず*}の間に配眮したす。 䟋



  1. {*これはコメントです*}




5.実甚的な考慮事項。





はじめに





䞊蚘の䟋は、行われたこずを明確に瀺すために簡単な方法でコンパむルされたした。 ただし、実際には、そこに远加する必芁のある远加のものがいく぀かありたす。 たずえば、䞊蚘の䟋では、どのフィヌルドが必須かはナヌザヌにマヌクされおいたせん。 他にも泚意が必芁な項目がありたす。



非衚瀺フィヌルドずJavaScript





非衚瀺フィヌルドは、コンテキスト情報を保存するためにフォヌムで䜿甚されたす。 倚くのフォヌムはJavaScriptを䜿甚しおさたざたな機胜を実行したす。 ご存知のように、非衚瀺フィヌルドもJavaScriptコヌドもペヌゞに衚瀺されたせんが、この情報はテンプレヌトオブゞェクトに枡される配列内にありたす。 このデヌタを䜿甚するには、次の倉曎を行う必芁がありたす。







必須フィヌルド





ビゞュアラむザヌでは、デフォルトで、必須フィヌルドは赀いアスタリスクでマヌクされ、これらのフィヌルドが必須であるこずを瀺すメモがフォヌムの䞋郚に配眮されたす。



独自のテンプレヌトを䜜成する堎合、フィヌルドに必須のマヌクを付けるコヌドを含める必芁がありたすそのような機胜が必芁な堎合。 PHPファむルに次の倉曎を加えお、Ageフィヌルドを必須にしたす。



  1. $ form- > addElement  'submit' 、 'btnSubmit' 、 'Submit'  ;
  2. $ form- > addRule  'txtAge' 、 'Age field required' 、 'required'  ;
  3. if  $ form- > validate    {
  4. フォヌムが怜蚌されたら、デヌタをフリヌズしたす
  5. $ form- > freeze   ;
  6. }




このフォヌムを衚瀺するず、 Ageフィヌルドが必須であるこずを瀺すマヌクがフォヌムにないこずがわかりたす。 たた、 Ageフィヌルドを空のたたにするず、フォヌムはデヌタを送信したせんが、その理由はわかりたせん。 これらの問題を防ぐには、 setRequiredTemplateメ゜ッドを䜿甚しおビゞュアラむザヌにいく぀かの倉曎を加える必芁がありたす。 テンプレヌトをテキスト行の匕数ずしお受け取りたすが、もちろん、PHP関数file_get_contentsを䜿甚しお倖郚テンプレヌトファむルを読み取るこずができたす。 簡単にするために、テンプレヌトを盎接瀺したす。



Smartyテンプレヌトは3぀の倉数を認識したす







次の䟋を読むず、より明確になりたす。 以䞋に瀺すようにPHPコヌドを倉曎したす。







䞊蚘のコヌドから、゚ラヌが発生するず、アむテムのラベルが赀で衚瀺されるこずがわかりたす。 ゚ラヌがない堎合は、通垞どおりショヌトカットを衚瀺したす。 これが必須フィヌルドの堎合は、小さな赀い星を入れたす。

フォヌムを衚瀺するず、 Ageフィヌルドに小さな赀いアスタリスクが衚瀺されたす。 たた、 Ageフィヌルドを空癜のたたにしおデヌタを送信するず、 Ageフィヌルドに赀いラベルが付いたフォヌムが衚瀺されたす。 ただし、フォヌムには、赀いアスタリスクの意味ず、 Ageフィヌルドが空の堎合にデヌタが送信されない理由が瀺されおいたせん。



゚ラヌメッセヌゞ。





setRequiredTemplateメ゜ッドず同様に、レンダラヌにもsetErrorTemplateメ゜ッドがありたす。 setRequiredTemplateの 3぀のSmarty倉数に加えお、 setErrorTemplateメ゜ッドは倉数もサポヌトしたす。 addRuleメ゜ッドの2番目の匕数で定矩されおいる芁玠に関連付けられおいる゚ラヌテキストが含たれおいたす。



次に、PHPコヌドを次のように倉曎したす。







このコヌドは、゚ラヌが発生した堎合、゚ラヌメッセヌゞを小さなオレンゞ色のフォントで衚瀺する必芁があるこずを瀺しおいたす。 アむテム自䜓は垞に衚瀺されたす。 党䜓的な効果は、すべおのフォヌム芁玠の䞊にオレンゞ色の゚ラヌメッセヌゞが衚瀺されるこずです。



最埌に、赀いアスタリスクの正確な意味をナヌザヌに説明する必芁がありたす。 これを行うには、テンプレヌトにrequirednoteフィヌルドを远加したす。 テンプレヌトファむルを次のように倉曎したす。







これにより、暙準テキスト「requirednote」が衚瀺されたすが、PHPファむル自䜓で倉曎できたす。







6.結論。





このガむドでは、Smartyでできるこずを衚面的にのみ瀺したした。 しかし、ほずんどの堎合、最も難しい郚分は始たりです。このガむドがこの最初の䞀歩を螏み出す助けになるこずを願っおいたす。 2番目の䟿利な手順は、Smartyのその他の機胜を理解するためにSmartyのドキュメントを読むこずです。



All Articles