EmberJS 模板

2021-11-29 11:00 更新

模板用于跨多個頁面創(chuàng)建標準布局。更改模板時,基于該模板的頁面會自動更改。模板提供標準化控件。

應(yīng)用程序模板

如果您沒有在應(yīng)用程序中定義模板,Ember.js默認會呈現(xiàn)一個應(yīng)用程序模板。Ember.js模板放置在<script>標記中。您必須至少定義一個占位符:{{outlet}}路由器填寫相應(yīng)的模板。

<script type="text/x-handlebars">
   <div>
      {{outlet}}
   </div>
   <h2>{{App.name}}</h2>
</script>

在上面的代碼中,我們使用的是handlebar模板。它在腳本標記中聲明,有助于顯示應(yīng)用程序中的名稱等屬性。

例子

<!DOCTYPE html>
<html>
   <head>
      <title>Emberjs Template</title>
      <!-- CDN's-->
      <script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/3.0.1/handlebars.min.js"></script>
      <script src="https://code.jquery.com/jquery-2.1.3.min.js"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/ember.js/1.10.0/ember.min.js"></script>
      <script src="https://builds.emberjs.com/tags/v1.10.0-beta.3/ember-template-compiler.js"></script>
      <script src="https://builds.emberjs.com/release/ember.debug.js"></script>
      <script src="https://builds.emberjs.com/beta/ember-data.js"></script>
   </head>
   <body>
      <script type="text/x-handlebars" data-template-name="application">
         <!-- name of the template is application and it is default template -->
         <div>
            {{outlet}}
         </div>
         <!-- call the name value -->
         <h2><i>{{App.name}}</i></h2>
      </script>

      <script type="text/javascript">
         //creating the App varible by the Ember.Application's create() method
         var App = Ember.Application.create()
        //assigning value to the name variable
         App.name = "Hello... Welcome to TutorialsPoint!";
      </script>
   </body>
</html>

輸出

讓我們執(zhí)行以下步驟,看看上面的代碼如何工作:

  • 將上面的代碼保存在template.html文件中

  • 在瀏覽器中打開此HTML文件。

讓我們通過點擊以下鏈接查看一些有關(guān)模板的更多詳細信息:

以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號