PHP smarty Basic syntax and three variables

Source: Internet
Author: User
Tags smarty template
PHP smarty Basic syntax and three major variables

In the Smarty template is mainly based on variables, the following is about smarty three variables and some common usage


First build the file you want to use in the root directory the same as the last time, Configs is the configuration folder




One, the variables allocated from PHP

With $smarty->assign ()

$smarty->display ()


First write the initial PHP code, and the same as the previous article conn.inc.php

 
  Assign ("title", "11111111");    $smarty->assign ("Content", "2222222222");    Allocates an associative array with less    $smarty->assign ("Author" = "He Hing", "Time" =>date ("y-m-d h:i:s"));    The registration function    $smarty->registerplugin ("function", "myfun", "Test");  Register the plug-in and register the test function as Myfun function    Test ($args) {    //args=array ("Size" =>7, "color" = "yellow", "num" =>5, " Connect "=" 3333333 ")    //Loop through    $str =" ";    for ($i =0; $i < $args ["num"]; $i + +) {    $str. = '. $args ["Content"]. '
'; } return $str; } Array $smarty->assign ("Contacts", Array ("0575-1241243", "[email protected]", Array ("243214124", "45345"))); Object class person{public $name = "Kefeng"; function say () { return $this->name. " Hhhhhhhh "; } } $smarty->assign ("P", new Person ()); $smarty->display ("demo.html"); The template that is displayed

Demo.html:

<{$title}>   content:<{$content}>
content:<{$content}>
content:<{$content}>
author:<{$author}>
time:<{$time}>
Time:<{myfun ()}>
Time:<{date ("y-m-d")}>

<{myfun size= "7" color= "Yellow" num= "5" content= "3333333"}><{$contacts [0]}>
<{$contacts [2][0]}>

<{$p->name}>
<{$p->say ()}>







Second, read the variable from the configuration file

Here you need to create a configs folder, inside the configuration file test.conf

Test.conf:

bodycolor= #3e3e3ebodybgcolor =yellowborder=10align=centerwidth=800bgcolor=gray[index]one=11111[list]two=22222[ content]three=33333

Open session in Demo.php

 
  Display ("demo.html");  The template that is displayed

Note that the following are: load config file: <{config_load "test.conf"}>
READ: <{#内容 #}>
Loading a zone file takes the second parameter: <{config_load "test.conf" section= "index"}>


Demo.html: In, here to write some header files, as well as loading code

      <{$title}>        <{config_load "test.conf" section= "index"}>      ....   .......  <{#one #}>
<{#two #}>
<{#three #}>
......


Three, reserved variables

Mainly include: $_get
$_post
$_session
$_server
$_env
<{$smarty ()}>

<{$smarty .session.username}>
<{$smarty .now}>
<{$smarty .const.root}>
<{$smarty .const.m_pi}>
<{$smarty .current_dir}>
















  • Related Article

    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.