Merge _08smarty template technology (3days)

Source: Internet
Author: User
Tags autoload delete cache smarty template

2013-12-9 Yesterday content review

  1. Two common methods assign () and display ()
    1. The Assign method passes the variable information to the template (it actually gives the information to the Smarty object property inside)
    2. The Append method passes the variable information to the template, and the difference between the assign is passed to the template as an array, and the variable information with the same name can be used
    3. The display () method shows the template, suffix. html.
    4. In general, when used, the template suffix habit is. TPL, so that others will not come directly to visit.
  2. 3 types of variables in the template are used
    1. Assign the variables passed to the template are used
    2. System reserved variable get post session cookie now config {$smarty. get. Name}
    3. Configure variable information config/site.conf {config_load file= "site.conf" section= "segment"}
      1. {#名字 #} {$smarty. config. name}
      2. Configure variable information to make the overall CSS style of the page change quickly
      3. [NewYear]
      4. Color=xx
      5. Backcolor=xx
      6. [Yuandan]
      7. Color=xx
      8. Backcolor=xx
  3. Using array elements and object properties in templates
    1. Array [subscript]
    2. Array. subscript
    3. Both of these methods support the use of indexes and associative arrays
    4. Object---Properties (object methods also support use)
  4. foreach traversal array (both indexes and associative arrays can be traversed)
    1. {foreach array as $k = = $v}
    2. {Foreachelse}
    3. Array is empty
    4. {/foreach}
    5. @first determine if it is the first message {[email protected]}
    6. @last
    7. @iteration
    8. @index
    9. @total
    10. @show whether the array is empty
    11. Section (indexed array)
  5. check boxes and drop-down lists use the
    1. {html_checkboxes option=$ array selected=$ array separator= " "}
    2. {html_checkboxes value=$ array output=$ number selected=$ array separator= " "}
    3. {html_options option=$ array selected=$}

?

"Inheritance, Layout"

?

    1. Layout page

    2. Sub-level page

?

"Template file contains"

?

"Template content Capture"

An ad content is displayed at the same time in multiple places on a page

<div> Specific advertising content </div>

"Variable Adjuster"

A variable conditioner is called a "pipe" inside a Linux system.

?

Adjusting variables

???? $name = "Linken";

???? {$name}//Linken

???? {$name |lower} to adjust the variable to lowercase//linken

???? {$name |upper} adjust the variable to a size of//linken

???? {$name |count_charactor} calculates the variable character length//6

?

???? Variables are adjusted by "function" in the middle of output, variables and functions | Vertical Bar Connection

???? The variable does not output properly on the page, but instead becomes the input parameter of the latter adjustment function

???? Variable adjuster: The output of the former is the input of the latter

?

????

?

Different operating system line break symbols display

?

"Smarty in conjunction with MVC projects"

?

?

Summarize:

    1. Layout, inheritance extends block
      1. <{}> < {extends}>
    2. Template contains {include file= "public/xx"}
    3. Variable adjuster truncate DATE_FORMAT lower Upper
    4. Smarty is combined with the MVC framework

?

Practice: Incorporating Smarty into your project

?

Cache

Two types of caches:

???? ①: Caching of static HTML code

???? ②: Cache the data obtained from the database.

?

???? The information we see on the page is generated by the PHP program and is eventually presented in HTML code.

???? Website information, in a period of time will not randomly change, then we put PHP generated good HTML code to cache, the next time users visit the site, you do not have to request PHP parsing, direct access to static HTML code.

?

???? Cache Media: file , memory (most used), database

???? Implementing static HTML code caching with Smarty

???? The most used cache in the actual working environment is the memcache memory cache. Caching is the data information of the database, not the static HTML code.

????

???? Where to use static HTML code caching:

???? Portal news website.

?

    1. Implementing HTML static caching via Smarty

    2. Cache time

      ' Cache_lifetime ' = 3600,

      $smarty-cache_lifetime = 20;

      $smarty, iscached (' template file '); Determine if the cache exists

      ?

?

    1. The difference between $smarty, caching=1, 2;

?

?

????

02.php

???? $smarty = new Smarty ();

???? $smarty-caching = 2;

???? $smarty-cache_lifetime = 20;

???? $smarty, display (' 02.html ');

???? Constant user access to 02.php, the situation caching=1/2 the same effect.

?

02.php generating static pages ( background )

???? $smarty = new Smarty ();

???? $smarty-caching = 2;

???? $smarty-cache_lifetime = 20;

???? $smarty, display (' 02.html ');

?

03.php 04.php 05.php access to static files ( front desk )

???? $smarty = new Smarty ();

???? $smarty, caching=2; Caching is turned on, otherwise the cache file cannot be used

???? $smarty, display (XXX); First determine if the cache file is valid, or regenerate the cache

?

???? There are many cache files, cache time is different, some cache 1 days, some cache 3 days, some cache 7 days

(This will also set the effective time of cache_lifetime Smarty.class.php at least 7 days).

?

The role of caching=2 is to set different cache times for different files.

?

"Paged page Cache effect"

1. Single-template multi-cache

????

    1. Delete Cache

"Local does not cache"

Local does not cache the application scenario: the first page, there is user login information, the display of the user name of the part can not be cached.

Specific application:

????

????

"Cache Collection"

Similar to single-template multi-cache

Display (' 04.html ', numbering); Generate a number for each static page

?

???? 1. Generating a Cache collection

????

2. Clear the Cache collection

?

Summarize:

???? Cache

???? Set the cache caching=1/2;

???? Determine if the cache exists iscached ()

???? Cache file Delete clearcache (' template name ')

???????????????? Clearallcache ();
???? Single template multi-cache production display (' template ', each static page of the logo cache_id);

???? Local does not cache

???????? {NoCache} {/nocache}

???????? {$name NoCache}????

???????? Assgin (' name ', ' Tom ', true);

???????? Insert_age () function application

???? Cache Collection

Homework:

    1. Customizing the small MVC Framework for integrated Smarty
    2. All controller controllers have a parent class that instantiates smarty inside the parent class using
    3. achieve inheritance and layout effects with tutor templates
    4. Practice caching using

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?????

?????

?

?

2013-12-10 Yesterday content review

  1. layout Inheritance Effect
    1. extends block
    2. have parent page and child page
    3. layout laoout parent page
    4. pa rent.html
    5. <body>
    6. <DIV&G t; header information </div>
    7. {block name= ' left '}{/block}
    8. {block name= "right"}{/block}
    9. <div> feet </div>
    10. </body>
    11. child page to inherit
    12. child page
    13. son.html
    14. < style>xxxx</style>{* does not work *}
    15. {extends name= "parent.html"}
    16. { Block Name= "left"}xxxxxx{/block}
    17. child page does not work because the child page can have only two things: extends and block
    18. The CSS style can be placed inside the {block} tag to make it work
  2. Template contains include
    1. {include file= "public part" assign= "XX"}
    2. {$XX}
  3. Template captures capture
    1. You can use capture tags to capture a piece of static code content, which is handy for other places on the page
    2. {Capture Name= "XX" assign= "YY" append= "ZZ"}<div> Static content </div>{/capture}
    3. Specific use:
    4. {$smarty. capture.xx}
    5. {$YY}
    6. {$ZZ [0]}
  4. Variable adjuster
    1. The output of the former equals the input of the latter
    2. {$name |lower}
    3. {$name |upper}
    4. {$content |truncate:20} intercepts only 17 characters, with 3 dots behind
    5. {$mytime |date_format: "%Y"} Time formatted display
    6. The Linux operating system also has variable regulators, called "pipelines"
  5. Used in conjunction with MVC
    1. Introducing Smarty in the configuration file
    2. Smarty instantiation is instantiated in the parent class of many controllers (the advantage is that all controllers can use Smarty directly, without having to re-instantiate them)
  6. Cache Settings Use
    1. Open Cache $smarty-CACHING=1/2;
    2. Cache expiration, two convenient to determine whether the cache is out of date
    3. 1. Smarty->cache_lifetime familiar with judgment
    4. 2. Judging by each cache file's own cache time


    e) Single-template multi-cache

    ???? Data paging to make a cache for every page of paging

    ???? Display ("Template", ref. PAGE_ID);

    1. Delete Cache
    2. ClearCache (template)
    3. ClearCache (template, number)
    4. Clearallcache () Delete all caches
    5. Local do not cache settings
    6. {NoCache} {/nocache}
    7. {$name NoCache}
    8. Assign, $smarty (name,value,true);
    9. function Insert_age () {Not cached content} {Insert Name= "age" assign= "XX"}
    10. Cache Collection
    11. Display (template, "hello|world|beijing");
    12. Display (template, "Hello|world|shanghai");
    13. ClearCache (null, ' Hello/world ');
    14. ClearCache (' template ', ' Hello|world|shanghai '); Delete only the specified template under Hello/world/shanghai

