简体   繁体   English

渲染应用程序范围的视图以及Ruby on Rails中的特定视图

[英]Render application wide view plus specific view in Ruby on Rails

Hello StackOverflow community, Hello StackOverflow社区,

I would like to know on how can I achieve application wide plus page specific view on Ruby on Rails. 我想知道如何在Ruby on Rails上实现应用程序范围加页面特定视图。

I put all of my navbar, footer etc in the application wide view (application.html.erb) so it acts as a base layout (which I think the real purpose of application wide view) to every view on my app. 我将所有导航栏,页脚等放在应用程序范围的视图(application.html.erb)中,因此它充当了我的应用程序上每个视图的基本布局(我认为应用程序范围视图的真正目的)。

From what I see now, Rails puts both code of say, application wide view and home/index view on runtime. 从我现在看到的,Rails将运行时的代码,应用程序范围的视图和主页/索引视图放在一起。 Links to stylesheets, scripts etc is a no problem and I am glad that they're being imported in app-wide view, saves time. 样式表,脚本等链接是没有问题的,我很高兴他们在应用程序范围内导入,节省时间。

How do I specify Rails to import the assets & contents for a specific view, for example, home/index? 如何指定Rails以导入特定视图的资产和内容,例如home / index?

How do I form the code in the following manner or so? 如何以下列方式形成代码?

app wide view (meta tags, navbar, header, global stylesheets) + page specific content (title, body) + app wide view (footer, javascripts) app wide view(元标记,导航栏,标题,全局样式表)+页面特定内容(标题,正文)+ app宽视图(页脚,javascripts)

I think best way to approach it is to split it to header, body and footer. 我认为最好的方法就是把它分成标题,正文和页脚。 Should I create another file and let it act as footer and let app.html.erb be the header? 我应该创建另一个文件并让它作为页脚,让app.html.erb成为标题?

Thanks! 谢谢!

The most common approach is to include both the header and footer in your Layout , yielding the page content in between. 最常见的方法是在Layout包含页眉和页脚,从而产生页面内容。

If you have assets that you want to include only in particular pages, or that need to be rendered elsewhere in your layout, you can use content_for and yield to achieve this. 如果您希望资产仅包含在特定页面中,或者需要在布局中的其他位置呈现,则可以使用content_foryield来实现此目的。

For example, in your page view: 例如,在您的页面视图中:

<% content_for :title do %>The title of this page!<% end %>

And in your layout, where you want the title tag to be output: 在您的布局中,您希望输出标题标记:

<title><%= content_for?(:title) ? yield(:title) : "Default page title goes here" %></title>

The same approach can be used for javascripts or stylesheets that you want to include only on particular pages. 对于您希望仅包含在特定页面上的javascripts或样式表,可以使用相同的方法。

For example, in some page that needs jQuery: 例如,在某些需要jQuery的页面中:

<% content_for :per_page_javascripts do %>    
  <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<% end %>

And in your layout (the header or footer, wherever you think JS should go): 在您的布局(页眉或页脚,无论您认为JS应该去哪里):

<%= yield(:per_page_javascripts) %>

Here's a guide that might help: http://guides.rubyonrails.org/layouts_and_rendering.html#using-the-content-for-method 这是一个可能有用的指南: http//guides.rubyonrails.org/layouts_and_rendering.html#using-the-content-for-method

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM