Several ways of smarty accessing variables in PHP

Source: Internet
Author: User
Tags arrays smarty template

$TPL =new Smarty ()//New Smarty object, I'm using the Smarty-3.1.6 version

1. Set Smarty template path $tpl->settemplatedir (); By default, templates

2. Set the Smarty template compilation Path $tpl->setcompiledir (); By default, Templates_c

3. Set the left and right delimiters of the Smarty template engine,

The code is as follows Copy Code

$TPL->left_delimiter= "<{";

$TPL->right_delimiter= "}>";

By default: public $left _delimiter = "{";//smarty source code

Public $right _delimiter = "}";//smarty source code

Why do we have to change these separators?

Because, for example, in an earlier version of the Smarty engine template, the error is not automatically recognized.

The code is as follows Copy Code

For example: <style> div{margin:0;}                   </style> or JavaScript <script> function Show () {alert ("Smarty"); } </script>

In both cases, there are "left and right braces", and the Smarty engine encounters an error.

4. Initialization operation we can create a separate PHP file for initialization operations, such as: smarty.ini.php. And then include it in the PHP file.

  code is as follows copy code

        <?php

               include ". /smarty3.1.6/libs/smarty.class.php ";

              $tpl =new Smarty ();

              $tpl->settemplatedir ("./ Tpl ");

              $tpl->settemplatedir ("./ Compile ");     

               $tpl->left_delimiter= "<{";

              $tpl->right_delimiter= "} > ";

      

5. Using the Smarty template engine's display function or including other templates, you can smarty the template directory specified in the object (for example, the TPL directory, the default is the Templates directory) as the base directory.

① Template directory is: TPL, there are many templates in the directory, there are default,green,red templates, the default template directory has a lot of template files (INDEX.TPL, HEADER.TPL, FOOTER.TPL), the correct use of display at this time: $tpl->display ("Default/index.tpl"), that is, the default template directory under the base directory

② include other template files (such as: Header.tpl, FOOTER.TPL) in a template file (such as: INDEX.TPL), the appropriate notation for include should be: <{include "Default/header.tpl"}>, <{include "Default/footer.tpl"}>

Although Index.tpl, HEADER.TPL, Footer.tpl are all in the same directory, but <{include "Header.tpl"}>, <{include "Footer.tpl"}> is the wrong wording, In this case, the Smarty engine looks for headers and footer in the TPL directory, rather than under default.

6. If you want the PHP program in each directory to load Smarty and use Smarty to specify the template directory and compile the directory, the only way is to use the absolute path.

How to access a variable in the 7.Smarty template engine (remember the "$" symbol before the variable in the template)

① Access Array

Indexed arrays:

The code is as follows Copy Code

$tpl->assign ("arr", Array ("AA", "BB", "CC"));

$tpl->assign ("arr2", Array ("Two-dimensional array one by one", "two-dimensional array one or two"), Array ("Two-dimensional array 21", "two-dimensional array 22"));

accessing indexed arrays: <{$arr [0]}>, <{$arr [0]}>, <{$arr [0]}>

Accessing two-dimensional indexed arrays: <{$arr 2[0][0]}>, <{$arr 2[0][1]}>

Associative array: (using the. Symbols to access)

accessing associative arrays

The code is as follows Copy Code
: <{$arr 3.id}>, <{$arr 3.name}>, <{$arr 3.age}>

② Access Objects

To create an object:

The code is as follows Copy Code

Class human{

Private $sex;

Private $name;

Private $age;

Public function __construct ($s, $n, $a) {

$this->sex= $s;

$this->name= $n;

$this->age= $a;

}

Public Function Print_info () {

return $this->sex. " --". $this->name." --". $this->age;

}

}

$tpl->assign ("Student", New Human ("Male", "Marcofly", 22));

Assigning values to objects in a template: <{$student->print_info ()}>

Mathematical operations in the 8.Smarty template engine can be applied to template variables

Assigning values to variables

The code is as follows Copy Code

$tpl->assign ("Num1", 10);

$tpl->assign ("num2", 5.5);

Template variable Output

  code is as follows copy code

<{$num 1 }>//Results

<{$num 2}>//Results 5.5

<{$num 1+ $num 2}>//Results 15.5

<{$num 1+ $num 2* $num 2/$ num1}>//results 13.025

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.