?

"Caching Principle"

    1. How the cached static file is generated

      A: It is the "PHP buffer" to obtain the content, generate static files.

????

Ob_start (); Open PHP Buffer

Ob_flush (); Output PHP Buffer contents

Ob_get_contents (); Get PHP buffer Content

Ob_clean () Empty the contents of the PHP buffer

Ob_get_clean (); Get the contents of the PHP buffer and empty the buffer

Flush () output Browser buffer contents

?

    1. To open the PHP buffer:

    1. PHP buffer and browser buffer content directly output, do not stay

    1. Get PHP buffer content and deposit files directly

    1. Buffer-related functions

    1. Firefox-to-cache processing

7.???????? PHP programs are accessed to perform process and cache relationships????

????????

?

"Smarty Execution Process"

Compiling files: html+php mixed code files

Cache files: HTML static code

?

To perform a sequential relationship:

???? If caching is turned on, the cache file is executed first

???? Without opening the cache, execute the compiled file

    1. Front filter Use

    2. Rear Filter

    3. Output filter

    4. The significant benefits that filters bring to us

      Our "Compile Files" and "cache files" can be processed centrally through a filter

      Especially in the MVC framework, all of the compiled and cached files will work as long as the filters are set in the parent class controller assembly.

    5. Force a rebuild of the compiled file (delete first)

?

?

Advanced Features

    1. Data Objects
      1. Background of data occurrence

      b) Specific use of data objects

    2. Object Registration

      We can pass an object to the template

      Both the properties and methods of objects in the template can be used

      ?

      Object Registration studies The method of the object.

      1. Object Registration function:

      Object registration can restrict how objects in a template access their own specific methods.

      2. The registered object only acts on the method , does not relate to the property

    3. Registered objects are used specifically

Summarize:

    1. The caching principle (page static process) fetches the PHP buffer content to generate a static file.
    2. Ob_flush () flush ()
    3. Filter use
    4. Data objects, many templates of the same request can use variables of the same name, without conflicts
    5. Registered objects: You can restrict the specific methods of object access in the template, only care about methods, not care about properties

?

"Smarty with existing projects"

???? Smarty is combined with existing projects:

????

????

???? Debugging an auto-load function

?

    1. Using the auto-load function

      __autoload () can be used alone, and if the system has more than one autoload, it improves access by Spl_autoload_register.

    2. Method two use

    3. Method Three uses

?

    1. Smarty and existing projects need to pay attention to the problem
      1. Handle the automatic loading function well Spl_autoload_register
      2. Templates are used through the Smarty syntax
      3. Instantiate the smarty inside the parent class.

?

Pre-operation $TPL parameter principle analysis:

?

"Custom Variable Adjuster"

{$name |lower} {$name |truncate:20}

    1. Variable modifiers can be nested using

2. Custom variable Adjuster

???? Custom Information Font size display

???? $smarty-Assign (' addr ', ' Beijing ');

???? {$ADDR}

???? {$addr |change_size:30}

???? <span tyle= "font-size:30px;" >beijing</span>

    1. Custom selector Code

      Specific use

?

?

"Custom Function Use"

1. {php} ... {/php} not recommended

3.1 has been abolished and requires SMARTYBC support

smarty3.1.14 version, is a PHP5 advanced version of the template engine

Within this version, the function name has its own naming style: Camel-named

Registerobject (new)-------------------Register_object (old)

Registerfilter (new)--------------------Register_filter (old)

?

?

2. {Strip} ... {/strip} Remove extra blanks and enter

Template file: Comments, line feeds, carriage returns, so that the template file looks good readability

Compile file: Template file line, enter in the compilation file will also be reflected, we can use the strip tag to compile the file line, enter to delete.

?

    1. debug tag, debugging, will give us a console that is related to the Smarty variable information

$smarty-debugging = true; Show Debug window

?

    1. {eval} ... {/eval} output variable information

    2. {Fetch} ... {/fetch} Get file contents

      {Fetch file= "http://www.baidu.com"}

?

    1. {Cycle} ... {/cycle}

Alternate appearance

    1. {Html_image}

      Create a picture label

      File

      Width

      Height

      href= Photo Hyperlink Address

    2. {html_table}

      Table Labels

      {html_checkboxes} {Html_radios} {Html_options}

      ?

????

?

    1. Smarty Property

$compile _check =true; [Compile check variable]

Once the template file or configuration file is updated, the compiled file will be rebuilt

When using Smarty, if the template file changes, the changes are immediately displayed

???????????????????? If the PHP program changes, the page will not immediately display changes (need to update the compilation file)

?

This property $compile_check if it is true, the template file or configuration file changes, and the compiled file is regenerated.

?

"Common Methods"

1. Append and assign distinguish use

Append: Put the variable inside the array of Smarty objects

Assign: Put the variable inside the Smarty object

    1. Assignbyref ()

????????

?

    1. Clearallassign () Delete all assign variable information
    2. Clearassign () Deletes the specified assign variable information
    3. Configload () introduces configuration variables????????
      1. {config_load file= "site.conf" section= "NewYear"}
      2. $smarty-Configload ("site.conf", ' newyear ');
    4. Clearconfig () Clear Configuration variables

????

7.

???? Display () Presentation template information

Fetch ()???? Get template information

Display () = echo fetch ();

Summarize:

    1. Custom variable Adjuster

    2. Smarty in combination with existing projects
      1. The autoload of their own projects is compatible with the autoload of Smarty
      2. Specific use
      3. Instantiating Smarty on the parent class controller
    3. Custom Function {debug} {Html_image} {Html_checkboxes}{html_radios}
    4. Common methods

?

Homework:

???? 1. Integrate Smarty with previous intermediate projects

???? 2. In the middle of the project to the smarty of the layout of the succession effect reflected

???? 3. Get the data out of the database,append (), and show the foreach in the template by Smarty

???? 4. If the page has a public section, place the public part code in a fixed file, and the other templates include

???? 5. Set the homepage as the cache page, the user information set " Local not cache "

?

?

?

?

?

?

?

?

?

?

?

2013-12-07 Yesterday content review

    1. The 0-n difference between regular function match subscript
      1. Mode unit/(SLKDKLS) (Slkdls)/0 matches all content 1-n specific mode unit contents
      2. The G global Match 0-n represents the first to nth matched content (no schema unit)
    2. Pattern Cell Reverse Reference
      1. Mode/(\d+) (\w+) \1/' 2013hello2013 '
    3. Regular combination characters
      1. \d Match 0-9 any number
      2. \d matches non-numeric
      3. \w matches letters, numbers, underscores one
      4. \w non-alphanumeric, numeric, underline
      5. \s Matching Spaces
      6. \s Non-whitespace
      7. \b Word boundaries (spaces, start or end quotes \ n)
      8. \b Words inside
    4. Regular limiter
      1. {m} limit front cell Strictly appears m times
      2. {m,} limit the front cell to appear at least m times, more unlimited
      3. {M,n} limits the front cell to appear at least m times, up to N times
      4. [^ATYC] Matching information is not any of the letters in the ATYC
      5. [^0-9] Non-digital

?

"Smarty Template Technology"

Templates: Static HTML files are templates

General PHP Program files are PHP program and HTML code mixed style.

?

Programmers specialize in developing PHP programs

HTML static page is an artist developer

?

The above code is a combination of PHP and HTML, if the system upgrade or maintenance, this code will make the programmer and the art of each other.

?

The advent of Smarty template technology will allow the PHP program and HTML code to be separated in physical form.

?

    1. customizing Smarty Templates

????

    1. Why use Smarty

      Interview needs.

      Even if you do not use smarty, you need to use a different template engine.

    2. Using Smarty

????

?

    1. Flexible configuration Smarty

      ?

      ?

    2. Encapsulates a parent class to configure Smarty variable information

?

    1. What variable information can be used inside the Smarty

      6.1 System Reserved variables

      6.2 Configuration Variables

      6.3 Changing the overall page style by configuring the variable Config

      ?

Summarize:

    1. Smarty Use
    2. PHP HTML separate smarty will allow php+html to be combined again

?

?

Assign ()

???? Parent class of the Smarty parent class Smarty_internal_templatebase Smarty_internal_data

Display ()

???? Smarty's parent class Smarty_internal_templatebase

?

"Assigning variable information to a template assign and append methods"

Assign is to assign information to a specific variable if the name repeats and covers the situation

Append is to give the information to an array, name repetition can exist simultaneously, in the access to the time required by the array subscript access to the specific element information

?

"Using array or object information in a template"

    1. To use an array in a template:

    1. Working with objects in templates

????????

?

"Traversing an array in a template"

1. Foreach Specific Use

    1. foreach traversal of two-dimensional arrays

    2. foreach nesting Uses

      ?

      1. Section

        foreach can traverse indexes and associative arrays

        section can only traverse indexed arrays

????????

?

    1. For Loop statement

      While loop

?????

????

"Branch Structure"

If ElseIf else

????

????

?

"Get check box button"

The Options property is a collection property of values and output

"Drop-down list"

?

?

Defining template variables by label assign

????

?

Summarize:

    1. Using array or object information in a template
      1. Array [subscript]
      2. Array. subscript
      3. Objects, Members
    2. Array traversal
      1. The foreach index is associated with the ability to traverse
      2. @index @iteration @first @last @show @total
      3. {Break} {Continue}
      4. Section???? Indexed array
      5. NAME=GD Loop Max Step
      6. {$smarty. Section.gd.first}
    3. Branching structure
      1. If ElseIf else
      2. > <= < = = = Div by
    4. Custom functions
      1. Html_checkboxes
      2. Html_options
      3. Html_radios

?

Homework:

    1. Pass variable information to the template in PHP, display the information in the template, the effect is like a template
    2. Practice Smarty Using the manual

?

?

?????

?

?

?

Merging _08smarty template technology (3days)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.