布局模板是通過(guò)設(shè)置layoutName屬性在視圖標(biāo)記中使用的Hhandlebars模板。{{yield}}助手通過(guò)通知布局模板確定插入主模板的位置。
Ember.View.extend({ layoutName: 'LayoutName', templateName: 'TemplateName' });
在上面的代碼中,定義要渲染的LayoutName和TemplateName。
<!DOCTYPE html> <html> <head> <title>Emberjs Adding Layouts to Views</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"> <!-- specifying the view name --> {{view "haslayout"}} </script> <script type="text/x-handlebars" data-template-name="mylayout"> <!-- rendered template will be inserted in the {{yield}} helper --> <h1> Hello.. {{yield}} </h1> </script> <script type="text/x-handlebars" data-template-name="haslayout"> <!-- displaying the value of name variable --> {{view.name}} </script> <script type="text/javascript"> App = Ember.Application.create(); App.HaslayoutView = Ember.View.extend({ //defining the view here name: 'Mack', //define the layout name 'mylayout' layoutName: 'mylayout', //defining the template name as 'haslayout' templateName: 'haslayout' }); </script> </body> </html>
讓我們執(zhí)行以下步驟,看看上面的代碼如何工作:
將上面的代碼保存在view_layout.html文件中
在瀏覽器中打開(kāi)此HTML文件。
更多建議: