English 中文(简体)
• 如何在PHP中模版?
原标题:How to template in PHP... the right way?
  • 时间:2009-09-29 17:35:25
  •  标签:

我开始理解购买力平价中的排他性的重要性。 I m 利用PHP本身作为避免学习任何新语言的模板发动机。 我仍然不清楚一点,尽管我对此有想法。 我想知道专家们做了些什么。

如果我建立一个完整的网页模板,包括所有表格、表格和所有其他材料(包括头盔和脚.),或者如果我把所有表格、表格等放在自己的档案中,然后按需要添加到模板中。

在这里,一个小小的例子可以澄清......

a) 创建一页,列出所有表格、表格和其他内容,并将之作为最后产品使用

// Set all the information
$template->title = $username;
$template->user_info = get_user($user_id);
$template->articles = get_articles($user_id);
$template->ads = random_ad($user_info);

// Load the template with everything already in it
$template->load( user_page.tpl )
$template->display();

页: 1

b) 在其档案中创建每一表、表格、相关部件,然后使用这些元素制造最后产品

// set the title and load the header template
$header[ title ] = $username;
$template->load( header.tpl , $header);

// get the user info as an array and load into user profile template
$user_info = get_user($user_id);
$template->load( user_profile.tpl );

// load the new article form
$template->load( new_article_form.tpl );

// get the articles as an array of info and load into user articles template
$articles = get_articles($user_id);
$template->load( user_articles.tpl , $articles);

// get a random ad based on user info
$ads = random_ad($user_info);
$template->load( advertisements.tpl );

// load the footer template and display final page
$template->load( footer.php );
$template->display();

如果每个文件装上,在最后一页需要显示的部分。

由于你本人的技巧,我想B,但我想知道什么和为什么。

最佳回答

我个人说,第一种做法是最好的,因为它使所有文件和文件都能够逐条完成。

第二种做法意味着,你在座标上有<条码>和>;div>。 由<代码></div>在你的脚本上封闭。 插头(除了可能还有几个方面适用)。 这意味着,如果你改变你的布局,在某个地方增加一个包裹(例如),你必须记住在另一个档案中也关闭(或者根据你的布局,有两个或三个不同的档案)。

几个不同的嵌入档案使情况更加恶化。 想一想的是,如果一个档案(包括有条件的)有额外的<代码>和>;/div>,那么就很难去掉一个网站。 你得到一份模糊的“有时,这页看上去是完全的,什么是我使用的”,很难追踪。 如果你重新使用基于桌的布局,则使母亲和母亲部更糟。

采用第一种办法,你仍然可以采用南半球原则。 你将模板装成一个变量。 指称:

$userVars[ name ] = $currentUser->name;
$templateVars[ userinfo ] = $template->load( userinfo.php , $userVars);

...
$template->display( template.tpl , $templateVars);

你们可以不断找到这种方式的文件。 有许多好处:

  • Each file is semantically complete HTML - all tags that are opened, are also closed in the same document. It s easy to edit one part of the layout without breaking (possibly unknowningly) anything else.
  • It s each to cache the output of certain templates, and re-use them
  • It s easy to apply AJAX to the site. For example, you have a stats.tpl template rendering inside a <div id="stats"> on the first page load. You also have a view that just renders the stats.tpl template by itself, and then use jquery to do $( #stats ).load( /ajaxstats.php ), which refreshes that div but without repeating code at all.
问题回答

每一模板(例如布局、头部/脚部)所共有结构的模板(包括可再利用的借方(例如形式))的模版继承。

方法 如果没有继承,你即为“共同点”(“共同点”),或“共同点”、“共同点”、“共同点”、“共同点”(更糟)。 • 一切照样照样处理B> ;

为此,我确实建议使用真正的、专用的排外发动机,而不是便衣。 它们使生活更加容易(遗产是一件事;另一件是变式的汽车。

模板继承可能会使最清洁的法典化。 您现在可在直截了当的网址上使用

没有好的或坏的解决办法。 有一些技术和准则,但是,在时间上,你将学习比其他办法更好的办法。

仅看一下,就会说,第二种做法使你更加灵活,将页数分成小部分。 较弱的人有时可能意味着更易于管理。

第二项解决办法还允许1名以上人员在网页上工作,因为他们只需要工作/更换网页的部分而不是整页。

目 录





相关问题
热门标签