2012-05-08 1 views
0

방금 ​​moustache.js에 대해 배웠고 HTML 템플릿을 만들려고합니다. 그러나 설명서는 해독하기가 약간 어렵습니다. 부분적인 부분을 설명하는 좋은 사이트를 찾았습니다. 나는 누군가가 어떻게해야 할지를 '나를 시작하게'할 수 있기를 바랬다.JSON을 사용하여 moustache.js html 템플릿 만들기

PHP :

은 내가 템플릿으로 변환 할 필요가 PHP 파일이있는 AJAX

$(function() { 
    $('#all').addClass('ui-selected') 
    $("#selectable").selectable({ 
     selected: updatefilters 

    }); 
    function updatefilters(ev, ui){ 
     // get the selected filters 
     var template, html; 
     var $selected = $('#selectable').children('.ui-selected'); 
     // create a string that has each filter separated by a pipe ("|") 
     var filters = $selected.map(function(){return this.id;}).get().join("\|"); 
     $.ajax({ 
      type: "POST", 
      url: 'updatefilters', 
      dataType: 'json', 
      data: { filters: filters }, 
      success: function(data){ 
       var template = "<div id ='board'>TESTING{{#body}}<table>"; 
       var partials = "{{#subject}}"; 
       //for (i=0 ; i< data.length ; i++) 
       //{html += "<tr><td>" + data[i].subject + "</td><td>" + data[i].body + "</td></tr>";} 
       //html += "</table></div>"; 
       var html = Moustache.to.html(template, data, partials); 
       $('#board').html(html); 
      } 
     }); 
    } 
}); 

임 여기

<div id='board'> 
<?php 
if ($threads) 
    { 
     $count = count($threads); 
     $perpage = 17; 
     $startlist = 3; 
     $numpages = ceil($count/$perpage); 
     if ($page == 'home') {$page = 1;} 
     $threadstart = ($page * $perpage) - $perpage; 
     $i = 0; 
     echo "<table class='board'> 
    <tr> 
      <th width='5%'><img src='".base_url()."img/board/icons/category_icon.png' alt='category_icon'/></th> 
      <th width='5%'>Tag</th> 
      <th width='50%'>Subject</th> 
      <th width='7.5%'>Author</th> 
      <th width='7.5%'>Replies/Views</th> 
      <th width='15%'>Last Post</th> 
    </tr>"; 
     foreach ($threads as $list) 
     { $i++; 
      $thread_owner = $this->thread_model->get_owner($list['owner_id']); 
      $thread_id = $list['thread_id']; 
      $query = $this->db->query("SELECT f.tag FROM filter_thread AS ft 
             INNER JOIN filter AS f ON ft.filter_id = f.filter_id 
             WHERE thread_id = $thread_id"); 
      $result = $query->result_array(); 
      $trunc_body = $this->thread_model->get_reply_summary($thread_id); 
      $filter = ""; 
      $filter2 =""; 
      $ent = "entertainment"; 
      foreach ($result as $string) 
      { 
       if ($string['tag'] == $ent) { 
        $filter2 .= "<div id='entertainment'><p>"; 
        $filter2 .= "<img src='".base_url()."img/board/icons/entertainment_icon.png' title='Entertainment' alt='Entertainment'/>"; 
        $filter2 .= "</p></div>"; 
       } else { 
       $filter2 .= "<div id='misc'><p>"; 
       $filter2 .= "<img src='".base_url()."img/board/icons/misc_icon.png' title='Misc' alt='Misc'/>"; 
       $filter2 .= "</p></div>"; 
       $filter .= " [".$string['tag']."]"; 
       } 
      } 
      if (!$result) { $filter = "" AND $filter2 =""; } 
      if ($i > $threadstart AND $i <= $threadstart + $perpage) 
      { 
       echo "<tr>"; 
       echo "<td>".$filter2."</td>"; 
       echo "<td>".$filter."</td>"; 
       echo "<td title='".$trunc_body['0']['body']."'><a href=".base_url()."main/thread/".$list['slug'].">".ucfirst($list['subject'])."<div id='filter'><p></p></div></a></td>"; 
       echo "<td><p>".$thread_owner."</p></td>"; 
       echo "<td align='right'>Replies: ".$list['replies_num']."<br>Views: ".$list['views']."</td>"; 
       $owner = $this->thread_model->get_owner($list['last_post_id']); 
       echo "<td>".$owner." <br>".$list['replystamp'] ."</td></tr>"; 
      } 
     } 
     echo "</table>"; 
     echo "<br>"; 
     echo "Current Page: $page | ";  
     echo "Page: "; 

     for ($n = 1; $n < $numpages+1; $n++) 
     { 
      echo "<a href=".base_url()."main/home/$n>$n</a> | "; 
     } 
    } 
?> 
</div> 

그리고이 확실하지 통해 이동하는 방법 moustache ..와 JSON 배열 .. 그리고 그것은 주석 부분 밖으로 않습니다 비록 그것은 페이지를 업데이 트하지 않습니다.

미리 감사드립니다. 의

답변

1

먼저 당신이 데이터를 분석하면 당신은 당신이 그것을 구문 분석해야합니다 JSON 문자열 (사용 $.parseJSON(data))

를 수신하는 모든 경우에 당신은 JSON 속성 throught를 반복해야합니다.

: 본체 : {콘텐츠 [dataObj {데이터 : 'A'} dataObj [데이터 'B']} JSON 다음있는 경우

<div>{{#body}}<div> 
<div>{{#content}}<div> 
<div>{{#dataObj}}<div> 
<span>{{data}}<span> 

partial을 사용하는 경우 기본 템플릿 내에서 호출해야합니다.

moustache5 설명서를 확인하면 많이 개선되었습니다.