PHP template engine smarty

xiaoxiao2021-03-06  70

PHP template engine SMARTY Author: Yu Bo Xiang source: logic and presentation layers IBM China development model to achieve MVC with PHP There are many template engines to choose from, but after the official birth of engine SMARTY choose will have a change. Its ideas and implementations are quite "avant-garde". This article mainly discusses the different characteristics of Smarty to other template engines, briefly introduces the engine installation and use, and compares the speed and ease of use of Smarty and phpLib Template with a small test case. First, the MVC requires template MVC to be earliered in a design pattern summarized during the development of SmallTalk languages. The MVC represents "model", "view", "control", and the purpose is to let different development roles in large and medium-sized projects. All the Division of the Division. In the development of a network application, you can use the following figure to represent the relationship between the concepts.

The figure shows a simple web application that sees the information on the browser is the content on the database server, but before this, the application server is processed. The developer is responsible for establishing data structures, processing data logic, and methods representing data. When the CGI started popular in China, the early Web programmers started self-study materials from HTML, and the HTML of the Print in Perl was not a difficult thing, but with the network's step speed, the page size is also From the original two, thirty k rose ten times. Write a CGI program has an urgent requirement: Separate Perl and HTML source. Thus, social progress is reflected in the division of labor within the development team. Because the art and programmers are not very familiar with each other, they need to communicate with a conventional "language" during cooperation. This language is not our mother tongue or English, the term is called "template", logic and representation relying on it. It is an expression of HTML and scripting language features. In this way, the representation layer can display data processed through the logical layer according to the desired format of the user. If you have experience in the development of MFC under the Windows platform, you will be familiar with the package of Document / Document Template / View, which is a typical MVC example. For web applications, individuals think that EJB / servlets / JSP in J2EE is the most powerful, of course, simple and beautiful Structs. Another very famous implementation is COM / DCOM ASP, this combination is used in our country. Through several MVCs, you can get a concept of template: a set of scripts inserted into HTML or an insertion of the script HTML, which shows the changed data. An example of a template file is given below, which is displayed in the browser after processes "Hello, World!"

introduction:

$ greetings </ title> </ head> <body> $ greetings <body> </ html></p> <p>The handling method is omitted here, and it is specifically contrast in later. Second, why do SMARTY? For PHP, there are many template engines to choose from, such as the earliest phpLib Template and the next Xiu Fast Template, have been quite mature and stable after several upgrades. If you are very satisfied with the template engine in the hands, then please see it down, I believe you as a free software enthusiast or pursuit of efficiency and elegant developers, how much it will be a bit in the following smarty. In addition to personal preferences, I have been tending to use official standards, such as Apache's XML engine AXIS. The advantage is that you can get as good compatibility (such as early MFC's compatibility with Win3X is better than other application frameworks, of course, now the various versions are completely perfect). I have been using the smarty to use Integrated Template Extension in the PEAR. This engine and phpLib Template, Fast Template are almost compatible, from templates syntax to the same display of the template: all read the template into the memory and then call the parse () function, replace the preset tag . Let's take a look at how Smarty is doing. After receiving the request, first determine whether the URL is requested for the first time. If so, "compile" the template file required to this URL into a PHP script, then redirect; if not, that is, the Template of the URL has been "compiled" After passing, check that you don't need to recompile, you can immediately immediately, recompile the condition you can set it yourself as a fixed time limit. The default is that the template file is modified. How, does it seem to be a bit more familiar? I think of it - this is not the principle of JSP! Indeed, this "compilation" is used to be incredible on the explanatory script engine such as PHP, but think carefully, Java is not explained by JVM? This is called "I don't do it, only I can't think." Since Java is talked, I will publish a point in the future of PHP. PHP official website announced that the PHP version 5.0 will be released at the end of 2003. This version has a lot of new features: such as abnormal processing, namespace, more object-oriented, and more. It can be said that it is more and more Java, and Smarty is also one of new features, making PHP more suitable for the development of large and medium-sized projects. But it seems that I have chosen its original because of my original, it is easy to use - getting farther and farther. But on a software survival cycle, PHP is in growth, and developers give it more functions, and the business application is better than the disadvantage. As a loyal user of PHP, I don't want PHP always accused "Ability"? Why choose Smarty, just because it is like JSP? Of course there is a more reasonable reason. First of all, in addition to the first compilation cost, as long as the template file is not modified, the compiled cache script is ready to use, save a lot of PARSE () time; second Smarty has a rich library like PHP, From the counting of the statistics to automatic indentation, text surrounds, and regular expressions can be used directly; if it is not enough, such as the function of the data result set page display, Smarty has strong extension capability, can be expanded in the form of a plugin. The fact is better than elite.</p> <p>I have designed a test program, which compares smarty and phpLib Template through speed and development difficulties. The reason for choosing phplib template is a phplib template in patrick's article "Select the most appropriate template in the PHP world." For the Fast Template competition, the pHPLIB Template won the whole victory, which makes smarty a good opponent. Before the test, please talk about the problems you need to pay attention to during the installation process. Third, the problem that may be encountered is on the official website of Smarty, there is a detailed user manual, which can choose the version of online HTML and PDF format. This is no longer involved in the content on the manual, just explains the issues that may be encountered in the initial use. The first question is very important: Tip can't find the required file? Not everyone is written in accordance with the Smarty default directory structure. Here you need manual designation, assuming that the directory structure is as follows: Just specify the directory structure in Index.php:</p> <p>introduction:</p> <p>$ smart-> template_dir = "smarty / templates /"; $ smart-> compile_dir = "smarty / templates_c /"; $ smart-> config_dir = "smarty / configs /"; $ smart-> cache_dir = "smarty / cache / "</p> <p>The first question is solved, the next is the second: How can I just use the beautiful template generated by Dreamweaver? It is not a problem with the template file, but because the default marker separator is {} by the smarty default, JavaScript is definitely containing this tag. Ok, we can use any character as a separator, plus this two sentences:</p> <p>introduction:</p> <p>$ Smart-> Left_Delimiter = "{/"; $ smart-> right_delimiter = "/}";</p> <p>This is basically completed, no problem. Fourth, the backing and class is more conceived. The main evaluation factors are of course speed. In order to perform speed test, an arithmetic average is taken. Repeat the page in the test page to generate N times, and then compare the total page to generate a time. Another important factor is ease of use (as for the scalability, there is no comparison), and the template used is not too small. I use the page of my personal page, a HTML file generated with Firework Dreamweaver, about 7k. The variable settings also take the most common block, called Block in the phplib template, and smarty said section. Don't underestimate this name, ease of use of sexual standards: the syntax of the template file and the script file is simple and easy.</p> <p>Let's go deep into the test. First look at the grammar of two template files: the left side of the blue strip is a template for phplib Template, and the right side belongs to Smarty. Personal preferences are different, so they don't come true here. Focus on the processing statement in the script, first look at the phpLib Template:</p> <p>introduction:</p> <p>$ TPL-> set_file ('phplib', 'bigfile.htm'); $ TPL-> Set_block ('phplib', 'row', 'rows'); for ($ j = 0; $ j <10; $ J ) ) {$ TPL-> set_var ('tag', "$ j"); $ TPL-> Parse ('Rows', 'Row', True);} $ TPL-> PARSE ('Out', 'PHPLIB') ; $ TPL-> P ('out'); below is Smarty:</p> <p>introduction:</p> <p>$ Smart-> Assign ('Row', $ ROW); $ Smart-> Display ('Bigfile.htm');</p> <p>Smarty only used Tags and ROW variables, and phplib Template has more template files, and there is an inexplicable OUT. Said that this OUT I didn't know why I want to exist when I first studied, I still don't have awkward. Why is smarty to deal with statements? The answer is that the work is completed by the engine. If you like to drill the source program, you can find a function called _compile_tag () in smarty_compiler.class.php, which is responsible for converting the Section this tag into a PHP statement. This is not a normal label, which has parameters and data, saving the workload of scripting programming, and the working volume on the template label is not large, and it can be determined that Smarty is high in SMARTY. The following is the speed of our most concerned. After all, for a skilled web developer, it is a matter of time problems, let alone template engine such learning curve gentle technology. The speed is the life of the web application, especially the template engine is used in a large site of concurrent visit, this is more important. Before the test start, I think PHPLIB Template will win on this link. Because it has experienced many upgrades, there is basically no bug, and smarty's engine is too big, not like its opponent only two files. Sure enough, the test results are as follows, phpLib Template has 25% speed advantage:</p> <p>But it won't always, I pressed a refresh again, this time I got a different result:</p> <p>PHPLIB is basically no change, but Smarty increased by 25%. Continue to refresh, the result is similar to the second result: Smarty is nearly 10% faster than phplib Template. I think this is the principle of compiling and interpretation. The Smarty engine itself is very large, plus the template into a PHP file, which is certainly less than a small PHPLIB TEMPLATE. But this is just the first time. When receiving the request, Smarty found that the template has been compiled, so the time consuming step is skipped, and the opponent will perform the search and replacement work in accordance with the class. This is a very classic "spatial change time" example in the compilation principle. 5. Conclusion The conclusion is if you already fall in love with smarty, what are you waiting for? Of course, it is not to say that it is all, just like I use MVC mode to write my personal website, but there is no reduction in workload, but it is always necessary to be a coupling between different levels. What is smarty not suitable? Classic examples in a manual: Weather Forecast website. I also thought of: the stock market market. Using Smarty on this website, it is more efficient due to frequent reconstruction, or PHPLIB Template is more suitable. This article is not to compare both engines, but to illustrate the advantage of Smarty. It is the most interested in use in it that it is a part of the PHP system. As an independent force, except for the two major systems of .NET and Java One, there are other options in large and medium-sized web development. This is a GNU project, and its significance is tantamount to Liu Deng, thousands of miles, jump into the big hill. Reference Smarty Official Site: Smarty.php.Net Wang Chen: "Select the most appropriate template in the PHP world" this article test program download: test.tar.bz2http: //phpe.net/uploads/attach/Article_1058233528.bz2</p> <p>About The Author in Boxiang, the pen name is from the University of Foreign Economics and Trade University. GNU is obsessed, I like to practice various programming languages ​​and study various architectures. Post number: 1275</p> <p>Reply: SMARTY is the core engine with many PHP Script, while smarty is in the end of the PHP World - Compare the templates in PHPLIB Template and FastTemplate PHP Projects Application is a good way to perform the processing performance layer recommended in medium and even large projects. But it is the implementation to the implementation of the template, but what existing template technology is used to compare. Templates in PHP world have two types of phpLib Template and FastTemplate. We evaluated the ease and speed of technology - Want to know the results? What is the cause of things: Do you have used fastTemplate? For template applications in PHP projects, in fact, I have already touched in many projects - about it, I think there is a lot of angles in the actual development stage or rising to design patterns. " The predecessors are discussed. For project implementation, in some medium and even large projects, it is effective to separate HTML (and other text forms) and PHP code, but not only improve interface designers and applications, not only in the development stage. Work efficiency will bring huge convenience to the test and maintenance of the project. However - the purpose of this article is not to discuss the advantages and disadvantages of the template, nor is it as a guided tutorial to teach how to use the template in the PHP project, but compare the most popular template processing mode in both PHP world in the application perspective (in fact However, there are two templates classes): phpLib Template and FastTemplate. In fact, I have been using phplib template in "quiet". It is very stable and it seems to look at the speed. I don't want to go any more alternatives - Although I also know this earth There is something like fastTemplate (and also in the world of Perl). Until one day, there was a colleague asked me: "I don't know how fastTemplate? Why don't we try fastTemplate?" "Well, let us try!" But as a steady method, in any new mode or Methods Before introducing projects, it is best to understand it more comprehensive, and find one or a few reasons to adopt it to adopt it - for fastTemplate is no exception. The protagonist appearance: Learn the phplib template and FastTemplate has already said, I have already used phplib for a while - I think you may be like me before the screen, but also a deep impression of this excellent tool class! Similarly, when I start to seek a template's solution, I naturally searched in the toolbox closest to the side, and I found the Template class in phplib. After the initial browsing of the API it provided (Of course, I would like to thank the PHPLIB detailed document), I started using its history - until now. FastTemplate seems to be more famous, and in the Perl world of its hurt, it seems to be in the PHP world. It is enough to believe in its ability to believe in just this. Regarding the use of the two, I would like to have more nonsense here; but after all, I feel that I am afraid I have written two tutorials. There is no existing tutorial. Welcome - In this article, there is phplib Template And FastTemplate's famous tutorial, if you have not had no one to know about these two templates, I suggest you go to see the two articles, you should get a lot of beneficial template application knowledge.</p> <p>(A mouse click and the eyeball is finished and even personally prepared test code.) Now you have some understanding of the two templates, maybe I have discovered a lot of similarities between them, I will summarize these places. a bit. The setting of the variable is obvious, the form of {foo} or {bar} is specified in the two templates; that is, in the two template processing methods, the appearance of the template file itself should be consistent (for example The HTML file contains a variable that will be replaced with {} identifies. The initialization of the template class (class builder) requires the directory location where the template file exists when building a template class. The most commonly used variables is the most commonly used variable replacement. In addition to the method names (PHPLIB Template uses set_var (), FastTemplate uses assign ()), and the usage is almost consistent - can be used (KEY, Value's way, you can also pass an array directly (Array (Key => Value). The processing of the template file is a way to specify a handle (Handler) for each template file, and the handle can also replace the variables in another template file as the value of the variable. Analysis, the output process is needed to call the parse () method (this method name is actually the same) to assign the template file that needs to be output to a handle, then call the respective output (P () in phplib template, fastTemplate The content of the handle is output and the processing is over processing it is FastPrint ()). Repeated resolution, such as removing several records from the database requires display, and the template file is only available to the alternative line variable, it is necessary to function. Both have such a function, just slightly different (phpLib Template), and fastTemplate uses PARSE (Handler, .Value) to add a point in front of the value, you should say phplib The method of Template is constructed relatively beautiful. The process of block parsing (or can be called dynamic analysis) Imagine that you need to take out the eligible data from the database and display it in the web page - but because the conditions will not be the same, you can't clear how many Data - This time if you need to use a template, the block is the best choice. It is a portion defined in a template with a particular symbol, which can be repeatedly parsed and added to (not the previous resolution by the last overlay) Output web page. The block may be like the same display (the left side is the block settings used by phpLib Template, and the right side is FastTemplate): Ok, if you introduce the above pale text, you can't touch it, then we come Take a look at the two detailed template processing routines! (If you are interested in discovering the test code behind, you will find that the following two examples come from there.), Is it almost consistent? Below is an example of block parsing, you will also find the same effect: our test objectives and results have ended the understanding of phpLib Template and fastTemplate, which should be able to enter this article - Of course, it should be easy to use in the application environment. At the same time, the ideal component building system is constructed, so it is necessary to evaluate the evaluation for such two similar techniques. The evaluation should be composed of two parts: the difficulty of the technology is difficult and the speed of the speed - the former is part of the comment, and the latter is a part of the test.</p> <p>For the former, we mainly commented on the API provided by the two classes; for the latter, we will let the test data to speak, of course, this is from need to write some simple test code. Round 1: The ease of technology This round is mainly to explore the usage of the API provided by PHPLIB Template and FastTemplate. It should be said that the API provided by the former is more in line with some of the common coding practices of PHP (especially when using other classes in your project, this normative will have a good impact on the entire project); while some of the methods The name always feels some awkward (I hope you don't think this is my narrow view, such as fastprint (), etc.), and the parameters of the method are not very "authentic", this can also be seen from the code just now. Another point needs to be pointed out that for the parsing of the template block, FastTemplate has begun to support until the nearest release. That is, if you use the previous version, you have to store this content independently in somewhere in handling the output, such as the output recorded in the database, and then add this file when the template analysis is processed - it is one The uncomfortable thing, especially for web designers. Of course, there is still a point in view - that is the support for PHP versions. PHPLIB is generated in the era of php3, this and fastTemplate is almost; but according to our app, phplib is quite good in the current PHP4 environment, and some information shows some information on PHP4, there are some bugs . Ok, talk so much (maybe you will feel the bad words of fastTemplate), the winner of this round is obvious: phpLib Template, especially when you use the phplib's other classes, such techniques are more obvious (You will not feel unfamiliar with these APIs from the same development group). Round 2: Processing speed Maybe this is the most concerned part of many people - in this round, we will use two template processing: one is a routine analysis, replacement, and the other is the analysis of blocks. , Alternative - simultaneous two ways also apply the most in the actual system: the former is a general page processing, the latter is the output processing of the database content. At the same time, since the format of the template files used in the two templates is basically the same, we can provide almost consistent template files for two template resolution, more increased the credibility of the test. A test scheme is prepared before the speed test is carried out, and it is to be written in two PHP test pages. At the same time, there is a control test page multiple calling these two pages and recording time for acquisition test data. . (If you are interested, you can also refer to the following detailed test programs, maybe you will understand this test.) Summary - After the entire test system is completed, we should be able to get the following file list in / Test directory: (A bit complex test plan) first is determined to determine the hardware and software environment of the test - hardware is definitely its own machine, Intel Celeron 733MHz, 256M RAM, 40G HDD; Software Platform OS is Win2k Pro, the web server is Apache PHP And run in a module.</p> <p>Secondly, the system plans this test - Of course, first open a TPL_TEST new directory in the Web server's document root directory to place all files for this test; then build the include directory in / tpl_test to store two template files (We tested the core, in .inc.php is the file extension) and a test class file (including timing and logging logs, and reading logs and analysis, etc., with .inc.php as file extension) and a data Document (preparing for the test for block parsing, mainly including a two-dimensional array, equivalent to .inc.php as a file extension), establish the template file (template file that needs to be parsed by .ihtml) For the file extension), establish the log generated by the logs directory storage test (later, it is found that the data of the test is obtained by the analysis of these logs, with .log as the file extension). Of course, the processing PHP files of the two templates are placed in the / Test directory. The most critical point of this test is that a PHP file is also needed, and the file that is mentioned above is called several times. PHPLIB Template parsed, then the PHP file that the result is responsible for requesting the above two pages in HTTP to obtain test data. Select the template and PHP program to be parsed - because the two template processing methods are almost consistent for the format of the template file itself (such as the form of {var}, etc.), so it can guarantee two in the same test. The selected template is as the maximum fairness of testing as much as possible; at the same time, the two templates commonly used in the simulation of the real system: the general page processing and the output processing of the database content, the test used template file It is also divided into two: one is a template file with some template files to be replaced, the other is a template file that requires repeated replacement of the content that should be output. The same is required for both template files, and two different PHP files are needed. Test Method - Request to / Test/Result.php in the browser, need to be tied with parameter type = [simple | complex], you can see the test results in simple or complex mode in the result of the result of the returned result. . Level 1 Level 2 Level 3 Remark / Test Test System Root Retribution Result.php Tests and generates a PHP file for results. You only need to request this page in the browser to get the test information simple__test_phplib.php Use phplib template to usually Template for analysis PHP file Simple__test_fast.php Using FastTemplate PHP file complex__test_phplib.php to analyze the general template using phplib Template PHP file complex__test_fast.php using FastTemplate to analyze the block template with block template / include contains a PHP class file .inc.php phplibtemplate.inc.php phplib Template class file fasttemplate.inc.php fasttemplate class file TPLTest.inc.PHP Test class, including timing, read / analyze log, etc. method. Data.inc.php The data file used when the block template is tested.</p> <p>Usually the template file simple_fast.ihtml / ihtml containing the template file using PHPLIB Template .ihtml simple_phplib.ihtml FastTemplate process using the template file is generally treated using PHPLIB Template complex_phplib.ihtml band processing block using template file complex_fast.ihtml treated with FastTemplate Block template file / logs contain log files .log simple_phplib.log uses phplib template to process a log Simple_fast.log generated by a PHPLIB Template to generate a log complex_phplib.log using a pHPLIB Template Processing Zone Template Generated Logs COMPLEX_FAST .log uses the logs generated by the FastTemplate Processing Zun Template. After the test system is designed and written, and we can visit this system after discussing colleagues responsible for web design, we can visit this system - the previous hard work makes it now Working in viewing results only need to be http: // localhost / tpl_test / in the browser address bar.</p> <p>Result.php? type = [simple | complex] (if you are tested in other non-local servers, the domain name should adopt the domain name of the server - such as my own machine called Patrick, etc.). Here is the result I have obtained in a certain test: (Test Results Data Interpretation) Name Interlaim Note Amount Total (Continuous Request This Total Total) This parameter can modify the maximum processing time of Max_SEQ in the result.php file. The value peak data of MAX_VALUE maximum processing time between 1-Amount is for reference to the minimum processing time of the Min_SEQ minimum processing time. The value peak data of the minimum processing time between 1-Amount is available for the most valuable data in the Average average processing time test. Of course, if you think that the results of the test are unreliable, you can press the browser's refresh button repeatedly, you can observe the results of different tests (theoretical should be different). The test results and the "XX Selection Award" are good, and PHPLIB Template has struck fastTemplate in the rounded secondary speed of the bias speed test; while the phplib template in the first round has a good API design and easy Use sex to top the wind. The results were obvious - our choice awards for the PHPLIB Template, and this test also made us have a deeper understanding of phplib's library design. Subjective evaluation Since there is a result, FastTemplate naturally can't enter our project. Although we have spent half a day from the results, it has been a variable result (phplib template continues to be in the project. Use it), but the process of testing is very valuable, especially the method of testing using PHP, should play a certain reference role in future similar decisions. References This article downloads (Article_256.zip) introduces phpLib Template and FastTemplate's famous article Templates, The phplib Way by David Orr, May Of 2000 (http://www.phpbuilder.com/columns/david20000512.php3) (PHPLIB Template) Templates - why and how to use them in PHP3 by Sascha Schumann, March of 1999 (http://www.phpbuilder.com/columns/sascha19990316.php3) (FastTemplate) PHPLIB Template and FastTemplate home page PHP Library Homepage (http://phplib.netuse.de) fastTemplate HomePage (http://www.thewebmaster.com Other template processing article and other information about PHP) PHP builder (http://www.phpbuilder .com) Chinese translation in the above article and other Chinese PHP Information PHP Chinese User (http://phpuser.com) About the author Wang Chen, a web developer, and is interested in writing and translating related to their own technical hobby Article (like others call yourself patrick when working and publishing articles). I have focused on PHP, and now there is more hobby lies in Java; it is also one of the advocators of Open Source. I often like to visit the bookstore when I am leisure; I often move around the home and the alma mater - graduated from the University of East South. You can email</p> <p>Patrickwang@163.com Contact him.</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-91570.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="91570" 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.037</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 = 'n4dBRJu_2BkKoBbEz1nW_2BwDq8S5VMgOnjOlwveq_2B5Ei2f9atvVbCjYIBw7SJdiY1Af5KZBkxubzEYIiJW3VbdmiA_3D_3D'; 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>