PHP Learning Notes (36) Smarty using custom functions and code blocks (different smarty2 and smarty3) ____ functions

Source: Internet
Author: User



Class

init.smarty.php

<?php

	define ("ROOT", ".");

	Problem solving: Warning:strftime () [Function.strftime]:
	date_default_timezone_set ("Asia/shanghai");
	Include ROOT. " /libs/smarty.class.php ";
	
	$TPL = new Smarty ();
	
	Smarty initialization
	$tpl->template_dir=root. " /templates/";
	$tpl->compile_dir=root. " /templates_c/";
	The configuration file location
	$tpl->config_dir=root. " /configs/";
	$tpl->left_delimiter= "<!--{";
	$TPL->right_delimiter= "}-->";
	? >

One: Define the use of functions and blocks in PHP files

mysmarty.php

<?php/** * * * All of the following use variables are based on the prefix and suffix set <!--{}--> * * 1. In smarty, use the function * * 2 in the form of custom tags (similar to HTML tags). Use
 Smarty Function * 2.1 Single line tag *//smarty2 in PHP * $tpl->register_function ("Hello", "say");
 *//smarty3 Way * $tpl->registerplugin ("function", "Hello", "say");
 * <!--{Hello size=5 color= "green" content= "22222" num=5}--> * 2.2 Block Mark * PHP *//smarty2 Way
 * $tpl->register_block ("World", "test");
 *//smarty3 Way * $tpl->registerplugin ("Block", "World", "test"); * <!--{World size=8 color= "Red" num=4}--> * world<!--{$title}--><br> * <!--{/
 world}--> * 3 Value * php * $tpl->assign ("title", $title);
 * $TPL->assign ("Color", "pink");
 * $tpl->assign ("content", "content"); * Template (if the associative array needs to use the 1 side of the inverted quotation mark to say) The associated variable contains to be parsed first * you can use $color and $content * <!--{Hello size=5 color= $color content= in the page $co Ntent "num=5}--> * <!--{Hello size=5 color= $color contenT= "' $arr. One '" num=5}--> * * * * * * * *///If the file fails to load require will stop parsing PHP and the include will continue to execute require ' init.smarty.php ';
	
	$title = "This is a text title";
	$tpl->assign ("title", $title);
	$TPL->assign ("Color", "pink");
	$tpl->assign ("content", "content");
	
	$tpl->assign ("arr", Array ("One" => "arr"));
Register a line function//smarty2 the way//$tpl->register_function ("Hello", "say");
	$tpl->register_block ("World", "test");
	Smarty3 Way $tpl->registerplugin ("function", "Hello", "say");
	
	Registration block $TPL->registerplugin ("Blocks", "World", "test");
		/** * Defines a function that parameters are placed in an array of label properties * @param pass parameters $args/function Say ($args) {$html = ""; For ($i =0 $i < $args [num]; $i + +) {$html. = ' <font size= ' > '. $args [' size ']. ' color= '. $args [' Color ']. > '. $args ["Content"]. "
		</font><br> ";
	return $html;
	/** * Definition block, parameters to place label properties * @param pass Parameters $args * @param pass content $content * @param reserved reference variable $a * @param reserved Reference variable $b * *
		function test ($args, $content,& $a,& $b) {$html = ""; for ($i =0; $i < $args ["num"]; $i + +) {$html. = ' <font size= ' > '. $args [' size ']. ' color= '. $args [' Color ']. ' " > '. $content. "
		</font><br> ";
	return $html; //template file name can be arbitrarily defined: For example: MYSMARTY.TPL only content is HTML can be $tpl->display ("mysmarty.html");?>

Mysmarty.html

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >

Two: Add a new file definition function and block in the Smarty Plugin folder plugins

mysmarty.php

<?php
/**
 * 
 * * All use variables below are based on the prefix and suffix <!--{}--> *
 functions
 Write functions using * * 
 plug-in form
 * 	In the Plugins folder, you define a representation function that starts with a function 	: function.hello.php's functions name Smarty_function_hello ($args, $smarty)
 * 	block at the beginning of blocks: block.world.php function name Smarty_block_world ($args, $content,& $smarty)
 * * 
 * 		
 *
	//If the file fails to load require will stop parsing PHP, and include will continue to execute
	require ' init.smarty.php ';
	$title = "This is a text title";
	
	$tpl->assign ("title", $title);
	$TPL->assign ("Color", "pink");
	$tpl->assign ("content", "content");
	$tpl->assign ("arr", Array ("One" => "arr"));
	
	
	
	Template file name can be arbitrarily defined: For example: MYSMARTY2.TPL only content is HTML can be
	$tpl->display ("mysmarty2.html");
>

Mysmarty.html

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >

Under plugins in the Smarty Plugins folder:

function.hello.php (note naming rules)

<?php

	/**
	 * Defines a function that parameters are placed in an array of label properties
	 * @param passing parameters $args
	/function Smarty_function_hello ($args, & $smarty) {
		$html = "";
		For ($i =0 $i < $args [num]; $i + +) {
			$html. = ' <font size= ' > '. $args [' size ']. ' color= '. $args [' Color ']. > '. $args ["Content"]. " </font><br> ";
		}
		return $html;
	>

block.world.php (note naming rules)

<?php

	/**
	 * definition block, parameter array label attribute
	 * @param passing Parameters $args
	 * @param passing content $content
	 * @param reserved reference variables $a c17/>* @param reserved Reference variable $b
	/function Smarty_block_world ($args, $content,& $smarty) {
		$html = "";
		For ($i =0 $i < $args [num]; $i + +) {
			$html. = ' <font size= ' > '. $args [' size ']. ' color= '. $args [' Color ']. > '. $content. " </font><br> ";
		}
		return $html;
	>


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.