search
HomeBackend DevelopmentPHP Tutorialphplib里带的模板功能的确非常好用,但有个致命的小错误需要更正_PHP

    phplib里带的template.inc包含了一个模板类,用它可以实现程序代码和
界面的分离,给程序设计和界面设计带来了极大的方便,而且它的实现比快速
模板要好。但有一个致命的小错误导致set_block()函数不能正常使用,而这个
函数是非常重要的,它在处理重复块时显得非常的好用。
    下面通过一个最简单的例子来讲解用法及解决方法:
模板文件:page1.ihtml(名字你可以随便取,后缀ihtml是一种习惯用法)


{TITLE}



ID: {ID}

NAME: {NAME}






处理文件:test.php
  include("./template.inc");
  # 创建模板变量$t  
  $t = new Template(".", "keep");
  # 定义页面变量的引用,这样你就可以用page1来引用页面了
  $t->set_file("page1","page1.ihtml");
  # 提取名称为TBK的块,让它指向tbks的引用
  $t->set_block("page1", "TBK", "tbks");
  # 替换标题TITLE变量
  $t->set_var("TITLE","my test of template");
  # 定义ID和NAME,然后把TBK附加到tbks
  for ($i=1; $i    $n  = $i;
    $nn = $i*10;
    $t->set_var(array("ID" => $n, "NAME" => $nn));
    $t->parse("tbks", "TBK", true);
  }
  # 最后输出页面
  $t->pparse("out", "page1");
?>
   但是这个例子是不能运行的,网友Openball曾给出一种解决方案,把
$t->set_block("page1", "TBK", "tbks");改为$t->set_block("page1", "TBK", "{tbks}");注意最后的“{}”符号,这样的确可以用了,
但有更好的解决方案,打开template.inc文件,找到set_block()函数里的
这句话:$str = preg_replace($reg, "{$name}", $str);呵呵,看到没,
phplib已经做了处理了,但忘了(我猜的:)转义“{”,因此把这句话改
为:$str = preg_replace($reg, "\{$name}", $str);就可以了。这样,
phplib自带的例子也可以用了,而且这种方法似乎更规范些,不知各位的
已经如何,具体的还可以参照我在实际中用的模板和程序,欢迎交流经验
   模板:http://myjingpin.com/tpl/v1/show.html
   程序:http://myjingpin.com/app/v1/show.php?mid=flash
Statement
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
PHP Email: Step-by-Step Sending GuidePHP Email: Step-by-Step Sending GuideMay 09, 2025 am 12:14 AM

PHPisusedforsendingemailsduetoitsintegrationwithservermailservicesandexternalSMTPproviders,automatingnotificationsandmarketingcampaigns.1)SetupyourPHPenvironmentwithawebserverandPHP,ensuringthemailfunctionisenabled.2)UseabasicscriptwithPHP'smailfunct

How to Send Email via PHP: Examples & CodeHow to Send Email via PHP: Examples & CodeMay 09, 2025 am 12:13 AM

The best way to send emails is to use the PHPMailer library. 1) Using the mail() function is simple but unreliable, which may cause emails to enter spam or cannot be delivered. 2) PHPMailer provides better control and reliability, and supports HTML mail, attachments and SMTP authentication. 3) Make sure SMTP settings are configured correctly and encryption (such as STARTTLS or SSL/TLS) is used to enhance security. 4) For large amounts of emails, consider using a mail queue system to optimize performance.

Advanced PHP Email: Custom Headers & FeaturesAdvanced PHP Email: Custom Headers & FeaturesMay 09, 2025 am 12:13 AM

CustomheadersandadvancedfeaturesinPHPemailenhancefunctionalityandreliability.1)Customheadersaddmetadatafortrackingandcategorization.2)HTMLemailsallowformattingandinteractivity.3)AttachmentscanbesentusinglibrarieslikePHPMailer.4)SMTPauthenticationimpr

Guide to Sending Emails with PHP & SMTPGuide to Sending Emails with PHP & SMTPMay 09, 2025 am 12:06 AM

Sending mail using PHP and SMTP can be achieved through the PHPMailer library. 1) Install and configure PHPMailer, 2) Set SMTP server details, 3) Define the email content, 4) Send emails and handle errors. Use this method to ensure the reliability and security of emails.

What is the best way to send an email using PHP?What is the best way to send an email using PHP?May 08, 2025 am 12:21 AM

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

Best Practices for Dependency Injection in PHPBest Practices for Dependency Injection in PHPMay 08, 2025 am 12:21 AM

The reason for using Dependency Injection (DI) is that it promotes loose coupling, testability, and maintainability of the code. 1) Use constructor to inject dependencies, 2) Avoid using service locators, 3) Use dependency injection containers to manage dependencies, 4) Improve testability through injecting dependencies, 5) Avoid over-injection dependencies, 6) Consider the impact of DI on performance.

PHP performance tuning tips and tricksPHP performance tuning tips and tricksMay 08, 2025 am 12:20 AM

PHPperformancetuningiscrucialbecauseitenhancesspeedandefficiency,whicharevitalforwebapplications.1)CachingwithAPCureducesdatabaseloadandimprovesresponsetimes.2)Optimizingdatabasequeriesbyselectingnecessarycolumnsandusingindexingspeedsupdataretrieval.

PHP Email Security: Best Practices for Sending EmailsPHP Email Security: Best Practices for Sending EmailsMay 08, 2025 am 12:16 AM

ThebestpracticesforsendingemailssecurelyinPHPinclude:1)UsingsecureconfigurationswithSMTPandSTARTTLSencryption,2)Validatingandsanitizinginputstopreventinjectionattacks,3)EncryptingsensitivedatawithinemailsusingOpenSSL,4)Properlyhandlingemailheaderstoa

See all articles

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version