librelist archives

« back to archive

Issues using ERB with Nesta

Issues using ERB with Nesta

From:
Rob Cooper
Date:
2011-12-04 @ 11:03
Hi guys,

First off, thank you for what looks like a great project - really looking
forward to getting this running.

I am currently in the process of creating a blog using Nesta, and want the
templates to be rendered using ERB/SCSS (since working with someone who is
more comfortable with those over HAML/SASS).

I have completed the following:

   - Created a theme directory.
   - Added the following to the 'App.rb' in the theme folder:

    get '/css/:sheet.css' do
      content_type 'text/css', :charset => 'utf-8'
      *cache scss(params[:sheet].to_sym)*
    end

    get '*' do
      set_common_variables
      @heading = @title
      parts = params[:splat].map { |p| p.sub(/\/$/, '') }
      @page = Nesta::Page.find_by_path(File.join(parts))
      raise Sinatra::NotFound if @page.nil?
      @title = @page.title
      set_from_page(:description, :keywords)
      *cache erb(@page.template, :layout => @page.layout)*
    end

   - I then converted all the standard HAML view files (categories, error,
   page, sidebar etc.) to ERB.

I then fired up the app and pretty much everything is rendering correctly
(SCSS is working, and the headers are fine) but I am getting:

Page not found
> Please try another page or, if you think something is wrong with our site,
> do get in touch and let us know.


Which I thought was coming from the 'not_found.erb' file in the theme
folder, but that doesn't seem to be the case!

I totally think this is down to user error, but I had a look at the code
and came across this - which I found confusing:

https://github.com/gma/nesta/blob/master/lib/nesta/models.rb#L38

I don't see ERB anywhere in the FORMATS array:
https://github.com/gma/nesta/blob/master/lib/nesta/models.rb#L11

So my next step was to try and rename the 'content/pages/index.erb' file I
had to '.haml' and see if that worked - and I got a different response:

No such file or directory -
> /Users/robcthegeek/.rvm/gems/ruby-1.9.2-p180/gems/nesta-0.9.11/views/page.erb


Now, my understanding from 'http://nestacms.com/docs/design' is that
precedence goes from 'app/views' > 'theme/views' > 'gem/views' - would this
not be the case? In my current project I have no 'app/views/page.erb' but I
do have one in 'theme/views'.

Any ideas?

Sorry for the long message - wanted to be as concise as possible!

MTIA! - Rob

Re: [nesta] Issues using ERB with Nesta

From:
Graham Ashton
Date:
2011-12-06 @ 13:20
On 4 Dec 2011, at 11:03, Rob Cooper wrote:

> I am currently in the process of creating a blog using Nesta, and want 
the templates to be rendered using ERB/SCSS (since working with someone 
who is more comfortable with those over HAML/SASS).
> 
> I have completed the following:
> 
>     get '/css/:sheet.css' do
>       content_type 'text/css', :charset => 'utf-8'
>       cache scss(params[:sheet].to_sym)
>     end

Relatively recently, a stylesheet method was implemented, so you can now 
say this and it'll automatically use .sass or .scss files:

    cache stylesheet(params[:sheet].to_sym)

>     get '*' do
>       set_common_variables
>       @heading = @title
>       parts = params[:splat].map { |p| p.sub(/\/$/, '') }
>       @page = Nesta::Page.find_by_path(File.join(parts))
>       raise Sinatra::NotFound if @page.nil?
>       @title = @page.title
>       set_from_page(:description, :keywords)
>       cache erb(@page.template, :layout => @page.layout)
>     end

That's a good start. I ought to create a similar method to auto load .haml
and .erb files, but I haven't done that yet. In fact, without a commit I 
just added .erb files won't get found inside ./views or theme/*/views.

Here's the commit (it's untested but I'm confident it'll get you going).

https://github.com/gma/nesta/commit/92d859adb8980102f63f78c88942c1af9a281a84

To use it you can follow the instructions on this excellent recipe 
contributed by Jacopo Notarstefano that I haven't quite got around to 
merging and deploying yet. It tells you how to install Nesta from the head
of the master branch on GitHub.


https://github.com/Jacquerie/nestacms.com/blob/df8c0edc9c5558178262d1c761d5b9801121a573/content/pages/docs/recipes/using-unreleased-version-of-nesta.mdown

> I totally think this is down to user error, but I had a look at the code
and came across this - which I found confusing:
> 
> https://github.com/gma/nesta/blob/master/lib/nesta/models.rb#L38
> 
> I don't see ERB anywhere in the FORMATS array: 
https://github.com/gma/nesta/blob/master/lib/nesta/models.rb#L11

No, it's not in there. ERB isn't a first class citizen in Nesta; I hadn't 
previously added support for loading .erb templates. Now that I have 
(that's the commit above), what we're getting into here is the prospect of
writing your pages in HTML.

I'll need to think about whether support for that ought to be in the core 
code base; it feels a bit weird for some reason, but maybe that's just me?

> So my next step was to try and rename the 'content/pages/index.erb' file
I had to '.haml' and see if that worked - and I got a different response:
> 
> No such file or directory - 
/Users/robcthegeek/.rvm/gems/ruby-1.9.2-p180/gems/nesta-0.9.11/views/page.erb

That makes sense; the commit above should fix that.

> Now, my understanding from 'http://nestacms.com/docs/design' is that 
precedence goes from 'app/views' > 'theme/views' > 'gem/views' - would 
this not be the case? In my current project I have no 'app/views/page.erb'
but I do have one in 'theme/views'.

I'm rather pleased that you managed to work out as much as you have; I 
think there are plenty of "how to customise Nesta" docs yet to be written,
yet you've discovered pretty much how it all works as it is.

Sorry for the delay in my reply (and slight tardiness in general of late);
I'm doing a lot of freelancing and preparing to move house, so spare time 
is a scarcity!

Cheers,
Graham

Re: [nesta] Issues using ERB with Nesta

From:
Rob Cooper
Date:
2011-12-07 @ 21:31
>
> Relatively recently, a stylesheet method was implemented, so you can now
> say this and it'll automatically use .sass or .scss files:
>
>    cache stylesheet(params[:sheet].to_sym)


Nice! Will get the code changed to utilise that!


That's a good start. I ought to create a similar method to auto load .haml
> and .erb files, but I haven't done that yet. In fact, without a commit I
> just added .erb files won't get found inside ./views or theme/*/views.
> Here's the commit (it's untested but I'm confident it'll get you going).
>
> https://github.com/gma/nesta/commit/92d859adb8980102f63f78c88942c1af9a281a84


