Mustache php:模板选择下拉列表的惯用方法

Mustache php:模板选择下拉列表的惯用方法,mustache,templating,Mustache,Templating,因此,在灵活使用Mustache.php处理一些复杂的html案例时,我遇到了一些问题 第一个是预先选择的下拉列表,例如 <select> <option value=''></option> <option value='bob'>Bob Williams</option> <option value='james' selected>James Smith</option> </select

因此,在灵活使用Mustache.php处理一些复杂的html案例时,我遇到了一些问题

第一个是预先选择的下拉列表,例如

<select>
  <option value=''></option>
  <option value='bob'>Bob Williams</option>
  <option value='james' selected>James Smith</option>
</select>

鲍勃·威廉姆斯
詹姆斯·史密斯
我有办法处理这件事,但我的方法似乎很僵硬:

  • 以php中的数组为例
  • 将其重新格式化为具有3个元素的多维数组;值,显示,选定(布尔值)
  • 将其传递到模板,在该模板中,选项、值和选定项以循环形式输出
有没有一种很棒的方法可以使用partials或匿名函数或方法或mustache.php的其他一些功能来创建预选的select下拉列表


编辑:将此问题缩减为单独的部分,以尽量提高清晰度。

在小胡子中实现这一点的惯用方法是创建视图(或视图模型),而不是传递散列数据:

<?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);
  }
}
您可以在模板中使用它,如下所示:

{{# state }}
  <label for="{{ name }}">State</label>
  {{> dropdown }}
{{/ state }}

{{# country }}
  <label for="{{ name }}">Country</label>
  {{> dropdown }}
{{/ country }}
更新您的主模板:

<h2>Shipping Address</h2>
{{# shippingAddress }}
  {{> address }}
{{/ shippingAddress }}

<h2>Billing Address</h2>
{{# billingAddress }}
  {{> address }}
{{/ billingAddress }}
送货地址
{{{#发货地址}
{{>地址}
{{/shippingAddress}
帐单地址
{{{billingAddress}
{{>地址}
{{/billingAddress}
走吧

<?php

$data = array(
  'shippingAddress' => new Address($shipStreet, $shipCity, $shipState, $shipZip, $shipCountry, 'shipping'),
  'billingAddress'  => new Address($billStreet, $billCity, $billState, $billZip, $billCountry, 'billing'),
};

$template->render($data);

bobthecow是一个很好的起点,但是代码中有一些错误

我不得不更改下拉类以支持kohana中的多选下拉,array_map要求您向函数传递相同数量的变量

下拉类:

class Tiaa_Dropdown
{
  public  $name;
  public  $value;
  private $options;

  public function __construct($name, array $options, $value)
  {
    $this->nameAttr    = $name;
    $this->options = $options;
    $this->value   = $value;
  }

  public function things()
  {
    $value = $this->value;
    $array_keys = array_keys($this->options);
    $array_values = array_values($this->options);
    return array_map(function($k, $v) use ($value) {

  if(is_array($value)) {
    $selected = (in_array($k, $value) ? 1 : 0);
  } else {
    $selected = ($value == $k);
  }

     return array(
        'value'    => $k,
        'display'  => $v,
        'selected' =>  $selected
      );
    }, $array_keys, $array_values);
  }
} // End

希望这对某人有所帮助。

hmm,我得花点时间来理解这一点。我的部分问题可能是,我正在使用参数化pdo对象来获取结果集,当需要修改模板的数据时,结果会变得有些不灵活,因此必须在结果集外的数据转储或其他一些不需要的选项之间进行选择。下面是使用带有胡子的“real ViewModel”的另一个示例:
<?php

class CountryDropdown extends Dropdown
{
  static $countries = array(...);

  public function __construct($value, $name = 'country')
  {
    parent::__construct($name, self::$countries, $value);
  }
}
<?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));
  }
}
<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 }}
<h2>Shipping Address</h2>
{{# shippingAddress }}
  {{> address }}
{{/ shippingAddress }}

<h2>Billing Address</h2>
{{# billingAddress }}
  {{> address }}
{{/ billingAddress }}
<?php

$data = array(
  'shippingAddress' => new Address($shipStreet, $shipCity, $shipState, $shipZip, $shipCountry, 'shipping'),
  'billingAddress'  => new Address($billStreet, $billCity, $billState, $billZip, $billCountry, 'billing'),
};

$template->render($data);
class Tiaa_Dropdown
{
  public  $name;
  public  $value;
  private $options;

  public function __construct($name, array $options, $value)
  {
    $this->nameAttr    = $name;
    $this->options = $options;
    $this->value   = $value;
  }

  public function things()
  {
    $value = $this->value;
    $array_keys = array_keys($this->options);
    $array_values = array_values($this->options);
    return array_map(function($k, $v) use ($value) {

  if(is_array($value)) {
    $selected = (in_array($k, $value) ? 1 : 0);
  } else {
    $selected = ($value == $k);
  }

     return array(
        'value'    => $k,
        'display'  => $v,
        'selected' =>  $selected
      );
    }, $array_keys, $array_values);
  }
} // End