我应该创建一个完整的页面模板,包括所有表格、表单和其他内容(包括页眉和页脚),还是将所有表格、表单等放在自己的文件中,然后根据需要将它们添加到模板中。
以下是一个小例子来澄清问题...
我应该:
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();
或者
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,但我想知道具体原因。