クライアント側のテンプレートはすでに現実のものです

背景


VKontakteソーシャルネットワーク用のIFrameアプリケーションを開発しています。 アプリケーションをナビゲートする最も便利な方法は、ページ全体をリロードせずにデータを動的にロードすることです。 以前、私はEJS-JavaScript Templatesに出会うまで、サーバーに表示する必要があるhtmlコードを生成しました...

EJS-組み込みJavaScript


EJSは、私にとって最も便利で適切なテンプレートエンジンの1つであることがわかりました。 単一の変数と配列(オブジェクトの読み取り)の両方で機能し、ロジック(if ... else ...)があります。

このテンプレートエンジンで何ができるかを例を示してみましょう。

テンプレート-/templates/question.ejs:
<div>
  <% if(question) { %>
   <h2><%= author %>: <%= question %></h2>
   <div><textarea name="answer" id="answer"></textarea></div>
   <ul class="nNav btnList">
     <li>
      <a href="" onclick="ACT.question.answer('index'); return false;"></a>
     </li>
   </ul>
  <% } else { %>
   <h2> , !</h2>
  <% } %>
</div>

* This source code was highlighted with Source Code Highlighter.


— /data/question.php:
{"id":"98","question":"What are you doing now?","author":"Mihalich88"}

* This source code was highlighted with Source Code Highlighter.


:
$.ajax({
  type: "POST",
  url: "/data/question.php",
  dataType: "json",
  data: data,
  success: function(ans){
   var html = new EJS({url: ' /templates/question.ejs'}).render(ans);
  }
});


* This source code was highlighted with Source Code Highlighter.



+ «»
  1. , .. json-,
  2. , ..
  3. MVC , «V»
  4. Opera Mobile 10

— «»
  1. , RIA

, EJS: http://formspring.vk-app.ru
: *.ejs. , *.tpl, — … , , .


Source: https://habr.com/ru/post/J104628/


All Articles