Home Database Mysql Tutorial phplib中的一些基本语法和函数

phplib中的一些基本语法和函数

Jun 07, 2016 pm 06:02 PM
function grammar

phplib中常用的方法有set_file,set_block,set_var,parse,ppasre,p,get等。

语法介绍:
  phplib中常用的方法有set_file,set_block,set_var,parse,ppasre,p,get等。
  声明:由于本系统采用的是phplib,如果页面中有大括号对,这将会替换成空白,所以在写此文章时,用"[[","]]"来替代大括号。大家在用的时候是用大括号便是,此处仅为写文章方便而作此约定。
  set_file:是用来引入模板文件。
  用法:
代码如下:
  $t->set_file("show_main","main.htm");
  或
  $t->set_file(array(
  "show_header"=>"header.htm",
  "show_main"=>"main.htm"
  ));

  set_block:用来声明一个区块
  用法:
  $t->set_block("show_main","rowlist","RL");
  稍微解释一下,show_main是用set_file取得的文件句柄,rowlist是模板页面中的区域标识一般如下方式来写
  
代码如下:
  
  


  
  
  
  
  
  
[[param]]


  如上是将作为了一个区块,这样就可以用循环来生成多行的列表了
  区块是可以嵌套的
  
代码如下:
  
  
  
  
  
  
  
  
  
  
[[param]]


  如上所示,这声明一个嵌套区块,这在boeiBlog的像册部分采用了这种方式,有兴趣的朋友可以找出来看看
  对于嵌套的模板,我们可以这样来使用
  $t->set_block("show_main","rowlist","RL"); // 里面的参数从前向后依次是包含的关系,最后一个是别名,主要用来区块识别
  $t->set_block("rowlist","collist","CL"); // 第一个参数是外层块的名称,第二个是自己的名乐,第三个是别名
  循环这样的区块时要特别注意
  如下:
  
