Discover how to dynamically change the layout using content in the database. Check out this episode for details.
script/generate migration add_layout_to_blogs layout_name:string custom_layout_content:text
rake db:migrate
gem install liquid
# controllers/application.rb
def load_blog
@current_blog = Blog.find_by_subdomain(current_subdomain)
if @current_blog.nil?
flash[:error] = "Blog invalid"
redirect_to root_url
else
self.class.layout(@current_blog.layout_name || 'application')
end
end
# config/environment.rb
config.gem 'liquid'
<!-- layouts/custom.html.erb -->
<%= Liquid::Template.parse(@current_blog.custom_layout_content).
render('page_content' => yield, 'page_title' => yield(:title)) %>
<!-- blogs/_form.html.erb -->
<p>
<%= f.label :layout_name %><br />
<%= f.select :layout_name, [["Standard", 'application'], ["Plain", 'plain'], ["Custom", 'custom']] %>
</p>
<p>
<%= f.label :custom_layout_content %><br />
<%= f.text_area :custom_layout_content, :rows => 12 %>
</p>
UPDATE: As Pratik mentioned in the comments, the above code is not thread safe. Here is a better way to handle the layout which is thread safe.
# application.rb
layout :set_layout
def load_blog
@current_blog = Blog.find_by_subdomain(current_subdomain)
if @current_blog.nil?
flash[:error] = "Blog invalid"
redirect_to root_url
end
end
def set_layout
(@current_blog && @current_blog.layout_name) || 'application'
end
分享到:
相关推荐
Build dynamic UI layouts in JavaFX and using the JavaFX UI controls Create charts in JavaFXDesign and deploy for embedded, mobile and tablet Leverage JavaFX languages and markup Who This Book Is ...
在"Layouts in Razor"部分中,文档讲解了如何在Razor模板中使用布局来提高代码的重用性。布局允许开发者创建一个具有预定义内容的页面框架,然后在各个页面中重用这个框架,仅需修改框架内的特定部分。内容占位符、...
GAF applications offer a more dynamic and flexible approach, suitable for applications requiring more interactive elements and layouts. ###### 2.2.3 Creating a Simple Header (for OIF and GAF) Headers...
- **Dynamic Configuration**:支持动态更新日志配置。 #### 14. Logging Separation Log4j2 支持日志分离,即在一个应用程序中使用多个独立的日志记录配置: - **Separate ClassLoaders**:使用不同的类加载器...
Android library for inflating dynamic layouts in runtime based on JSON configuration fetched from server. Useful in situations when layouts need to change without updating the app. Advanced features ...
作者讲解了如何运用磁贴(Live Tiles)、动态版面(Dynamic Layouts)以及触控友好性来创造流畅的用户体验。此外,还涵盖了如何利用Windows 8的特性,如Charms栏、开始屏幕和App Bar,来增强应用的互动性和功能性。 ...
在 iOS 8 中使用 Auto Layouts 引入的Self Sizing Cells示例以及不正确自动计算 UILabel 的preferredMaxLayoutWidth的解决方法。 ##介绍 在 iOS 6 中,Apple 向 UILabel 添加了preferredMaxLayoutWidth属性,...
- **Input Management Settings**: Settings related to input management, such as keyboard layouts and input methods, must be consistent across devices. ##### 3.9 Device Administration Device ...
#### 第5章:Building Dynamic UI Layouts in JavaFX - **目标读者**:需要创建自适应布局的开发者。 - **内容概览**: - 使用各种布局管理器构建动态界面的方法。 - 如何根据窗口大小变化调整布局。 - 实现响应...
- **Manufacturing Systems:** In manufacturing systems, TS has been used to optimize machine layouts, tooling selections, and assembly line configurations to enhance overall system performance. ...
You’ll also implement key data-binding techniques that will make your user interfaces dynamic, and create personalized animations and visual effects within your user interfaces using Custom ...
You’ll also implement key data-binding techniques that will make your user interfaces dynamic, and create personalized animations and visual effects within your user interfaces using Custom ...
Content controls are used to display dynamic content in WPF applications. Chapters 5 and Appendix B cover controls like `Button`, `Label`, `TextBlock`, and `TextBox`, among others. They explain how to...
DWM,全称Dynamic Window Manager,是一款轻量级的X窗口管理器,专为那些追求简洁、高效操作环境的Linux用户设计。它基于C语言编写,强调小巧、快速,并且允许高度自定义。DWM的灵活性使得用户可以根据自己的需求...
石墨烯 一个基于G6的图分析工具包 English | :sparkles: 特征 :artist_palette: 外观漂亮,样式配置标准化 ... 对于复杂业务场景中的功能,例如layout switching , dynamic layouts , sub-graph layou
DDR2 SDRAM (Double Data Rate 2 Synchronous Dynamic Random Access Memory) represents an advancement in memory technology, specifically designed to simplify and optimize the layout of printed circuit ...
You'll start with in-depth explanations of D3's out-of-the-box layouts, along with dozens of practical use cases that align with different types of visualizations. Then, you'll explore practical ...
It supports a variety of tags and expressions that make it easy to manipulate data and generate complex layouts. 4. **Menus and Views**: These components define the navigation structure and the ...