Smarty Getting Started Tutorial 1

Source: Internet
Author: User

Instance 1:

Let's look at a simple example.

Index. tpl


CODE: [Copy to clipboard] {* the text contained in * is the comment content in the smarty variable identifier *}
<{Include file = "header. tpl"}> {* page header *}
Hello everyone, my name is <{$ name}>. You are welcome to read my smarty learning materials.
<{Include file = "foot. tpl"}> {* end of page *}

The above example is a tpl template, where:
1. <{**}> is the template page comment. It does not output any output when the smarty parses the template. It is only used by the template designer to comment on the template.
2. <{include file = "xxx. tpl "}> use this sentence to include a template file to the current page. In this example, the head is used in public services on the website. tpl and foot. tpl is included. You can
In this case, copy all the content in xxx. tpl to the current statement. Of course, you can copy the content in XXX. tpl to the current statement without using this sentence.
That's all right.

3. {$ name}: template variable, which is the core component of smarty. It is included in the left boundary {and right boundary} defined by smarty and provided in the form of a PHP variable. It will be used in the smarty program.
$ Smarty-> assign ("name", "Li Xiaojun"); replace the $ name in the template with the word "Li Xiaojun.
The entire instance source code is as follows:

Header. tpl


CODE: [Copy to clipboard] <Head>
<Title> Master smarty tutorial </title>
</Head>
<Body>
Foot. tpl


CODE: [Copy to clipboard] <Center> CopyRight (C) by Email: teacherli@163.com August 2004 </center>
<Hr>
</Body>
</Html>
Index. tpl


CODE: [Copy to clipboard] {* the text contained in * is the comment content in the smarty variable identifier *}
{Include file = "header. tpl"} {* page header *}
Hello everyone, my name is {$ name}. You are welcome to read my smarty learning materials.
{Include file = "foot. tpl"} {* end of page *}
Index. php


PHP: [Copy to clipboard]
<? Php
/*************************************** ******
*
* File name: index. php
* For use: Display instance programs
*
* Author: Master
* Email: teacherli@163.com

* Rectification: forest
**************************************** *****/
Include_once ("./comm/Smarty. class. php"); // contains the smarty class file

$ Smarty = new Smarty (); // Create a smarty instance object $ smarty
$ Smarty-> template_dir = "./templates"; // set the template directory
$ Smarty-> compile_dir = "./templates_c"; // Set the compilation Directory

//----------------------------------------------------
// Left and right boundary characters. The default value is {}.
// Conflict, so it is recommended to set it to <{}> or another.
//----------------------------------------------------
$ Smarty-> left_delimiter = "<{";
$ Smarty-> right_delimiter = "}> ";

$ Smarty-> assign ("name", "Li Xiaojun"); // replace Template variables

// Compile and display the index. tpl template under./templates
$ Smarty-> display ("index. tpl ");
?>


The final execution of this program will be shown:
======================================
Execute index. php
======================================
<Html>
<Head>
<Title> Master smarty tutorial </title>
</Head>
<Body>
Hello everyone, my name is Li Xiaojun. You are welcome to read my smarty learning materials.
<Hr>
<Center> CopyRight (C) by August 2004 <center>
<Hr>
</Body>
</Html>

Large-Scale Price Reduction
  • 59% Max. and 23% Avg.
  • Price Reduction for Core Products
  • Price Reduction in Multiple Regions
undefined. /
Connect with us on Discord
  • Secure, anonymous group chat without disturbance
  • Stay updated on campaigns, new products, and more
  • Support for all your questions
undefined. /
Free Tier
  • Start free from ECS to Big Data
  • Get Started in 3 Simple Steps
  • Try ECS t5 1C1G
undefined. /

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.