I will check out this commit and the post by Jacopo and let you know -
thanks.

On a personal note, I am confused why I am even having errors - since the
docs for "using a different templating engine" demo changing to ERB - *have
I missed anything in addition to the code I posted?* (Just trying to figure
out if it's complete 'PLBCAK' for any learning points or if I am
misunderstanding where Nesta *could* deal with ERB).


I'll need to think about whether support for that ought to be in the core
> code base; it feels a bit weird for some reason, but maybe that's just me?


If it helps - I *really* think you should consider it if you want Nesta to
be used by anything more than the geek community. I am currently in a 2-man
startup, my co-founder being non-techie. We both need/want a simple CMS,
but asking her to get in to HAML syntax (as well as all the other techie
bits she is having to pick up) would be unfair. On top of that, most CMS'
are maintained by non-techie people (last 3 gigs all had CMS' maintained by
people who could barely turn a computer on). Markdown I think they can get,
and ERB (for templates) is close enough to HTML that they can normally
figure out enough to make things work/make changes (i.e. they get a list of
things they can "do" from the dev, and then they are away).

Just my two bits - hope it's food for thought.

If there is any way I can help with this then do let me know. Still pretty
fresh on the Ruby scene and totally wanting to shake my OSS virginity ;)


> Sorry for the delay in my reply (and slight tardiness in general of late);
> I'm doing a lot of freelancing and preparing to move house, so spare time
> is a scarcity!


Naturally, no problem - we all have lives outside of code! I hope the move
and project(s) are going well!

Graham, Jacopo - thank you so much. Will be in touch soon.

Re: [nesta] Issues using ERB with Nesta

From:
Graham Ashton
Date:
2011-12-13 @ 18:15
On 7 Dec 2011, at 21:31, Rob Cooper wrote:

> On a personal note, I am confused why I am even having errors - since 
the docs for "using a different templating engine" demo changing to ERB - 
have I missed anything in addition to the code I posted?

It's possible that I wrote those docs [1] when your local ./views 
directory was the default location that Nesta would search for template 
files (this is back in the old days before Nesta was a gem). I just 
checked the code and unless Nesta does some magic to the Sinatra rendering
methods (e.g. haml, sass, scss, erb, etc.) then Nesta will only be able to
find a template if it is in the gem. That makes the erb part of that page 
wrong.

[1] http://nestacms.com/docs/design/templating-engines

I need to fix them, and then expand on them quite significantly, with a 
bit of a walk through of how to do this stuff. Maybe with a screencast.

.haml, .sass, .scss and (on the master branch on GitHub) .erb files are 
the only templates that have this chain of potential view folders enabled.
(currently)

>> I'll need to think about whether support for that ought to be in the 
core code base; it feels a bit weird for some reason, but maybe that's 
just me?
> 
> If it helps - I *really* think you should consider it if you want Nesta 
to be used by anything more than the geek community.

To be specific I meant "support for putting .html files inside content/pages".

It feels to me as though a CMS really ought to get you to use something 
more friendly. Haml is different to HTML in this context as you can use it
as a glorified front end to Markdown or Textile.

The bottom of this page is an example:


https://github.com/gma/nesta-demo-content/blob/master/pages/examples/using-haml.haml

> I am currently in a 2-man startup, my co-founder being non-techie. We 
both need/want a simple CMS, but asking her to get in to HAML syntax (as 
well as all the other techie bits she is having to pick up) would be 
unfair.

I agree. I was showing a less technical friend how to make some sites in 
it the other week and I had to do the erb patch straight off the bat. I'm 
sold on supporting HTML in the views folder, I just need to put a new 
release out (and probably treat a slew of other file types in a similar 
way).

> If there is any way I can help with this then do let me know. Still 
pretty  fresh on the Ruby scene and totally wanting to shake my OSS 
virginity ;)

Cool, thanks. :-)

I'll take a look at the way things are when I've finished moving and see 
what's on the list.

> I hope the move and project(s) are going well!

Cheers. Currently surrounded by boxes!

Re: [nesta] Issues using ERB with Nesta

From:
Jacopo Notarstefano
Date:
2011-12-06 @ 23:34
Thank you Graham!

@Rob: If you find it confusing or incomplete I'd love to hear back your
comments, so that I can improve it.



> To use it you can follow the instructions on this excellent recipe
> contributed by Jacopo Notarstefano that I haven't quite got around to
> merging and deploying yet. It tells you how to install Nesta from the head
> of the master branch on GitHub.
>
>
> 
https://github.com/Jacquerie/nestacms.com/blob/df8c0edc9c5558178262d1c761d5b9801121a573/content/pages/docs/recipes/using-unreleased-version-of-nesta.mdown
>
>
Cheers,
> Graham
>