Layout path when different to view directory

62 views
Skip to first unread message

sal1n

unread,
Apr 14, 2013, 9:08:34 AM4/14/13
to scor...@googlegroups.com
Hi

I'm trying to do something along these lines:

/views
    -/layouts
        --layout.slim
    -/posts
        --index.slim

class PostsController < Scorched::Controller
    
    render_defaults.merge!(dir: 'views/posts', layout: :layout, engine: 'slim')

    get '/' do
        render :index
    end

This doesn't work because it looks for layout.slim in /views/posts/ rather than /views/layouts/ - which is what I'd expect.

However I've tried setting layout: as a string path but I either get a slim error which I believe means the file wasn't found - "Unexpected text after closed tag (__TEMPLATE)" - or the string I use is rendered as the layout e.g "../layouts/layout.slim".

Am I completely misunderstanding how the layout parameter should be used?  I'd be grateful for any suggestions to point me towards a solution.

Thanks

Steve


Tom Wardrop

unread,
Apr 14, 2013, 9:36:13 AM4/14/13
to scor...@googlegroups.com
Hi Steve,

It's best to imagine the layout option as the first argument to #render, as that's essentially how it's implemented. Whatever is set as the layout, ends up being used as the first argument to a sub-sequent render call. So...

render 'Some text', layout: :main

...eventually ends up calling render again as...

render :main

The other thing to remember is that symbol's are treated semantically different to strings. A symbol is assumed to be a file path, while a string is assumed to be a literal template.

Looking at your example, you could do one of the following to make it work. Either set the default directory to your view root, e.g.

render_defaults[:dir] = 'views'
# ...
render
:'posts/index', layout: :'layouts/layout'

Or simply go up a directory when setting the layout...

render :index, layout: :'../layouts/layout'

Scorched doesn't (currently) provide a means to set a different default directory for layouts. The need for such a feature is somewhat negated by the fact that you'd typically set the default layout for each controller, as typically, mosts views rendered in a particular controller will use the same layout...

render_defaults.merge!(dir: 'views/posts', layout: :'../layouts/layout', engine: 'slim')

render
:index

If you think you've got a good use case for adding the option for a default layout directory, then perhaps this is something you'd like to raise as a feature request in the Github Issue Tracker.

I hope that helps.

Cheers,
Tom

sal1n

unread,
Apr 14, 2013, 11:23:55 AM4/14/13
to scor...@googlegroups.com
Hi Tom

Thanks for the response, that all makes sense.  I stupidly didn't realise in Ruby you can also create a symbol using a string literal e.g. :'string' ...it all works now and I've learnt something new:)

Cheers

Steve


Reply all
Reply to author
Forward
0 new messages