Kzq*_*qai 9 templating mustache
所以我遇到了一些问题,围绕着最好的习惯方式,使用Mustache.php灵活地处理一些复杂的html案例
第一个是预先选择的选择下拉列表,例如
<select>
<option value=''></option>
<option value='bob'>Bob Williams</option>
<option value='james' selected>James Smith</option>
</select>
Run Code Online (Sandbox Code Playgroud)
我有办法解决这个问题,但我的方式似乎非常不灵活:
是否有一种很棒的方法可以使用部分或匿名函数或方法或我缺少的mustache.php的其他一些功能来制作预选的选择下拉列表?
编辑:将此问题缩减为单独的部分,以尽量提高清晰度.
bob*_*cow 20
在Mustache中执行此操作的惯用方法是创建View(或ViewModel),而不是传入数据哈希:
<?php
class Dropdown
{
public $name;
public $value;
private $options;
public function __construct($name, array $options, $value)
{
$this->name = $name;
$this->options = $options;
$this->value = $value;
}
public function options()
{
$value = $this->value;
return array_map(function($k, $v) use ($value) {
return array(
'value' => $k,
'display' => $v,
'selected' => ($value === $k),
)
}, array_keys($this->options), $this->options);
}
}
Run Code Online (Sandbox Code Playgroud)
然后你可以将它与dropdown部分...
<select name="{{ name }}">
{{# options }}
<option value="{{ value }}"{{# selected }} selected{{/ selected }}>
{{ display }}
</option>
{{/ options }}
</select>
Run Code Online (Sandbox Code Playgroud)
您可以在模板中使用哪个,如下所示:
{{# state }}
<label for="{{ name }}">State</label>
{{> dropdown }}
{{/ state }}
{{# country }}
<label for="{{ name }}">Country</label>
{{> dropdown }}
{{/ country }}
Run Code Online (Sandbox Code Playgroud)
并呈现它:
<?php
$data = array(
'state' => new Dropdown('state', $someListOfStates, 'CA'),
'country' => new Dropdown('country', $someListOfCountries, 'USA'),
);
$template->render($data);
Run Code Online (Sandbox Code Playgroud)
有了这个:
<?php
class StateDropdown extends Dropdown
{
static $states = array(...);
public function __construct($value, $name = 'state')
{
parent::__construct($name, self::$states, $value);
}
}
Run Code Online (Sandbox Code Playgroud)
还有这个:
<?php
class CountryDropdown extends Dropdown
{
static $countries = array(...);
public function __construct($value, $name = 'country')
{
parent::__construct($name, self::$countries, $value);
}
}
Run Code Online (Sandbox Code Playgroud)
其中一个:
<?php
class Address
{
public $street;
public $city;
public $state;
public $zip;
public $country;
public function __construct($street, $city, $state, $zip, $country, $name = 'address')
{
$this->street = $street;
$this->city = $city;
$this->state = new StateDropdown($state, sprintf('%s[state]', $name));
$this->zip = $zip;
$this->country = new CountryDropdown($country, sprintf('%s[country]', $name));
}
}
Run Code Online (Sandbox Code Playgroud)
投入一个新的address部分:
<label for="{{ name }}[street]">Street</label>
<input type="text" name="{{ name }}[street]" value="{{ street }}">
<label for="{{ name }}[city]">City</label>
<input type="text" name="{{ name }}[city]" value="{{ city }}">
{{# state }}
<label for="{{ name }}">State</label>
{{> dropdown }}
{{/ state }}
<label for="{{ name }}[zip]">Postal code</label>
<input type="text" name="{{ name }}[zip]" value="{{ zip }}">
{{# country }}
<label for="{{ name }}">Country</label>
{{> dropdown }}
{{/ country }}
Run Code Online (Sandbox Code Playgroud)
更新主模板:
<h2>Shipping Address</h2>
{{# shippingAddress }}
{{> address }}
{{/ shippingAddress }}
<h2>Billing Address</h2>
{{# billingAddress }}
{{> address }}
{{/ billingAddress }}
Run Code Online (Sandbox Code Playgroud)
去!
<?php
$data = array(
'shippingAddress' => new Address($shipStreet, $shipCity, $shipState, $shipZip, $shipCountry, 'shipping'),
'billingAddress' => new Address($billStreet, $billCity, $billState, $billZip, $billCountry, 'billing'),
};
$template->render($data);
Run Code Online (Sandbox Code Playgroud)
现在,您可以使用模块化,可重复使用,易于测试,可扩展的代码和部分代码.
请注意,我们创建的类是"Views"或"ViewModels".它们不是您的域模型对象......他们不关心持久性或验证,他们关心的只是为模板准备值.如果你也在使用模型,这会让它更容易,因为像我们的Address类这样的东西可以包装你的地址模型,并直接从模型中获取它需要的值,而不是要求你将一堆东西传递给构造函数.
如果您采用这种方法得出其逻辑结论,那么您的应用程序中每个操作/模板对最终会有一个顶级View或ViewModel类 - View可以在内部委托子视图和部分,就像我们从下拉列表中所做的那样我们的地址视图,但你有一个一流的View或ViewModel负责渲染每个动作.
意义(在MVC/MVVM世界中),您的Controller操作将执行它所需的任何"操作",然后创建负责填充模板的View或ViewModel类,将其交给几个域Model对象,并调用render on on模板.Controller不会准备任何数据,因为这是View层的责任.它只需简单地处理几个模型对象.
现在你所有的"渲染"逻辑都被整齐地封装在View层中,所有的标记都整齐地封装在你的模板文件中,你的模型没有丑陋的格式化业务,你的控制器很好,很轻,应该是:)