How to load external views

36 views
Skip to first unread message

Gen17:5

unread,
Dec 9, 2013, 8:33:29 PM12/9/13
to seren...@googlegroups.com
Hi Im looking to structure my folders, so i can have a views folder and load templates from there..

how do I do that when rendinering a view in the main.js file ?

there are absolutly no tutorials on serenadejs and would like a simple getting started tutorial with VANILLA app functionality like loading a template...

im using ruby sinatra for my server..

Jonas Nicklas

unread,
Dec 10, 2013, 6:26:22 PM12/10/13
to seren...@googlegroups.com
Hi,

If you're using sprockets with Sinatra, the serenade.rb gem allows you to easily integrate Serenade into your sprockets environment. Check the docs.

If you're looking for a more Vanilla approach, there are several ways you can do this:

1) Integrate Serenade into your build process. This is the option I prefer, you add a compilation step which compiles a serenade view file into a js file and then concats it with the rest of your JavaScript. This is the approach taken by serenade.rb, it makes this process really easy. I'm not sure what options you have with other build systems, such as Grunt, Brunch or lineman, you'll have to dig around.

2) Put the template in a <script> tag in your HTML. This is the approach that Ember for example seems to favour. I personally don't really like it. It makes your HTML very bloated. Loading the template is pretty easy then, just do something like:

[].forEach.call(document.querySelectorAll("script[type=text/serenade]"), function(tag) {
  Serenade.view(tag.getAttribute("id"), tag.innerHTML)
})

Or something like that, that's just off the top of my head. This is surely the simplest solution.

3) Inline your views in your javascript. Ugh. Pretty ugly. It's slightly better if you're using CoffeeScript, as it at least supports decent multi-line strings. Still not nice.

4) Load your views via AJAX. Depending on your app this might make sense, but it might not scale.

Hope that gives you some starting points. Let me know if you need more assistance.

/Jonas



--
You received this message because you are subscribed to the Google Groups "Serenade.js" group.
To unsubscribe from this group and stop receiving emails from it, send an email to serenadejs+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Sergey Makridenkov

unread,
Dec 17, 2013, 5:53:45 AM12/17/13
to seren...@googlegroups.com
Hi Jonas,

I am wonder, re option one, which build system are you prefer or you use simple Ruby script?

Jonas Nicklas

unread,
Dec 17, 2013, 6:17:30 PM12/17/13
to Sergey Makridenkov, seren...@googlegroups.com
Every project I’ve used Serenade with has been built on Rails, so that’s what I’ve used.
-- 
Jonas Nicklas

Sergey Makridenkov

unread,
Dec 18, 2013, 1:40:10 AM12/18/13
to seren...@googlegroups.com, Sergey Makridenkov
I want share my pure Ruby script to compile Coffee, Sass and Serenade.
I use it in my Sinatra app.

Script simple, flexible and dirty :-)
Script use gem 'rerun' to auto recompile assets on file changes.

Reply all
Reply to author
Forward
0 new messages