`
CaiDeHen
  • 浏览: 94255 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

#125 Dynamic Layouts

阅读更多
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
分享到:
评论

相关推荐

    Pro JavaFX 9: A Definitive Guide to Building Desktop, Mobile, and Embedded Java

    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 ...

    Razor Razor原版文档

    在"Layouts in Razor"部分中,文档讲解了如何在Razor模板中使用布局来提高代码的重用性。布局允许开发者创建一个具有预定义内容的页面框架,然后在各个页面中重用这个框架,仅需修改框架内的特定部分。内容占位符、...

    Floor Plan Manager - ABAP Webdynpro

    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...

    log4j2用户指南

    - **Dynamic Configuration**:支持动态更新日志配置。 #### 14. Logging Separation Log4j2 支持日志分离,即在一个应用程序中使用多个独立的日志记录配置: - **Separate ClassLoaders**:使用不同的类加载器...

    Android代码-dynamico

    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 ...

    Designing for Windows 8_Fundamentals of Great Design in Windows Store Apps

    作者讲解了如何运用磁贴(Live Tiles)、动态版面(Dynamic Layouts)以及触控友好性来创造流畅的用户体验。此外,还涵盖了如何利用Windows 8的特性,如Charms栏、开始屏幕和App Bar,来增强应用的互动性和功能性。 ...

    iOS8SelfSizingCells:在 iOS 8 中使用 Auto Layouts 引入的 Self Sizing Cells 示例以及错误自动计算 UILabel 的 preferredMaxLayoutWidth 的解决方法

    在 iOS 8 中使用 Auto Layouts 引入的Self Sizing Cells示例以及不正确自动计算 UILabel 的preferredMaxLayoutWidth的解决方法。 ##介绍 在 iOS 6 中,Apple 向 UILabel 添加了preferredMaxLayoutWidth属性,...

    Android 4.0 Compatibility Definition

    - **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 ...

    Pro JavaFX 8.pdf

    #### 第5章:Building Dynamic UI Layouts in JavaFX - **目标读者**:需要创建自适应布局的开发者。 - **内容概览**: - 使用各种布局管理器构建动态界面的方法。 - 如何根据窗口大小变化调整布局。 - 实现响应...

    tabu search fundmentals and uses ---fred glover

    - **Manufacturing Systems:** In manufacturing systems, TS has been used to optimize machine layouts, tooling selections, and assembly line configurations to enhance overall system performance. ...

    Mastering Xamarin UI Development pdf

    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 ...

    Mastering.Xamarin.UI.Development.epub

    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 ...

    WPF Programmer's Reference: Windows Presentation Foundation with C# 2010 and .NET 4

    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...

    linux-dwm配置

    DWM,全称Dynamic Window Manager,是一款轻量级的X窗口管理器,专为那些追求简洁、高效操作环境的Linux用户设计。它基于C语言编写,强调小巧、快速,并且允许高度自定义。DWM的灵活性使得用户可以根据自己的需求...

    Graphin:一个基于G6的图可视化的React工具包

    石墨烯 一个基于G6的图分析工具包 English | :sparkles: 特征 :artist_palette: 外观漂亮,样式配置标准化 ... 对于复杂业务场景中的功能,例如layout switching , dynamic layouts , sub-graph layou

    DDR package

    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 ...

    d3.js in action

    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 ...

    odoo themes

    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 ...

Global site tag (gtag.js) - Google Analytics