Haml templates sinatra




















If using OSX or Linux, Ruby may be one of a number of open-source packages that come pre-installed and you can skip over to the next paragraph. In case you would like to check if check if you have Ruby installed, open up the terminal prompt and type:.

The output of this will either be the version of Ruby installed or an error complaining that Ruby wasn't found. Should you need to install Ruby manually e.

Next, we will need to install Ruby Gems. Gems are a standard way to package programs or libraries written in Ruby and with Ruby Gems it's possible to install additional dependencies for Ruby applications very easily. Once downloaded, open up a terminal, navigate to the folder where this resides and enter:. There will likely be a version number included in your download and you should make sure to include this when tying the above.

Finally, a symlink symbolic link to tie everything togther should be fun as follows:. With Ruby Gems setup, we can now easily install Sinatra.

For Linux or OSX type this in your terminal:. As with other DSLs and frameworks, Sinatra supports a wide range of different templating engines. ERB is the one most often recommended by the Sinatra camp, however as a part of this post, we're going to explore the use of Haml to define our application templates.

Haml stands for HTML Abstractional Markup Language and is a lightweight markup language abstraction that can be used to describe HTML without the need to use traditional markup language semantics such as opening and closing tags. If you haven't already downloaded and installed MongoDB from an earlier post, please do so now. With Ruby Gems, Mongo can be installed in just one line:.

Navigate to where you've downloaded and extracted Mongo and run the following from terminal:. In the first, cd to your MongoDB bin directory or type in the complete path to it. You'll need to start mongod. Finally, in the second terminal, start the mongo shell which will connect up to localhost by default. Now run the following lines at the terminal:. The public directory contains the scripts and stylesheets for our application and uses HTML5 Boilerplate as a base.

Lastly, the views directory hosts the Haml source files for our application's index and templates, both of which are compiled to standard HTML markup at runtime. In our main application view AppView , we want to load any previously stored Todo items in our Mongo database when the view initializes.

This is done below with the line Todos. When we attempt to access this from our Sinatra-backed API, it should return a list of all the Todo items that have been previously stored in Mongo. We'll take a look at this again when writing the Ruby code powering our backend.

The model for our Todo application remains largely unchanged from the versions previously covered in this book. It is however worth noting that calling the function model.

We want to make sure that for any operations changing underlying data create, update, delete that our Mongo data store correctly reflects these. For app. Next, we create a new connection to Mongo, specifying any custom configuration desired.

Finally we define the routes to be supported by our API. The rest of our routes make use of the params hash and a number of useful helper methods included with the MongoDB Ruby driver. For more details on these, please read the comments I've made inline below:. That's it. The above is extremely lean for an entire API, but does allow us to read and write data to support the functionality required by our client-side application.

For more on what MongoDB and the MongoDB Ruby driver are capable of, please do feel free to read their documentation for more information. If you're a developer wishing to take this example further, why not try to add some additional capabilities to the service:.

Finally, we move on to the Haml files that define our application index layout. When using these I am seeing the css becoming embedded into the post processed html instead of creating a link to the style. The processed html is as follows even the doctype is messed up and title isn't included :. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow.

Learn more. HAML layout. Asked 9 years, 5 months ago. Active 6 months ago. Viewed 1k times. My index. Currently this is a test I did: myapp. Any help would be much appreciated.

I don't know if it matters but I'm using shotgun for development. Improve this question. Find centralized, trusted content and collaborate around the technologies you use most. Connect and share knowledge within a single location that is structured and easy to search. I have a simple sinatra app that uses haml and sass for the views. One of the views located in the views folder is a partial for my navigation menu. I am trying to render it from index. Read Jason's answer below!

What are you trying works in rails! Sinatra has no partial method. An implementation of partial on Sinatra looks like this source gist from github:. Including this method, you may call partial in your. Notice the syntax differences between rails and sinatra regarding render method!

Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. How to render a partial in sinatra view haml in haml? Ask Question. Asked 11 years, 2 months ago. Active 6 years, 3 months ago.



0コメント

  • 1000 / 1000