English 中文(简体)
B. 制作模板驱动的PHP站点的首选指南
原标题:Beginner s guide to making a template driven PHP site

你们是否知道能够帮助我开始在PHP制造模版驱动的场地?

最佳回答

根据你们的需要,你可以建立自己的模版系统,包括几个档案。

问题回答

首先,我用智慧开车,因为我已经用了许多东西。 既然这是一个社区主人,如果你选择,就可自由地 examples击你们自己的排气发动机。

Installing Smarty

时间结构安排为你的超文本,将你的超文本内容与你的逻辑代码分开。 它还为组织您的超文本页面的各个组成部分提供了一种途径,从而便于管理,而且可以更重新使用。

从一开始,我们就用智慧展示一个简单的网页。 首先,你们实际上需要获得智慧。 因此,我们向Smarty下载网页。 截至撰写本报告时,最新的稳定版本是3.07聪明的,因此,我们走在前面并下载。 <代码>.tar.gz file or the .zip file is up to the system You have. 对于Windows和Macuser(很可能使用MAMPXAMPP)来说,.zip<>> 代码可能是个好选择,尽管你总是能够处理<代码>.tar.gz文档。 拥有短链氯化石蜡或∗BSD型系统的用户可填充.tar.gz 版本。

现在,我们抽取档案,并填写以下目录:

COPYING.lib
demo/ 
libs/
|__ Smarty.class.php
|__ ... some other files and folders ...
README
SMARTY2_BC_NOTES

如今,Sarty.ster.php是我们重新纳入使用Sarty的主要档案。 它位于校准局。 由于校正是一种相对通用的校正名称,我们将把这一名称复制到我们的文件根数,并改名为<编码>Smarty。 现在,我们的理论文件根植于现在空洞,因此,我们最终将视此:

Smarty/
|__ Smarty.class.php
|__ ... some other files and folders ...

A Simple Template

既然已经安装了智能,我们便制作了一个非常基本的模板和网站,以显示它是如何运作的。 首先,为了保存我们组织的物品,我们要打上“<>条码/条码>的夹,以保持我们的模板。 然后,我们创建<条码>米页>.php文档和<条码>米页>tpl。 载于<代码>的文档:

mypage.php
templates/
|__ mypage.tpl
Smarty/
|__ Smarty.class.php
|__ ... some other files and folders ...

<>mypage.php>

<?php
require_once( Smarty/Smarty.class.php );

$smarty = new Smarty();
$smarty->assign("MYVAR", "Hello World");
$smarty->display("templates/mypage.tpl");
?>

<>>templates/mypage.tpl

<html>
<head>
  <title>My Sample Page</title>
</head>
<body>
<h1>{$MYVAR}</h1>
</body>
</html>

现在,如果我们浏览mypage.php,例如http:// localhost:8000/mypage.php,“Hello World”将用大体黑体表示。 因此发生了什么? 让我们通过守则:

require_once( Smarty/Smarty.class.php );

在这方面,我们包括主要的智慧阶层。 我们需要任何和所有聪明的功能。

$smarty->assign("MYVAR", "Hello World");

你们会利用这一命运。 指派指挥在这方面具有价值,即“世界之声”,并将其指定为“世界之声”。 如果看一下模板:

<h1>{$MYVAR}</h1>

页: 1{}s 表示需要评估内部表述。 这可以是指挥,也可以是变数。 它可以产生产出,或只是设定一个可变值。 在这种情况下,它只是将我们分配的<代码>$MYVAR的价值输出。

$smarty->display("templates/mypage.tpl");

最后,我们告诉Smarty说,该模板是仿照你在PHP网页上简单显示的。 Fancy eh? 现在,许多人希望通过阵列(如一系列成果)来振兴,因此,请看一下如何实现这一目标的榜样。

Looping Through Arrays

第一,我们调整了我们的法典。 新名单如下:

<>mypage.php>

<?php
require_once( Smarty/Smarty.class.php );

$myarray = array(
"John",
"Jane",
"Henry",
"Nancy",
"Gorilla"
);

$smarty = new Smarty();
$smarty->assign("MYARRAY", $myarray);
$smarty->display("templates/mypage.tpl");
?>

<>>templates/mypage.tpl

<html>
<head>
  <title>My Sample Page</title>
</head>
<body>
<h1>Results</h1>

<ul>
{foreach $MYARRAY as $myvalue}
  <li>{$myvalue}</li>
{/foreach}
</ul>
</body>
</html>

如果你重新上下页,你就会照此办理:

https://i.stack.imgur.com/wIAGk.png” alt=“Result of Code”/>

现在只有几处改动来说明:

$smarty->assign("MYARRAY", $myarray);

Instead of a string value, we assigned a variable which holds an array. This was passed to Smarty which used the data in the {foreach} loop:

<ul>
{foreach $MYARRAY as $myvalue}
  <li>{$myvalue}</li>
{/foreach}
</ul>

The foreach in Smarty is much like the foreach of PHP. In this case, Smarty takes the array that was assigned to $MYVALUE and loops through it, setting $myvalue to the result of the current loop value. From there we can use {$myvalue} to refer to each individual item. Now, let s say we want to make this unordered list a widget to reuse in other places. We can do that through using templates themselves as variables.

Template Modularization

HTML can get very long very quickly. Smarty helps manage this by letting you break out parts of the page into individual components. So what we ll do is take our unordered list and put it into a separate page. Our new code will look like this:

<>mypage.php>

<?php
require_once( Smarty/Smarty.class.php );

$myarray = array(
"John",
"Jane",
"Henry",
"Nancy",
"Gorilla"
);

$smarty = new Smarty();
$smarty->assign("MYARRAY", $myarray);
$content = $smarty->fetch("templates/mycontent.tpl");

$smarty->assign("MYCONTENT", $content);
$smarty->display("templates/mypage.tpl");
?>

<>>templates/mypage.tpl

<html>
<head>
  <title>My Sample Page</title>
</head>
<body>
<h1>Results</h1>

{$MYCONTENT}

</body>
</html>

<>mycontent.tpl

<ul>
{foreach $MYARRAY as $myvalue}
  <li>{$myvalue}</li>
{/foreach}
</ul>

The result we get is the same, however the backend is now more organized. We can now reuse this mycontent.tpl file in other pages if we want. Common usages of this organization is making header, footer, and other parts of the page individual templates. This lets you narrow down to relevant pieces.

So what happened on the backend? The important command to take note of here is:

$content = $smarty->fetch("templates/mycontent.tpl");

fetch() works like display, except that instead of outputting it right away, it returns the result as a string of the rendered HTML. Note that because $MYARRAY is used in the mycontent.tpl, we have to assign the array right before it, not right before the final display() call. This ordering is important!

Conclusion

这结束了对聪明语的基本介绍,并利用了一种辅助发动机来管理你的内容。 Smarty Documentation为了解所有婚姻都有能力提供大量资源。 确保通过所有现有的指挥机构,确保你高效利用这一指挥!

Smarty for PHP is an adequate templating system. You can try and use that, plus their documentation to help develop a template driven website.





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签