代码如下:
  
  $t->set_block("show_main","rowlist","RL");
  $t->set_block("rowlist","collist","CL");
  for($i=0;$i  {
   $t->set("CL");// 这里要对追加的列循环执行一次清理,否则会多出一堆东西
   for($ii=0;$ii   {
   $t->set_var("param","boeiBlog");
   $t->parse("CL","collist",true);// true参数表明这是追加
   }
   $t->parse("RL","rowlist",true);// 这里的true也是表追加
  }

  上述代码将会产生一个5X5的表格,每个单元格里会出现一个boeiBlog
  set_var:用来作变量替换
  上述代码里的$t->set_var("param","boeiBlog");就是把模板中的param变量替换成boeiBlog这个字符串,当然也可以替换成变量,如:
  
代码如下:
  
  $curdate = date("Y-m-d");
  $t->set_var("param",$curdate);
  set_var也有追加属性,如:
  
代码如下:
  
  $curdate = date("Y-m-d");
  for($i=0;$i  {
   $t->set_var("param","
".$curdate,true);
  }

  这将产生十个连续的当前日期
  有时候可以用set_var的追加属性来替代block的循环.
  set_var是可以用数组的,如:
  
代码如下:
  
  $t->set_var(array(
  "param"=>"boeiBlog",
  "title"=>"柏艾网络"
  ));

  模板如下:
  
代码如下:
  
  
  
  
  
  
[[param]],[[title]]


  parse:用于解析文件
  当我们将模板中的所有变量都处理完之后,可以用parse一将这个模板进行解析。这是模板处理的最后几道工序。
  如:
  
代码如下:
  
  $t->set_file("show_index","index.htm");
  $t->set_file("show_main","main.htm");
  $t->set_var("param","boeiBlog");
  $t->parse("main","show_main");

  我们所用的模板可能是:
  
代码如下:
  
  main.htm
  
  
  
  
  
[[param]]


  如果此时还有另外一个模板,其结构如下:
  
  
  index.htm
  
[[main]]

  那么上述代码将会把main.htm中的变量替换成boeiBlog后再放到index.htm中的main处,最后形成一个在
标签中的表格
  解析完成之后便是输出页面,
  p:用于输出页面
  如:
  
代码如下:
  
  $t->set_file("show_index","index.htm");
  $t->set_file("show_main","main.htm");
  $t->set_var("param","boeiBlog");
  $t->parse("main","show_main");
  $t->parse("index","show_index");
  $t->p("index");// 此处便会将整个index页面输出,注意main.htm已经被嵌入到index.htm,所以不用$t->p("main");

  pparse:同p一样也用来输出页面
  如:
  
代码如下:
  
  上述代码可以如下简化
  $t->set_file("show_index","index.htm");
  $t->set_file("show_main","main.htm");
  $t->set_var("param","boeiBlog");
  $t->parse("main","show_main");
  $t->pparse("index","show_index");// 此处将p和parse结合到一起,立即完成解析并输出

  get:用于获得文件内容
  如:
  
代码如下:
  
  $t->set_file("show_index","index.htm");
  $t->set_file("show_main","main.htm");
  $t->set_var("param","boeiBlog");
  $t->parse("main","show_main");
  $t->parse("index","show_index");
  $getstr = $t->get("index");
  echo $getstr;// 你将会看到这实际上和p是一样的。

  利用get,我们可以轻松的取得生成页面的内容,这可以用于静态页面的生成。可以看到phplib用来处理静态页面是非常方便的
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Tips for dynamically creating new functions in golang functions Tips for dynamically creating new functions in golang functions Apr 25, 2024 pm 02:39 PM

Go language provides two dynamic function creation technologies: closure and reflection. closures allow access to variables within the closure scope, and reflection can create new functions using the FuncOf function. These technologies are useful in customizing HTTP routers, implementing highly customizable systems, and building pluggable components.

Considerations for parameter order in C++ function naming Considerations for parameter order in C++ function naming Apr 24, 2024 pm 04:21 PM

In C++ function naming, it is crucial to consider parameter order to improve readability, reduce errors, and facilitate refactoring. Common parameter order conventions include: action-object, object-action, semantic meaning, and standard library compliance. The optimal order depends on the purpose of the function, parameter types, potential confusion, and language conventions.

How to write efficient and maintainable functions in Java? How to write efficient and maintainable functions in Java? Apr 24, 2024 am 11:33 AM

The key to writing efficient and maintainable Java functions is: keep it simple. Use meaningful naming. Handle special situations. Use appropriate visibility.

Complete collection of excel function formulas Complete collection of excel function formulas May 07, 2024 pm 12:04 PM

1. The SUM function is used to sum the numbers in a column or a group of cells, for example: =SUM(A1:J10). 2. The AVERAGE function is used to calculate the average of the numbers in a column or a group of cells, for example: =AVERAGE(A1:A10). 3. COUNT function, used to count the number of numbers or text in a column or a group of cells, for example: =COUNT(A1:A10) 4. IF function, used to make logical judgments based on specified conditions and return the corresponding result.

Comparison of the advantages and disadvantages of C++ function default parameters and variable parameters Comparison of the advantages and disadvantages of C++ function default parameters and variable parameters Apr 21, 2024 am 10:21 AM

The advantages of default parameters in C++ functions include simplifying calls, enhancing readability, and avoiding errors. The disadvantages are limited flexibility and naming restrictions. Advantages of variadic parameters include unlimited flexibility and dynamic binding. Disadvantages include greater complexity, implicit type conversions, and difficulty in debugging.

What are the benefits of C++ functions returning reference types? What are the benefits of C++ functions returning reference types? Apr 20, 2024 pm 09:12 PM

The benefits of functions returning reference types in C++ include: Performance improvements: Passing by reference avoids object copying, thus saving memory and time. Direct modification: The caller can directly modify the returned reference object without reassigning it. Code simplicity: Passing by reference simplifies the code and requires no additional assignment operations.

What are the syntax and structure characteristics of lambda expressions? What are the syntax and structure characteristics of lambda expressions? Apr 25, 2024 pm 01:12 PM

Lambda expression is an anonymous function without a name, and its syntax is: (parameter_list)->expression. They feature anonymity, diversity, currying, and closure. In practical applications, Lambda expressions can be used to define functions concisely, such as the summation function sum_lambda=lambdax,y:x+y, and apply the map() function to the list to perform the summation operation.

What is the difference between custom PHP functions and predefined functions? What is the difference between custom PHP functions and predefined functions? Apr 22, 2024 pm 02:21 PM

The difference between custom PHP functions and predefined functions is: Scope: Custom functions are limited to the scope of their definition, while predefined functions are accessible throughout the script. How to define: Custom functions are defined using the function keyword, while predefined functions are defined by the PHP kernel. Parameter passing: Custom functions receive parameters, while predefined functions may not require parameters. Extensibility: Custom functions can be created as needed, while predefined functions are built-in and cannot be modified.

See all articles