PHP smarty Template inheritance. Implement parent templates (view master pages and content pages) with built-in functions extends and blocks

Source: Internet
Author: User
Tags smarty template

Template Inheritance (Content page Inherits master page): Implements code reuse.

Resolving code reuse issues:
Scenario 1: Use include all (include the equivalent of copy and paste, which will form a large number of the same page, occupy bandwidth)
Scenario 2: Full use of extends (recommended use of the scheme)
Scenario 3: One using include, one using extends (not recommended)


Steps:
The first step is to create a parent template for the child template to inherit, layout, and template layout.
The second step is to create a child template that inherits the parent template through the built-in function {extends}.
In the third step, if you need to overwrite the contents of the parent template, you need to implement it through the built-in function {block}, which needs to be used in both the parent template and the child template.


Pay attention to the details
1, Layout page can have many blocks, child page can also have many blocks, through the Name property to associate.
2, the child page in addition to extends and block other content does not show
3, Layout page block can have the default content, the child page does not realize directly display, the implementation is covered.
4, layout page block can be nested with each other, the implementation of the child can be targeted.
5. {$smarty. block.child} layout can invoke the contents of the child, {$smarty. block.parent} child pages can invoke the parent page content

extends.php (back end):

<?php
include "libs/smarty.class.php";
$smarty = new Smarty ();

$smarty->template_dir = "Templates";
$smarty->compile_dir = "Templates_c";

$smarty->display (' Order.tpl ');
$smarty->display (' List.tpl ');
layout.tpl (Parent template page (view master page)):
<! DOCTYPE html>
order.tpl (Child template page, on: Left/right: Bottom layout structure):
{Extends file = ' Layout.tpl '}  {* Child template page (content page), extends Inherits parent template (View master page) *}
{Block name = ' main '}  The {* Block overrides the corresponding block in the parent template (View master page). The uncovered block is based on the parent template's output *}{* overwrite block (main) implementation: The next layout structure *}
	<div class= "main" >
		order content
	</div>
{/block}


list.tpl (Sub Template page, on:: Bottom layout structure):
{Extends file = "Layout.tpl"}  
{Block name = ' content '}    {* Overlay block (content) implementation: The layout structure under: *}
	<div class= "Content" >
		Product List Contents
	</div>
{/block}


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.