looking for non-existent views/layouts/layouts/layout.erb

36 views
Skip to first unread message

Amitava Shee

unread,
Jul 13, 2011, 11:06:45 AM7/13/11
to middlem...@googlegroups.com
In the past mm was able to pick layout.erb within a sub-folder under $approot/views and apply it to the files in that sub-folder.

My directory structure looks like the below
$approot
|____config.rb
|____config.ru
|____public
|____views
| |____case11
| | |____c_11_1.html.erb
| | |____c_11_2.html.erb
| | |____c_11_3.html.erb
| | |____c_11_4.html.erb
| | |____layout.erb
| | |____assets
| | | |____css
| | | |____images
| | | |____js
| | | |____media

The idea is to create a self-contained sub-folder within the views folder for each of the cases - perhaps some shared assets across the cases.

But when I run mm-build I get the following exception - http://pastie.org/2207594

I traced it to the following code in padrino-core-0.9.29/lib/padrino-core/application/rendering.rb:149

      def fetch_layout_path(given_layout=nil)
        layout_name = given_layout || @layout || :application
        @_cached_layout ||= {}
        cached_layout_path = @_cached_layout[layout_name]
        return cached_layout_path if cached_layout_path
        has_layout_at_root = Dir["#{views}/#{layout_name}.*"].any?
        layout_path = has_layout_at_root ? layout_name.to_sym : File.join('layouts', layout_name.to_s).to_sym
        @_cached_layout[layout_name] = layout_path unless reload_templates?
        layout_path
      end

layout_path computes to $approot/views/layouts/layouts/layout.erb which is of course not where I want the layout. Is there a way to have mm pickup layout.erb from the sub-folder ($approot/views/cases in my case above).

Also, is it possible not to rename *.js to *.js.erb underneath views folder. If I leave the js files alone, mm complains of missing template engine.

Thanks,
Amitava




      

OyoKooN

unread,
Jul 13, 2011, 2:43:43 PM7/13/11
to middlem...@googlegroups.com
I'm used to put my layout files under the /view/layouts/ directory.

For your .js, put them into the public/javascripts directory.
You don't have to do that with the 2.0beta version ;

Thomas Reynolds

unread,
Jul 13, 2011, 2:59:07 PM7/13/11
to middlem...@googlegroups.com
Are you telling Middleman that this path uses a custom layout? Something like:

page "/case11/*", :layout => "case11/layout"

-Thomas

On Wednesday, July 13, 2011 at 11:49 AM, Thomas Reynolds wrote:

Are you telling Middleman that this path uses a custom layout? Something like:

page "/case11/*", :layout => "case11/layout"

-Thomas

Amitava Shee

unread,
Jul 13, 2011, 10:12:50 PM7/13/11
to middlem...@googlegroups.com
Indeed! That did it.

If I were to tweak the code such that it looks for layouts in sub-folder first and falls back to $approot/views/layouts, what would be the best way?

Here's my attempt in config.rb

class << self

 
  def fetch_layout_path(given_layout=nil)
    layout_name = given_layout || @layout || :application
    puts "============ #{layout_name}"

    @_cached_layout ||= {}
    cached_layout_path = @_cached_layout[layout_name]
    return cached_layout_path if cached_layout_path
    has_layout_at_root = Dir["#{views}/#{layout_name}.*"].any?
    layout_path = has_layout_at_root ? layout_name.to_sym : File.join('layouts', layout_name.to_s).to_sym
    @_cached_layout[layout_name] = layout_path unless reload_templates?
    layout_path
  end
end

However, I do not know how to get the current file being rendered (case11/case_11_1.html.erb) so that I extract current folder (case11). Any hint?

Ultimately, my goals is to create a git repo for the entire site and have folks add cases by just dropping a self-contained folder underneath views. A git hook will invoke mm-build and push the resulting site.

Thanks,
Amitava

Thomas Reynolds

unread,
Jul 13, 2011, 10:17:19 PM7/13/11
to middlem...@googlegroups.com
That layout code is deep in Padrino and kind of nasty.

I'd simply look for files named layout:

Dir["**/layout.*"].each do |layout_file|

Figure out the path

full_path = File.expand_path(layout_file)
path_dir = File.dirname(full_path)
relative_path = path_dir.split("source/").last

page "#{relative_path}/*", :layout => "#{relative_path"/layout"

end

Something like that.

-Thomas

Amitava Shee

unread,
Jul 13, 2011, 10:39:47 PM7/13/11
to middlem...@googlegroups.com
Ah! - that makes sense.

Thank you,
Amitava

Amitava Shee

unread,
Jul 14, 2011, 9:53:00 AM7/14/11
to middlem...@googlegroups.com
Is there a way to disable *.js expansion and just have mm copy through? I am wanting to have non-programmers drop folders with web-assets and not worry too much about the back-end process (middleman).

I could use the public folder but it will not be a simple self-contained folder drop.

Thanks again,
Amitava

Thomas Reynolds

unread,
Jul 14, 2011, 12:50:22 PM7/14/11
to middlem...@googlegroups.com
Middleman version 2.0 will have a single combined folder which would facilitate this. If you're brave, you can play with the betas:

gem install middleman --pre

-Thomas
Reply all
Reply to author
Forward
0 new messages