将模板放在我的 MVC 结构中的什么位置?在哪里放置定义?
Where to put templates in my MVC structure? And where to put defines?
我的 MVC 中有以下结构:
1) app
1. controllers
Home.class.php
2. core
App.class.php
Controller.class.php
3. models
User.class.php
4. views
- home
index.php
.htaccess
init.php
2) public
1. css
2. js
.htaccess
index.php
现在我也想实现模板化。我之前使用以下代码在我的项目中使用模板。
<?php
class Template
{
private $assignedValues = array();
private $tpl;
public function __construct($_path = '')
{
if(!empty($_path)){
if(file_exists($_path)){
$this->tpl = file_get_contents($_path);
}
else{
echo '<b>Template Error:</b> File Inclusion Error.';
}
}
}
public function assign($_searchString, $_replaceString)
{
if(!empty($_searchString)){
$this->assignedValues[strtoupper($_searchString)] = $_replaceString;
}
}
public function show()
{
if(count($this->assignedValues > 0)){
foreach ($this->assignedValues as $key => $value) {
$this->tpl = str_replace('{'.$key.'}', $value, $this->tpl);
}
}
echo $this->tpl;
}
}
那么我必须把这个 class 放在哪里?包含模板的文件夹在哪里?我还使用了带有定义的默认文件。放置我的定义文件的最佳位置是什么?
为什么不在:
app/views/layouts/your-template.php
对我来说,将它与其他基于 "view" 的结构放在一起很有意义,因为它是一个模板。
Laravel(php 框架)也是这样做的。
我的 MVC 中有以下结构:
1) app
1. controllers
Home.class.php
2. core
App.class.php
Controller.class.php
3. models
User.class.php
4. views
- home
index.php
.htaccess
init.php
2) public
1. css
2. js
.htaccess
index.php
现在我也想实现模板化。我之前使用以下代码在我的项目中使用模板。
<?php
class Template
{
private $assignedValues = array();
private $tpl;
public function __construct($_path = '')
{
if(!empty($_path)){
if(file_exists($_path)){
$this->tpl = file_get_contents($_path);
}
else{
echo '<b>Template Error:</b> File Inclusion Error.';
}
}
}
public function assign($_searchString, $_replaceString)
{
if(!empty($_searchString)){
$this->assignedValues[strtoupper($_searchString)] = $_replaceString;
}
}
public function show()
{
if(count($this->assignedValues > 0)){
foreach ($this->assignedValues as $key => $value) {
$this->tpl = str_replace('{'.$key.'}', $value, $this->tpl);
}
}
echo $this->tpl;
}
}
那么我必须把这个 class 放在哪里?包含模板的文件夹在哪里?我还使用了带有定义的默认文件。放置我的定义文件的最佳位置是什么?
为什么不在:
app/views/layouts/your-template.php
对我来说,将它与其他基于 "view" 的结构放在一起很有意义,因为它是一个模板。
Laravel(php 框架)也是这样做的。