PHP 小胡子 - 多个部分
PHP Mustache - multiple partials
我发现这个 fiddle 展示了如何在 Mustache 模板中包含多个部分:http://jsfiddle.net/YW5zF/3/
我正在尝试将其转换为 PHP 小胡子,但遇到了一些困难。有人可以帮忙吗?
本质上,我试图了解如何在一个模板中多次重复使用输入模板。
这是我的主模板 (contact.mustache):
<form id="contact-form" method="post" action="contact.php" role="form">
<div class="messages"></div>
<div class="controls">
<div class="row">
<div class="col-md-6">
{{>input}}
</div>
<div class="col-md-6">
{{>input}}
</div>
</div>
<div class="row">
<div class="col-md-6">
{{>input}}
</div>
<div class="col-md-6">
{{>input}}
</div>
</div>
<div class="row">
<div class="col-md-12">
{{>textarea}}
</div>
<div class="col-md-12">
<input type="submit" class="btn btn-success btn-send" value="Send message">
</div>
</div>
<div class="row">
<div class="col-md-12">
<p class="text-muted"><strong>*</strong> These fields are required.</p>
</div>
</div>
</div>
</form>
这是我的部分输入 (input.mustache):
{{#input}}
<div class="{{div_class}}">
<label class="{{label_class}}" for="{{id}}">{{text}}</label>
<input type="{{type}}" id="{{id}}" name="{{name}}" class="{{input_class}}" placeholder="{{placeholder}}" {{required}} data-error="{{data-error}}">
<div class="{{line_class}}"></div>
</div>
{{/input}}
这是我的文本区域模板 (textarea.mustache)
{{#textarea}}
<div class="{{div_class}}">
<label class="{{label_class}}" for="{{id}}">{{text}}</label>
<textarea id="{{id}}" name="{{name}}" class="{{input_class}}" placeholder="{{placeholder}}" rows="{{rows}}" {{required}} data-error="{{data-error}}"></textarea>
<div class="{{line_class}}"></div>
</div>
{{/textarea}}
这是我的 PHP 代码来尝试渲染它:
<?php
require_once __DIR__ . '/vendor/autoload.php';
$m = new Mustache_Engine;
$data = array(
0 =>array(
"div_class"=>"form-group",
"id"=>"form_name",
"type"=>"text",
"name"=>"name",
"placeholder"=> "Please enter your firstname *",
"required"=>"required='required'"
"data-error"=>"Firstname is required.",
"input_class"=>"form-control",
"label_class"=>"floating-label",
"text"=>"Firstname *",
"line_class"=>"line-ripple",
),
1=> array(
"div_class"=>"form-group",
"id"=>"form_lastname",
"type"=>"text",
"name"=>"surname",
"placeholder"=> "Please enter your lastname *",
"required"=>"required='required'"
"data-error"=>"Lastname is required.",
"input_class"=>"form-control",
"label_class"=>"floating-label",
"text"=>"Lastname *",
"line_class"=>"line-ripple",
),
2 =>array(
"div_class"=>"form-group",
"id"=>"form_email",
"type"=>"email",
"name"=>"email",
"placeholder"=> "Please enter your email *",
"required"=>"required='required'"
"data-error"=>"Valid email is required.",
"input_class"=>"form-control",
"label_class"=>"floating-label",
"text"=>"Email *",
"line_class"=>"line-ripple",
),
3 =>array(
"div_class"=>"form-group",
"id"=>"form_phone",
"type"=>"tel",
"name"=>"phone",
"placeholder"=> "Please enter your phone",
"input_class"=>"form-control",
"label_class"=>"floating-label",
"text"=>"Phone",
"line_class"=>"line-ripple",
),
4 =>array(
"div_class"=>"form-group",
"id"=>"form_message",
"name"=>"message",
"placeholder"=> "Message for me *",
"required"=>"required='required'"
"data-error"=>"Please,leave us a message.",
"input_class"=>"form-control",
"label_class"=>"floating-label",
"rows"=>4,
"text"=>"Message *",
"line_class"=>"line-ripple",
)
);
// main templates in the /views folder, partials in the /views/partials folder
$m = new Mustache_Engine(array(
'loader' => new Mustache_Loader_FilesystemLoader(dirname(__FILE__) . '/views'),
'partials_loader' => new Mustache_Loader_FilesystemLoader(dirname(__FILE__) . '/views/partials'),
));
// loads template from `views/contact.mustache` and renders it.
echo $m->render("contact", $data);
这个脚本的当前结果是:
<form id="contact-form" method="post" action="contact.php" role="form">
<div class="messages"></div>
<div class="controls">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label class="floating-label" for="form_name">Firstname *</label>
<input type="text" id="form_name" name="name" class="form-control" placeholder="Please enter your firstname *" required='required' data-error="Firstname is required.">
<div class="line-ripple"></div>
</div>
<div class="form-group">
<label class="floating-label" for="form_lastname">Lastname *</label>
<input type="text" id="form_lastname" name="surname" class="form-control" placeholder="Please enter your lastname *" required='required' data-error="Lastname is required.">
<div class="line-ripple"></div>
</div>
<div class="form-group">
<label class="floating-label" for="form_email">Email *</label>
<input type="email" id="form_email" name="email" class="form-control" placeholder="Please enter your email *" required='required' data-error="Valid email is required.">
<div class="line-ripple"></div>
</div>
<div class="form-group">
<label class="floating-label" for="form_phone">Phone</label>
<input type="tel" id="form_phone" name="phone" class="form-control" placeholder="Please enter your phone" data-error="">
<div class="line-ripple"></div>
</div>
<div class="form-group">
<label class="floating-label" for="form_message">Message *</label>
<input type="" id="form_message" name="message" class="form-control" placeholder="Message for me *" required='required' data-error="Please,leave us a message.">
<div class="line-ripple"></div>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label class="floating-label" for="form_name">Firstname *</label>
<input type="text" id="form_name" name="name" class="form-control" placeholder="Please enter your firstname *" required='required' data-error="Firstname is required.">
<div class="line-ripple"></div>
</div>
<div class="form-group">
<label class="floating-label" for="form_lastname">Lastname *</label>
<input type="text" id="form_lastname" name="surname" class="form-control" placeholder="Please enter your lastname *" required='required' data-error="Lastname is required.">
<div class="line-ripple"></div>
</div>
<div class="form-group">
<label class="floating-label" for="form_email">Email *</label>
<input type="email" id="form_email" name="email" class="form-control" placeholder="Please enter your email *" required='required' data-error="Valid email is required.">
<div class="line-ripple"></div>
</div>
<div class="form-group">
<label class="floating-label" for="form_phone">Phone</label>
<input type="tel" id="form_phone" name="phone" class="form-control" placeholder="Please enter your phone" data-error="">
<div class="line-ripple"></div>
</div>
<div class="form-group">
<label class="floating-label" for="form_message">Message *</label>
<input type="" id="form_message" name="message" class="form-control" placeholder="Message for me *" required='required' data-error="Please,leave us a message.">
<div class="line-ripple"></div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label class="floating-label" for="form_name">Firstname *</label>
<input type="text" id="form_name" name="name" class="form-control" placeholder="Please enter your firstname *" required='required' data-error="Firstname is required.">
<div class="line-ripple"></div>
</div>
<div class="form-group">
<label class="floating-label" for="form_lastname">Lastname *</label>
<input type="text" id="form_lastname" name="surname" class="form-control" placeholder="Please enter your lastname *" required='required' data-error="Lastname is required.">
<div class="line-ripple"></div>
</div>
<div class="form-group">
<label class="floating-label" for="form_email">Email *</label>
<input type="email" id="form_email" name="email" class="form-control" placeholder="Please enter your email *" required='required' data-error="Valid email is required.">
<div class="line-ripple"></div>
</div>
<div class="form-group">
<label class="floating-label" for="form_phone">Phone</label>
<input type="tel" id="form_phone" name="phone" class="form-control" placeholder="Please enter your phone" data-error="">
<div class="line-ripple"></div>
</div>
<div class="form-group">
<label class="floating-label" for="form_message">Message *</label>
<input type="" id="form_message" name="message" class="form-control" placeholder="Message for me *" required='required' data-error="Please,leave us a message.">
<div class="line-ripple"></div>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label class="floating-label" for="form_name">Firstname *</label>
<input type="text" id="form_name" name="name" class="form-control" placeholder="Please enter your firstname *" required='required' data-error="Firstname is required.">
<div class="line-ripple"></div>
</div>
<div class="form-group">
<label class="floating-label" for="form_lastname">Lastname *</label>
<input type="text" id="form_lastname" name="surname" class="form-control" placeholder="Please enter your lastname *" required='required' data-error="Lastname is required.">
<div class="line-ripple"></div>
</div>
<div class="form-group">
<label class="floating-label" for="form_email">Email *</label>
<input type="email" id="form_email" name="email" class="form-control" placeholder="Please enter your email *" required='required' data-error="Valid email is required.">
<div class="line-ripple"></div>
</div>
<div class="form-group">
<label class="floating-label" for="form_phone">Phone</label>
<input type="tel" id="form_phone" name="phone" class="form-control" placeholder="Please enter your phone" data-error="">
<div class="line-ripple"></div>
</div>
<div class="form-group">
<label class="floating-label" for="form_message">Message *</label>
<input type="" id="form_message" name="message" class="form-control" placeholder="Message for me *" required='required' data-error="Please,leave us a message.">
<div class="line-ripple"></div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
</div>
<div class="col-md-12">
<input type="submit" class="btn btn-success btn-send" value="Send message">
</div>
</div>
<div class="row">
<div class="col-md-12">
<p class="text-muted"><strong>*</strong> These fields are required.</p>
</div>
</div>
</div>
</form>
为什么不正确?
编辑终于开始工作了,下面列出了更改...
这里是contact.mustache
<form id="contact-form" method="post" action="contact.php" role="form">
<div class="messages"></div>
<div class="controls">
{{>input}}
{{>textarea}}
<div class="col-md-12">
<input type="submit" class="btn btn-success btn-send" value="Send message">
</div>
<div class="row">
<div class="col-md-12">
<p class="text-muted"><strong>*</strong> These fields are required.</p>
</div>
</div>
</div>
</form>
这里是input.mustache
{{#input}}
<div class="row">
<div class="col-md-6">
<div class="{{div_class}}">
<label class="{{label_class}}" for="{{id}}">{{text}}</label>
<input type="{{type}}" id="{{id}}" name="{{name}}" class="{{input_class}}" placeholder="{{placeholder}}" {{required}} data-error="{{data-error}}">
<div class="{{line_class}}"></div>
</div>
</div>
</div>
{{/input}}
这里是textarea.mustache
{{#textarea}}
<div class="row">
<div class="col-md-12">
<div class="{{div_class}}">
<label class="{{label_class}}" for="{{id}}">{{text}}</label>
<textarea id="{{id}}" name="{{name}}" class="{{input_class}}" placeholder="{{placeholder}}" rows="{{rows}}" {{required}} data-error="{{data-error}}"></textarea>
<div class="{{line_class}}"></div>
</div>
</div>
</div>
{{/textarea}}
这是数据数组(代码保持不变):
$data = array(
"input"=>array(
0 =>array(
"div_class"=>"form-group",
"id"=>"form_name",
"type"=>"text",
"name"=>"name",
"placeholder"=> "Please enter your firstname *",
"required"=>"required='required'",
"data-error"=>"Firstname is required.",
"input_class"=>"form-control",
"label_class"=>"floating-label",
"text"=>"Firstname *",
"line_class"=>"line-ripple",
),
1=> array(
"div_class"=>"form-group",
"id"=>"form_lastname",
"type"=>"text",
"name"=>"surname",
"placeholder"=> "Please enter your lastname *",
"required"=>"required='required'",
"data-error"=>"Lastname is required.",
"input_class"=>"form-control",
"label_class"=>"floating-label",
"text"=>"Lastname *",
"line_class"=>"line-ripple",
),
2 =>array(
"div_class"=>"form-group",
"id"=>"form_email",
"type"=>"email",
"name"=>"email",
"placeholder"=> "Please enter your email *",
"required"=>"required='required'",
"data-error"=>"Valid email is required.",
"input_class"=>"form-control",
"label_class"=>"floating-label",
"text"=>"Email *",
"line_class"=>"line-ripple",
),
3 =>array(
"div_class"=>"form-group",
"id"=>"form_phone",
"type"=>"tel",
"name"=>"phone",
"placeholder"=> "Please enter your phone",
"input_class"=>"form-control",
"label_class"=>"floating-label",
"text"=>"Phone",
"line_class"=>"line-ripple",
),
),
"textarea"=>array(
0 =>array(
"div_class"=>"form-group",
"id"=>"form_message",
"name"=>"message",
"placeholder"=> "Message for me *",
"required"=>"required='required'",
"data-error"=>"Please,leave us a message.",
"input_class"=>"form-control",
"label_class"=>"floating-label",
"text"=>"Message *",
"rows"=>4,
"line_class"=>"line-ripple",
),
)
);
这是输出:
<form id="contact-form" method="post" action="contact.php" role="form">
<div class="messages"></div>
<div class="controls">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label class="floating-label" for="form_name">Firstname *</label>
<input type="text" id="form_name" name="name" class="form-control" placeholder="Please enter your firstname *" required='required' data-error="Firstname is required.">
<div class="line-ripple"></div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label class="floating-label" for="form_lastname">Lastname *</label>
<input type="text" id="form_lastname" name="surname" class="form-control" placeholder="Please enter your lastname *" required='required' data-error="Lastname is required.">
<div class="line-ripple"></div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label class="floating-label" for="form_email">Email *</label>
<input type="email" id="form_email" name="email" class="form-control" placeholder="Please enter your email *" required='required' data-error="Valid email is required.">
<div class="line-ripple"></div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label class="floating-label" for="form_phone">Phone</label>
<input type="tel" id="form_phone" name="phone" class="form-control" placeholder="Please enter your phone" data-error="">
<div class="line-ripple"></div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="form-group">
<label class="floating-label" for="form_message">Message *</label>
<textarea id="form_message" name="message" class="form-control" placeholder="Message for me *" rows="4" required='required' data-error="Please,leave us a message."></textarea>
<div class="line-ripple"></div>
</div>
</div>
</div>
<div class="col-md-12">
<input type="submit" class="btn btn-success btn-send" value="Send message">
</div>
<div class="row">
<div class="col-md-12">
<p class="text-muted"><strong>*</strong> These fields are required.</p>
</div>
</div>
</div>
</form>
真正有用的东西
PHP 渲染函数似乎只有两个参数,与 Javascript 不同。因此,删除第三个参数并将 {{>objPartial}} 重命名为 {{>input}} 以便它与文件匹配应该使其与 PHP 渲染器一起工作。
此外,每次在主模板中遇到分部时,它都会查看提供给它的数据,并根据分部为数组的每个索引创建代码。如果部分的每次出现仅针对数组的一个元素呈现,那么您的代码将完美运行。
至此,您几乎可以正常工作了,只需要再调整几处。
{{#input}}
<div class="col-md-6">
<div class="{{div_class}}">
<label class="{{label_class}}" for="{{id}}">{{text}}</label>
<input type="{{type}}" id="{{id}}" name="{{name}}" class="{{input_class}}" placeholder="{{placeholder}}" {{required}} data-error="{{data-error}}">
<div class="{{line_class}}"></div>
</div>
{{/input}}
并且在主模板中只有一个对 {{>input}} 的引用。
<form id="contact-form" method="post" action="contact.php" role="form">
<div class="messages"></div>
<div class="controls">
<div class="row">
{{>input}}
</div>
<div class="row">
<div class="col-md-12">
{{>textarea}}
</div>
<div class="col-md-12">
<input type="submit" class="btn btn-success btn-send" value="Send message">
</div>
</div>
<div class="row">
<div class="col-md-12">
<p class="text-muted"><strong>*</strong> These fields are required.</p>
</div>
</div>
</div>
</form>
有一种方法可以保留每两个元素一行 div 的格式,但是对于 bootstrap 列,除非您正在执行某种特殊格式,否则没有必要这样做。
旧答案(保留以防对其他人有用)
这里有一个轻微的调整,我相信会让这项工作成功。不是为每条内容调用部分,而是为整个内容数组调用一次。您的 $data 数组需要略有不同,所以这就是我所看到的:
$data = array(
"input"=>array(
array(
"div_class"=>"text-field", "id"=>"firstName",
"input_class"=>"text-field__input", "label_class"=>"floating-label",
"line_class"=>"line-ripple", "text"=>"Curly"
)
),
array(
"div_class"=>"text-field", "id"=>"lastName",
"input_class"=>"text-field__input",
"label_class"=>"floating-label",
"line_class"=>"line-ripple", "text"=>"Athos"
)
)
);
您的部分会略有变化以包含 .col-md-6:
{{#input}}
<div class="col-md-6">
<div class="{{div_class}}">
<input type="text" id="{{id}}" class="{{input_class}}">
<label class="{{label_class}}" for="{{id}}">{{text}}</label>
<div class="{{line_class}}"></div>
</div>
</div>
{{/input}}
那你可以替换
<div class="row">
<div class="col-md-6">
{{#firstname}}
{{>objPartial}}
{{/firstname}}
</div>
<div class="col-md-6">
{{#lastname}}
{{>objPartial}}
{{/lastname}}
</div>
</div>
和
<div class="row">
{{>objPartial}}
</div>
我发现这个 fiddle 展示了如何在 Mustache 模板中包含多个部分:http://jsfiddle.net/YW5zF/3/
我正在尝试将其转换为 PHP 小胡子,但遇到了一些困难。有人可以帮忙吗?
本质上,我试图了解如何在一个模板中多次重复使用输入模板。
这是我的主模板 (contact.mustache):
<form id="contact-form" method="post" action="contact.php" role="form">
<div class="messages"></div>
<div class="controls">
<div class="row">
<div class="col-md-6">
{{>input}}
</div>
<div class="col-md-6">
{{>input}}
</div>
</div>
<div class="row">
<div class="col-md-6">
{{>input}}
</div>
<div class="col-md-6">
{{>input}}
</div>
</div>
<div class="row">
<div class="col-md-12">
{{>textarea}}
</div>
<div class="col-md-12">
<input type="submit" class="btn btn-success btn-send" value="Send message">
</div>
</div>
<div class="row">
<div class="col-md-12">
<p class="text-muted"><strong>*</strong> These fields are required.</p>
</div>
</div>
</div>
</form>
这是我的部分输入 (input.mustache):
{{#input}}
<div class="{{div_class}}">
<label class="{{label_class}}" for="{{id}}">{{text}}</label>
<input type="{{type}}" id="{{id}}" name="{{name}}" class="{{input_class}}" placeholder="{{placeholder}}" {{required}} data-error="{{data-error}}">
<div class="{{line_class}}"></div>
</div>
{{/input}}
这是我的文本区域模板 (textarea.mustache)
{{#textarea}}
<div class="{{div_class}}">
<label class="{{label_class}}" for="{{id}}">{{text}}</label>
<textarea id="{{id}}" name="{{name}}" class="{{input_class}}" placeholder="{{placeholder}}" rows="{{rows}}" {{required}} data-error="{{data-error}}"></textarea>
<div class="{{line_class}}"></div>
</div>
{{/textarea}}
这是我的 PHP 代码来尝试渲染它:
<?php
require_once __DIR__ . '/vendor/autoload.php';
$m = new Mustache_Engine;
$data = array(
0 =>array(
"div_class"=>"form-group",
"id"=>"form_name",
"type"=>"text",
"name"=>"name",
"placeholder"=> "Please enter your firstname *",
"required"=>"required='required'"
"data-error"=>"Firstname is required.",
"input_class"=>"form-control",
"label_class"=>"floating-label",
"text"=>"Firstname *",
"line_class"=>"line-ripple",
),
1=> array(
"div_class"=>"form-group",
"id"=>"form_lastname",
"type"=>"text",
"name"=>"surname",
"placeholder"=> "Please enter your lastname *",
"required"=>"required='required'"
"data-error"=>"Lastname is required.",
"input_class"=>"form-control",
"label_class"=>"floating-label",
"text"=>"Lastname *",
"line_class"=>"line-ripple",
),
2 =>array(
"div_class"=>"form-group",
"id"=>"form_email",
"type"=>"email",
"name"=>"email",
"placeholder"=> "Please enter your email *",
"required"=>"required='required'"
"data-error"=>"Valid email is required.",
"input_class"=>"form-control",
"label_class"=>"floating-label",
"text"=>"Email *",
"line_class"=>"line-ripple",
),
3 =>array(
"div_class"=>"form-group",
"id"=>"form_phone",
"type"=>"tel",
"name"=>"phone",
"placeholder"=> "Please enter your phone",
"input_class"=>"form-control",
"label_class"=>"floating-label",
"text"=>"Phone",
"line_class"=>"line-ripple",
),
4 =>array(
"div_class"=>"form-group",
"id"=>"form_message",
"name"=>"message",
"placeholder"=> "Message for me *",
"required"=>"required='required'"
"data-error"=>"Please,leave us a message.",
"input_class"=>"form-control",
"label_class"=>"floating-label",
"rows"=>4,
"text"=>"Message *",
"line_class"=>"line-ripple",
)
);
// main templates in the /views folder, partials in the /views/partials folder
$m = new Mustache_Engine(array(
'loader' => new Mustache_Loader_FilesystemLoader(dirname(__FILE__) . '/views'),
'partials_loader' => new Mustache_Loader_FilesystemLoader(dirname(__FILE__) . '/views/partials'),
));
// loads template from `views/contact.mustache` and renders it.
echo $m->render("contact", $data);
这个脚本的当前结果是:
<form id="contact-form" method="post" action="contact.php" role="form">
<div class="messages"></div>
<div class="controls">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label class="floating-label" for="form_name">Firstname *</label>
<input type="text" id="form_name" name="name" class="form-control" placeholder="Please enter your firstname *" required='required' data-error="Firstname is required.">
<div class="line-ripple"></div>
</div>
<div class="form-group">
<label class="floating-label" for="form_lastname">Lastname *</label>
<input type="text" id="form_lastname" name="surname" class="form-control" placeholder="Please enter your lastname *" required='required' data-error="Lastname is required.">
<div class="line-ripple"></div>
</div>
<div class="form-group">
<label class="floating-label" for="form_email">Email *</label>
<input type="email" id="form_email" name="email" class="form-control" placeholder="Please enter your email *" required='required' data-error="Valid email is required.">
<div class="line-ripple"></div>
</div>
<div class="form-group">
<label class="floating-label" for="form_phone">Phone</label>
<input type="tel" id="form_phone" name="phone" class="form-control" placeholder="Please enter your phone" data-error="">
<div class="line-ripple"></div>
</div>
<div class="form-group">
<label class="floating-label" for="form_message">Message *</label>
<input type="" id="form_message" name="message" class="form-control" placeholder="Message for me *" required='required' data-error="Please,leave us a message.">
<div class="line-ripple"></div>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label class="floating-label" for="form_name">Firstname *</label>
<input type="text" id="form_name" name="name" class="form-control" placeholder="Please enter your firstname *" required='required' data-error="Firstname is required.">
<div class="line-ripple"></div>
</div>
<div class="form-group">
<label class="floating-label" for="form_lastname">Lastname *</label>
<input type="text" id="form_lastname" name="surname" class="form-control" placeholder="Please enter your lastname *" required='required' data-error="Lastname is required.">
<div class="line-ripple"></div>
</div>
<div class="form-group">
<label class="floating-label" for="form_email">Email *</label>
<input type="email" id="form_email" name="email" class="form-control" placeholder="Please enter your email *" required='required' data-error="Valid email is required.">
<div class="line-ripple"></div>
</div>
<div class="form-group">
<label class="floating-label" for="form_phone">Phone</label>
<input type="tel" id="form_phone" name="phone" class="form-control" placeholder="Please enter your phone" data-error="">
<div class="line-ripple"></div>
</div>
<div class="form-group">
<label class="floating-label" for="form_message">Message *</label>
<input type="" id="form_message" name="message" class="form-control" placeholder="Message for me *" required='required' data-error="Please,leave us a message.">
<div class="line-ripple"></div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label class="floating-label" for="form_name">Firstname *</label>
<input type="text" id="form_name" name="name" class="form-control" placeholder="Please enter your firstname *" required='required' data-error="Firstname is required.">
<div class="line-ripple"></div>
</div>
<div class="form-group">
<label class="floating-label" for="form_lastname">Lastname *</label>
<input type="text" id="form_lastname" name="surname" class="form-control" placeholder="Please enter your lastname *" required='required' data-error="Lastname is required.">
<div class="line-ripple"></div>
</div>
<div class="form-group">
<label class="floating-label" for="form_email">Email *</label>
<input type="email" id="form_email" name="email" class="form-control" placeholder="Please enter your email *" required='required' data-error="Valid email is required.">
<div class="line-ripple"></div>
</div>
<div class="form-group">
<label class="floating-label" for="form_phone">Phone</label>
<input type="tel" id="form_phone" name="phone" class="form-control" placeholder="Please enter your phone" data-error="">
<div class="line-ripple"></div>
</div>
<div class="form-group">
<label class="floating-label" for="form_message">Message *</label>
<input type="" id="form_message" name="message" class="form-control" placeholder="Message for me *" required='required' data-error="Please,leave us a message.">
<div class="line-ripple"></div>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label class="floating-label" for="form_name">Firstname *</label>
<input type="text" id="form_name" name="name" class="form-control" placeholder="Please enter your firstname *" required='required' data-error="Firstname is required.">
<div class="line-ripple"></div>
</div>
<div class="form-group">
<label class="floating-label" for="form_lastname">Lastname *</label>
<input type="text" id="form_lastname" name="surname" class="form-control" placeholder="Please enter your lastname *" required='required' data-error="Lastname is required.">
<div class="line-ripple"></div>
</div>
<div class="form-group">
<label class="floating-label" for="form_email">Email *</label>
<input type="email" id="form_email" name="email" class="form-control" placeholder="Please enter your email *" required='required' data-error="Valid email is required.">
<div class="line-ripple"></div>
</div>
<div class="form-group">
<label class="floating-label" for="form_phone">Phone</label>
<input type="tel" id="form_phone" name="phone" class="form-control" placeholder="Please enter your phone" data-error="">
<div class="line-ripple"></div>
</div>
<div class="form-group">
<label class="floating-label" for="form_message">Message *</label>
<input type="" id="form_message" name="message" class="form-control" placeholder="Message for me *" required='required' data-error="Please,leave us a message.">
<div class="line-ripple"></div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
</div>
<div class="col-md-12">
<input type="submit" class="btn btn-success btn-send" value="Send message">
</div>
</div>
<div class="row">
<div class="col-md-12">
<p class="text-muted"><strong>*</strong> These fields are required.</p>
</div>
</div>
</div>
</form>
为什么不正确?
编辑终于开始工作了,下面列出了更改...
这里是contact.mustache
<form id="contact-form" method="post" action="contact.php" role="form">
<div class="messages"></div>
<div class="controls">
{{>input}}
{{>textarea}}
<div class="col-md-12">
<input type="submit" class="btn btn-success btn-send" value="Send message">
</div>
<div class="row">
<div class="col-md-12">
<p class="text-muted"><strong>*</strong> These fields are required.</p>
</div>
</div>
</div>
</form>
这里是input.mustache
{{#input}}
<div class="row">
<div class="col-md-6">
<div class="{{div_class}}">
<label class="{{label_class}}" for="{{id}}">{{text}}</label>
<input type="{{type}}" id="{{id}}" name="{{name}}" class="{{input_class}}" placeholder="{{placeholder}}" {{required}} data-error="{{data-error}}">
<div class="{{line_class}}"></div>
</div>
</div>
</div>
{{/input}}
这里是textarea.mustache
{{#textarea}}
<div class="row">
<div class="col-md-12">
<div class="{{div_class}}">
<label class="{{label_class}}" for="{{id}}">{{text}}</label>
<textarea id="{{id}}" name="{{name}}" class="{{input_class}}" placeholder="{{placeholder}}" rows="{{rows}}" {{required}} data-error="{{data-error}}"></textarea>
<div class="{{line_class}}"></div>
</div>
</div>
</div>
{{/textarea}}
这是数据数组(代码保持不变):
$data = array(
"input"=>array(
0 =>array(
"div_class"=>"form-group",
"id"=>"form_name",
"type"=>"text",
"name"=>"name",
"placeholder"=> "Please enter your firstname *",
"required"=>"required='required'",
"data-error"=>"Firstname is required.",
"input_class"=>"form-control",
"label_class"=>"floating-label",
"text"=>"Firstname *",
"line_class"=>"line-ripple",
),
1=> array(
"div_class"=>"form-group",
"id"=>"form_lastname",
"type"=>"text",
"name"=>"surname",
"placeholder"=> "Please enter your lastname *",
"required"=>"required='required'",
"data-error"=>"Lastname is required.",
"input_class"=>"form-control",
"label_class"=>"floating-label",
"text"=>"Lastname *",
"line_class"=>"line-ripple",
),
2 =>array(
"div_class"=>"form-group",
"id"=>"form_email",
"type"=>"email",
"name"=>"email",
"placeholder"=> "Please enter your email *",
"required"=>"required='required'",
"data-error"=>"Valid email is required.",
"input_class"=>"form-control",
"label_class"=>"floating-label",
"text"=>"Email *",
"line_class"=>"line-ripple",
),
3 =>array(
"div_class"=>"form-group",
"id"=>"form_phone",
"type"=>"tel",
"name"=>"phone",
"placeholder"=> "Please enter your phone",
"input_class"=>"form-control",
"label_class"=>"floating-label",
"text"=>"Phone",
"line_class"=>"line-ripple",
),
),
"textarea"=>array(
0 =>array(
"div_class"=>"form-group",
"id"=>"form_message",
"name"=>"message",
"placeholder"=> "Message for me *",
"required"=>"required='required'",
"data-error"=>"Please,leave us a message.",
"input_class"=>"form-control",
"label_class"=>"floating-label",
"text"=>"Message *",
"rows"=>4,
"line_class"=>"line-ripple",
),
)
);
这是输出:
<form id="contact-form" method="post" action="contact.php" role="form">
<div class="messages"></div>
<div class="controls">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label class="floating-label" for="form_name">Firstname *</label>
<input type="text" id="form_name" name="name" class="form-control" placeholder="Please enter your firstname *" required='required' data-error="Firstname is required.">
<div class="line-ripple"></div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label class="floating-label" for="form_lastname">Lastname *</label>
<input type="text" id="form_lastname" name="surname" class="form-control" placeholder="Please enter your lastname *" required='required' data-error="Lastname is required.">
<div class="line-ripple"></div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label class="floating-label" for="form_email">Email *</label>
<input type="email" id="form_email" name="email" class="form-control" placeholder="Please enter your email *" required='required' data-error="Valid email is required.">
<div class="line-ripple"></div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label class="floating-label" for="form_phone">Phone</label>
<input type="tel" id="form_phone" name="phone" class="form-control" placeholder="Please enter your phone" data-error="">
<div class="line-ripple"></div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="form-group">
<label class="floating-label" for="form_message">Message *</label>
<textarea id="form_message" name="message" class="form-control" placeholder="Message for me *" rows="4" required='required' data-error="Please,leave us a message."></textarea>
<div class="line-ripple"></div>
</div>
</div>
</div>
<div class="col-md-12">
<input type="submit" class="btn btn-success btn-send" value="Send message">
</div>
<div class="row">
<div class="col-md-12">
<p class="text-muted"><strong>*</strong> These fields are required.</p>
</div>
</div>
</div>
</form>
真正有用的东西
PHP 渲染函数似乎只有两个参数,与 Javascript 不同。因此,删除第三个参数并将 {{>objPartial}} 重命名为 {{>input}} 以便它与文件匹配应该使其与 PHP 渲染器一起工作。
此外,每次在主模板中遇到分部时,它都会查看提供给它的数据,并根据分部为数组的每个索引创建代码。如果部分的每次出现仅针对数组的一个元素呈现,那么您的代码将完美运行。
至此,您几乎可以正常工作了,只需要再调整几处。
{{#input}}
<div class="col-md-6">
<div class="{{div_class}}">
<label class="{{label_class}}" for="{{id}}">{{text}}</label>
<input type="{{type}}" id="{{id}}" name="{{name}}" class="{{input_class}}" placeholder="{{placeholder}}" {{required}} data-error="{{data-error}}">
<div class="{{line_class}}"></div>
</div>
{{/input}}
并且在主模板中只有一个对 {{>input}} 的引用。
<form id="contact-form" method="post" action="contact.php" role="form">
<div class="messages"></div>
<div class="controls">
<div class="row">
{{>input}}
</div>
<div class="row">
<div class="col-md-12">
{{>textarea}}
</div>
<div class="col-md-12">
<input type="submit" class="btn btn-success btn-send" value="Send message">
</div>
</div>
<div class="row">
<div class="col-md-12">
<p class="text-muted"><strong>*</strong> These fields are required.</p>
</div>
</div>
</div>
</form>
有一种方法可以保留每两个元素一行 div 的格式,但是对于 bootstrap 列,除非您正在执行某种特殊格式,否则没有必要这样做。
旧答案(保留以防对其他人有用)
这里有一个轻微的调整,我相信会让这项工作成功。不是为每条内容调用部分,而是为整个内容数组调用一次。您的 $data 数组需要略有不同,所以这就是我所看到的:
$data = array(
"input"=>array(
array(
"div_class"=>"text-field", "id"=>"firstName",
"input_class"=>"text-field__input", "label_class"=>"floating-label",
"line_class"=>"line-ripple", "text"=>"Curly"
)
),
array(
"div_class"=>"text-field", "id"=>"lastName",
"input_class"=>"text-field__input",
"label_class"=>"floating-label",
"line_class"=>"line-ripple", "text"=>"Athos"
)
)
);
您的部分会略有变化以包含 .col-md-6:
{{#input}}
<div class="col-md-6">
<div class="{{div_class}}">
<input type="text" id="{{id}}" class="{{input_class}}">
<label class="{{label_class}}" for="{{id}}">{{text}}</label>
<div class="{{line_class}}"></div>
</div>
</div>
{{/input}}
那你可以替换
<div class="row">
<div class="col-md-6">
{{#firstname}}
{{>objPartial}}
{{/firstname}}
</div>
<div class="col-md-6">
{{#lastname}}
{{>objPartial}}
{{/lastname}}
</div>
</div>
和
<div class="row">
{{>objPartial}}
</div>