PHP Learning Notes (37) The variable adjuster in Smarty is used

Source: Internet
Author: User
Tags modifier require

Basically Smarty's built-in regulator is already rich enough to meet simple requirements. To find out or add a new adjuster after you've learned about it


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= "}-->";
	? >

mysmarty.php

<?php
/**
 * * 
 * All use variables are based on the prefix and suffix
 * variable adjuster
 * You 		want to pass a string of uppercase, intercept, lowercase, then you need to pass three strings
 * A 		variable adjuster can be used in a template using |functionname;
 * 		functionname The first parameter is $STR, how to add other parameters can be added: To add parameters
 * 		<!--{$str |tochange: "Lower"}-->
 * 		<!--{$str |tochange: "Ten": "}-->"
 * Two methods set
 * 	1. Write in PHP
 * 	2. Add modifier.tochange.php * 3 to the Plugins folder 	. Combination Adjuster * *
 system- 
 supplied variable regulator (see Smarty API) 
 * Can own Baidu has been verified plug-ins
 * * 
 can also find the format plug-in *
 * 	
 * */
	//If the file load failed require will stop parsing PHP , while the include continues to execute
	require ' init.smarty.php ';
	
	$title = "This is a text title";
	$str = "I am A-Bay boy!";
	
	
	$tpl->assign ("title", $title);
	$TPL->assign ("str", $STR);
	$tpl->assign ("Strc", $title);
	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" >

Add the following file to the Smarty Plugin plugins folder for custom adjuster use

modifier.tocut.php

<?php
	function Smarty_modifier_tocut ($str, $star, $len) {
		$str =substr ($str, $star, $len);
		return $str;
	}

modifier.tochange.php

<?php
	function Smarty_modifier_tochange ($str, $mod) {
		switch ($mod) {case
			"lower":
				$str = Strtolower ($STR);
				break;
			Case "Upper":
				$str =strtoupper ($STR);
				break;
			Case "a":
				$str =ucfirst ($STR);
				break;
		return $str;
	}


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.