【smarty】smartyのカスタム関数とカスタムブロック

3868 ワード

 、                 
<?php
   include_once("libs/Smarty.class.php");   //   smarty   
   $smarty=new Smarty();   //   smarty    $smarty
   /*
   $smarty->caching=false;  //       
   $smarty->template_dir="./templates/";  //       
   $smarty->compile_dir="./templates_c/";  //      
   $smarty->cache_dir="./smarty_cache/";  //      
   */
   //              {  }     <{ }>
   $smarty->left_delimiter="<{";
   $smarty->right_delimiter="}>";

   //        
   //          <{hsp times="10" size="5" color="red" con="hello,world"}>
 function test1($args){
    $str="";
    for($i=0;$i<$args['times'];$i++){
     $str.="<font color='".$args['color']."' size='".$args['size']."'>".$args['con']."</font>"."<br>";
    }
    return $str;
   }
   //     
   $smarty->register_function("hsp","test1");
   $smarty->display("hello.tpl");
?>
       
<{hsp times="10" size="5" color="green" con="hello,world"}>
 、            
<?php
   include_once("libs/Smarty.class.php");   //   smarty   
   $smarty=new Smarty();   //   smarty    $smarty
   /*
   $smarty->caching=false;  //       
   $smarty->template_dir="./templates/";  //       
   $smarty->compile_dir="./templates_c/";  //      
   $smarty->cache_dir="./smarty_cache/";  //      
   */
   //              {  }     <{ }>
   $smarty->left_delimiter="<{";
   $smarty->right_delimiter="}>";

   //         (   )
   function test2($args,$con){
    $str="";
    for($i=0;$i<$args['times'];$i++){
     $str.="<font color='".$args['color']."' size='".$args['size']."'>".$con."</font>"."<br>";
    }
    return $str;
   }
   //     
   $smarty->register_block("fun","test2");
   $smarty->display("hello.tpl");
?>
           
<{fun times="10" size="5" color="red"}>
hello,world
<{/fun}>

 、             
                      libs/plugins         php    
                 ,                ,    
         :function.      .php     :function.eqv.php
           &$smarty                 :function smarty_function_eqv($params, &$smarty)
        :function smarty_function_      ($params, &$smarty){     
                    
   }
  : libs/plugins          function.hsp.php   
       
<?php

function smarty_function_hsp($args, &$smarty){
    $str="";
    for($i=0;$i<$args['times'];$i++){
     $str.="<font color='".$args['color']."' size='".$args['size']."'>".$args['con']."</font>"."<br>";
    }
    return $str;
}
  
?>
        
<{hsp times="10" size="5" color="green" con="hello,world"}>

 、         

             ,            
      :block.  .php     :function.eqv.php
     :function smarty_block_  ($params,$content, &$smarty){    
  : libs/plugins           block.test.php   
       
<?php
function smarty_block_test($args, $con, &$smarty){
    $str="";
    for($i=0;$i<$args['times'];$i++){
     $str.="<font color='".$args['color']."' size='".$args['size']."'>".$con."</font>"."<br>";
    }
    return $str;
   }
?>
        
<{test times="10" size="5" color="yellow"}>
hello,world
<{/test}>