0

对于早期开发,我通常构建站点的静态版本。以前我会使用 PHP 并有类似...

images
javascripts
stylesheets
templates
-- header.php
-- footer.php
index.php
users.php

并且将index.phpusers.php一些基本的 PHP 包含这些页眉和页脚文件的代码。

我还获得了能够使用一些 PHP 函数的额外好处。

但是我已经很久没有使用 PHP 了,几乎只使用 Ruby ......所以我想知道,有没有办法在 Ruby 中实现类似这样的基本功能?

主要是寻找可以让我:

  1. 做基本文件包括(所以我可以创建简单的模板)
  2. 在文件中运行 Ruby

理想情况下,我也可以使用LiveReload

附加细节:我在 OS X 上本地运行它,我通常使用Pow作为服务器。

4

5 回答 5

1

Peter is right to recommend Sinatra. There are typically two types of Sinatra applications. Modular and classical. For your example, I'll create a classical application. It isn't much work to convert it to a modular if you find that style would better fit your needs.

You'll want to install the gem with gem install sinatra. Create a new directory for your project and two new files as follows:

# app.rb
require 'sinatra'

get '/' do
  erb :index
end

# config.ru
require './app'
run Sinatra::Application

Create another directory called views and add this file:

# index.erb
Hello World!

Then type ruby app.rb and viola, you now have a working project on localhost:4567/. To serve static files like css and js, just create a public directory. From there, any file will be able to accessed after the root url. So if you created a css folder, its respective url would be: yourdomain.com/css/styles.css.

So the entire directory would be as folllows:

app/
  app.rb
  config.ru
  public/
    css/
    js/
    images/
  views/
    index.erb

Between the Sinatra Book and the read me, you should be able to find all the info you need.

To accomplish the templates, you'll need something called Sinatra Partial.

I'm not too familiar LiveReload but it seems like Compass accomplishes the same thing and has great integration with Sinatra. So long as pow is a rack based, you should have no problem using it.

Here is a Sinatra Bootstrap I use for all my projects. It has Compass and Sinatra Partial preconfigured and makes it really easy to deploy with Heroku. It also uses Slim, Coffeescript, Thin (as the server), Twitter Bootstrap and Sass but it shouldn't be too much work to sub that with your respective favorites or remove them all together.

于 2012-10-21T22:13:22.713 回答
1

I gifted my girlfriend a website with her domain name, I chose Jekyll for the job which uses RubyGems for the purpose.

It was easy and fun. Moreover you have lots of themes available which you can configure with .YML files just changing there will work for hole site. Although Linux or Mac OS is suggested by official Jekyll website but i did it in windows which was not much of a problem. They have steps defined for working on windows on their website.

http://jekyllrb.com/ 

The best part was that i could host the website via the git hub pages only. I didn't have to buy anything. Git hub allows you to host one repo. (funFact : Jekyll was developed by github inventor)

That's the one i used and made the website in 20 hours with no prior knowledge of Ruby and Jekyll. So i will suggest you check it out!

于 2014-11-24T11:51:16.433 回答
0

也许Jekyll符合要求?

其描述:

Jekyll 是一个简单的、支持博客的、静态站点生成器。它需要一个模板目录(代表网站的原始形式),通过 Textile 或 Markdown 和 Liquid 转换器运行它,并输出一个适合与 Apache 或您喜欢的 Web 服务器一起服务的完整的静态网站。这也是 GitHub Pages 背后的引擎,您可以使用它在 GitHub 上托管您的项目页面或博客

于 2012-10-18T12:45:06.150 回答
0

是的,我为此使用 Sinatra,有关一些示例,请参阅https://github.com/sinatra/sinatra/。在文件中运行 ruby​​ 并不是非常静态的,但在 Sinatra 中你可以两者都做。

于 2012-10-18T16:40:17.927 回答
0

Jekyll won't leave you run Ruby code "inside" the files. Jekyll is a parsing engine bundled as a ruby gem. You basically code templates using HTML and Liquid and write content using markdown that gets embebed generating plain HTML files that you can upload to any web server.

于 2012-10-21T21:45:11.010 回答