2010-11-15 26 views
10

Tôi đang bị treo bộ ria mép cho một dự án mà tôi đã bắt đầu vào cuối tuần.Phần ria mép Mustache và sử dụng lại mã

Tôi đang sử dụng triển khai PHP. Tôi có, tuy nhiên một vài yêu cầu như tôi không được sử dụng để hệ thống.

Làm cách nào để bạn xử lý mẫu thừa kế hoặc sử dụng lại? Tôi biết về partials, nhưng làm thế nào tôi nên sử dụng chúng? Tôi đang làm một cái gì đó như thế này, ala bao gồm:

top.mustache:

<!DOCTYPE html> 
<html lang='es'> 
<head> 
    <meta charset=utf-8" /> 
    <link rel="stylesheet" href="/media/style.css" type="text/css" media="screen" /> 
</head> 
<body> 
    <header><h1><a href="/">Top</a></h1> 
    </header> 
    <section> 

bottom.mustache:

 </section> 
     <footer><a href="http://potajecreativo.com/">potaje</a></footer> 
</body> 
</html> 

Và một cái nhìn để render mẫu này:

{{>top}} 
<form action="/album/" method="post"> 
    <p><label for="name">Name</label> <input type="text" name="name" value=""/></p> 
    <p><label for="description">Description</label> <textarea name="description" rows="8" cols="40"></textarea></p> 
    <p><input type="submit" value="Save" /></p> 
</form> 
{{>bottom }} 

Đây có phải là phương pháp phù hợp không?

Trả lời

6

Dưới đây là ví dụ về cách triển khai php của công trình Mustache. Lưu ý là Mustache.php sẽ không phải là mở rộng partials/mẫu được bao gồm, vì vậy bạn phải đưa chúng đến bộ ria mép như bên dưới. Ví dụ này được ghép lại với nhau trên một khung công tác CakePHP cũ hơn.

<? 
    # php cannot recognize multiple acceptable file extensions for partials, 
    # so toggle it to mustache's extension 
    $this->ext = '.mustache'; 

    # Mustache says it's logic-less, but that's not exactly true. 
    # Render out the basic header which calls the logo partial and has 
    # a {{# logged_in? }} condition which dictates which user box we 
    # show. Thus, we need to render out the html for both the logged in 
    # and logged out user boxes 
    $basic_header_html = $this->renderElement('basic_header'); 
    $logo  = $this->renderElement('shared/logo'); 
    $logged_in = $this->renderElement('shared/logged_in_user_box'); 
    $logged_out = $this->renderElement('shared/logged_out_user_box'); 

    $m = new Mustache($basic_header_html,      
        array('logged_in?' => !empty($this->Auth->userData), 
          'cache_buster' => time(), 
          'display_name' => 'StackOverflow Customer'), 
        array('shared/logo' => $logo, 
          'shared/logged_in_user_box' => $logged_in, 
          'shared/logged_out_user_box' => $logged_out)); 
?> 

<!DOCTYPE html> 
<html> 
<head> 
    <title>Mustache Test</title> 
</head> 

<body> 
    <?= $m->render(); ?> 
</body> 
</html> 

basic_header.mustache

<div id="header" class="basic"> 
    {{> shared/logo }} 

    {{# logged_in? }} 
    {{> shared/logged_in_user_box }} 
    {{/ logged_in? }} 

    {{^ logged_in? }} 
    {{> shared/logged_out_user_box }} 
    {{/ logged_in? }} 
</div> 

chia sẻ/logo.mustache

<a class="logo" href="/foo/bar"><img alt="" src="/images/logo.png?{{ cache_buster }}" /></a> 

chia sẻ/logged_in_user_box.mustache

Hello {{display_name}}, you are logged in. 

chia sẻ câu trả lời/logged_out_user_box.mustache

Hello. You are not logged in. 
+5

Tôi giả sử bạn đang sử dụng khung Bánh trong ví dụ của bạn ở đây, mặc dù không có gì trong câu hỏi hoặc câu trả lời của bạn ám chỉ điều đó. Bạn có thể muốn làm rõ rằng trong câu trả lời của bạn để làm cho nó ít khó hiểu. – jsdalton

Các vấn đề liên quan