Smarty instance tutorial - template design section -1

xiaoxiao2021-03-06  79

Smarty instance tutorial - template design section -1

Author: teacherlxj Document Type: Posted Visitor Statistics: 239 Source: 9cbs Time: 2004-08-22 08:38:31 Keywords: smarty, PHP template engine First, what is a smarty smarty is written using PHP? Template PHP template engine, which provides the separation of logic and external content, and simple speaking, the purpose is to use PHP programmers to separate, the logical content of the programmer to change the program is not affected by the artist page design. The US re-modify the page does not affect the program logic of the program, which is especially important in multiplayers. Second, Smarty Advantages:  1. Speed: Programs Writing with Smarty can improve the maximum speed, which is relative to other template engine technology.                                                                                                                                                   In the middle, no more template recompile (in the case where the source program is not changed)     cache technology: Smarty selects a cache technology, which can cache the user's final HTML file into a static HTML page When setting Smarty's Cache property is true, the user's Web request is directly converted to this static HTML file during Smarty, which is equivalent to calling a static HTML file.  . Plugin technology: Smarty can customize the plugin. The plugin is actually some custom functions.                                                                                                                     . The template file is used to use the judgment statement that can be easily reundrained to the template. Third, not suitable for using Smarty:   1. Requires real-time update content. For example, a stock display, it needs to be updated frequently, this type of program uses smarty to slow down the template processing speed.   small project. Small projects are quickly and programmers, using Smarty to lose the rapid advantages of PHP development because of the simple projects and programmers and one person. Fourth, install the Smarty class:   Installation Smarty's environment: PHP version 4.06 or later.   installation Smarty method is very simple, download Smarty.tar.gz from http://samrty.php.net, find the lib directory after decompression, create directory CommM in the current site, and all documents in LIB   To copy it into the comm directory, complete the basic installation. Please see the manual in other advanced installation methods. 5. SMARTY uses in the template:          This section takes several examples. The Smarty Template is usually used. TPL logo, some people write the extensions directly into .html for the convenience of the United States, and it is possible. This article uses a Smarty standard write: in .tpl to represent a smarty template.   Example 1: Let's take a simple example.

    ================================================================================================================================================================================ ====      ========================================= ============   {* Display is the use of the smarty variable identifier * contains text to comment content *     {include file = "header.tpl"} {* page *   Hello, my name is {$ name}, welcome everyone to read my smarty learning materials.                                  {**} is a template page comment, it is template on Smarty Do not perform any outputs when parsing, only for templates for annotation.    {include file = "xxx.tpl"} Use this sentence to include a template file to the current page, the example in the example will be included in the public.tpl with FOOT.TPL in the website, you can  Think, use this sentence to copy all of the contents in XXX.TPL at the current statement. Of course, you can also use this sentence, you can also copy the contents of XXX.TPL to the current statement.     {$ name}: Template variable, the core composition in Smarty, uses smarty defined left boundary {and right boundary}, given in the form of PHP variables, will be used in the Smarty program   $ smarty -> Assign ("Name", "Li Xiaojun"); replace $ Name in the template into three words of "Li Xiaojun".

 The entire example source program is as follows: ===================================================================================================================================================================================================== ==================   Master Smarty tutorial </ title>   </ head> <body> === ============================ foot.tpl ==================================================================================================================================================================== ============= <hr> <center> Copyright (c) by Master Email: teacherli@163.com </ center>    <HR>   Body> </ html>    ============================================= ==========   index.tpl   ====================================== ==================                  {include file = "header.tpl" } {* Page *                   {{$ name}, welcome everyone to read my smarty learning materials.</p> <p>                                                                                                                                    ====================== ===================  index.php             ============================================================================================================================================================================ ==========================? PHP / ******************* ************************* * * File Name: Index.php                                                                                                                                                   ********** ******************** /   incrude_once ("./ Comm / smarty.class.php"); // contains smarty class file   $ smarty = new Smarty ();                                                                                                                                                   Set the compilation directory      ----------------------------------------- -----------       {}, but the actual application is easy to conflict with JavaScript //, but it is recommended to set <{}> or others.</p> <p>  / ------------------------------------------------------------------------------ ------                                                                   Template variable replacement                                                              :        =================================   Execute index.php   ========== =======================   <html>                                                                                                                                    <body> Hello, my name is Li Xiaojun, welcome everyone to read my smarty learning materials. <Hr> <center> Copyright (c) BY Master Email: Teacherli@163.com </ center>   <HR>         This Example is an example of integrated use of Smarty Template parameters, these parameters are used to control the output of the template, I only select some of them, other parameters you will go see the reference.</p> <p>  ==================================================   exmple2.tpl   =============================================================================================================================================================================== ===  <HTML>   <head> <title> Master Smarty Example 2 </ title> </ head> <body>                                           capitalize} <br>  2. The second sentence template variables Li Xiaojun: {$ str2 | cat: "Li Xiaojun"} <br>  3. The third sentence of output current date: {$ str3 | date_format: " % Y year% M month% d day "} 4. The fourth sentence is not processed in the PHP program, it shows the default value: {$ str4 | default:" There is no value! "    5.</p> <p>The fifty sentence wants to indent the 8 blank alphanumeric, and use "*" to replace these 8 blank characters: <br>   {$ str5 | indent: 8: "*"}} <br>   6. The sixth sentence transforms all of the TEACHERLI@163.com: {$ str6 | lower} <br> 7. The seventh sentence replaces the Teacherli in the variable into: Li Xiaojun: {$ STR7 | Replace: "TEACHERLI": "Li Xiaojun"} <br> 8. The eighth sentence is a combination of variable modifiers: {$ str8 | Capitalize | Cat: "This is a new time:" | DATE_FORMAT: "% y year% Month% d day "| Lower}                                                                              ===================== =============================================================== ======== ===================== <? PHP / ******************************* ********************* * * * File name: example2.php *                                                         * Make] Master "* email:                                                                                                                                        *******************        onc c.class.php "); // contains the Smarty class file   $ smarty = new smarty ();                                                                     . / / --------------------------------- ------   // left and right boundary, default is {}, but the practical application is easy to conflict with JavaScript //, so it is recommended to set <{}> or other.</p> <p>  / ------------------------------------------------------------------------------ ------   $ smarty-> left_delimiter = "{smarty-> right_delimiter ="} "; $ smarty-> assign (" str1 "," My name is xiao jun, li "); // replace STR1 to my name is xiao jun, li. $ Smarty-> assign (" str2 "," My name is: "); // Out: My name is: Li Xiaojun  $ SMARTY-> Assign ("Str3", "AD"); // Output AD ("SMARTY-> Assign (" STR4 ",") ("Str4", ") (" str4 ","); // The default value is displayed when the fourth sentence is not processed. If you use the previous sentence, it is replaced with "" $ smarty-> assign ("str5", "8 * * *"); // Fifth sentence output: ******** 8 * $ smarty-> assign ("str6", "teacherli@163.com"); // will output teacherli@163.com                                                                                      ("Str7", "this is teacherli"); // Displayed in the template: this is Li Xiaojun ("str8", "str8", "here is combining:");   // Compile and display INDEX.TPL template under ./Templates              The final output:     =============== ================================================================================================================================================================================== =============================================================================================================================================================================================================   <html> <HEAD> <title> Master Smarty Example 2 </ title> </ head> <body>                                                                                                                                  Li . <</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-94455.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="94455" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.047</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = 'lj4DRSAh5aPDFH81XX537I_2FQVPbP5WJyG9JAr9Ky4h3HlwpLYbVPR2DzDysRV_2Fe1KsLqmJSo5Z_2FOWE13'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>