5

What would be the closest thing to having Ruby in HTML like for PHP with <?php ?> tags?

Can it be done without the need of frameworks that impose website structure or without the need to run Ruby servers, ecc... ?

Is there a way?

Redoman
  • 3,059
  • 3
  • 34
  • 62
  • 2
    there is also this similar question https://stackoverflow.com/questions/3486664/embedding-ruby-code-in-html but it is open to accept answers which also suggest using frameworks. Also has not received new answers in 4 years.. – Redoman Nov 13 '14 at 02:43
  • There are tons of templating frameworks for Ruby: ERB, HAML, Slim, and more. – August Nov 13 '14 at 02:47
  • I know and I've used them but I am asking if it is possible to use them as it happens with , without the need to run a Ruby framework behind them, big or tiny that it is. – Redoman Nov 13 '14 at 02:49
  • Yes, they're independent of frameworks. Look at the [documentation for `ERB`](http://ruby-doc.org/stdlib-2.1.4/libdoc/erb/rdoc/ERB.html). Or [HAML documentation](http://haml.info/docs/yardoc/file.REFERENCE.html) – August Nov 13 '14 at 02:52
  • I also meant "as immediate as PHP tags" ... – Redoman Nov 13 '14 at 03:14

3 Answers3

2

rack-server-pages

Rack middleware and application for serving dynamic pages in very simple way. There are no controllers or models, just only views like a jsp, asp and php!

https://github.com/migrs/rack-server-pages

https://stackoverflow.com/a/32938202/988591

You can run it with "rackup" or any app server supporting rack. For example with passenger you just need to go to the folder where config.ru is and run "passenger start".

Community
  • 1
  • 1
Redoman
  • 3,059
  • 3
  • 34
  • 62
1

You're looking for erb files.

Open a test.erb file and write this down:

<h1><%= "hello" %></h1>

then run it with:

ruby -rerb -e 'puts ERB.new(File.read("test.erb")).result'

To run erb within a webser you need to wrap it somehow.

Here is a gem that does the job:

Serve - A Rapid Prototyping Framework for Web Applications

gem install serve

And then run it on the directory where your scripts are:

serve

The standard address is localhost:4000

Guilherme Viebig
  • 6,901
  • 3
  • 28
  • 30
  • 1
    great, but how do I make this run on each http request as it happens with PHP ? Is there an established way to do so or a currently mantained gem implementing that in the Ruby world? – Redoman Nov 13 '14 at 03:01
  • No, I want it to run without adding in another server. It just has to execute on web request, as with PHP in Apache. – Redoman Nov 13 '14 at 03:16
  • you can always use cgi... check http://stackoverflow.com/questions/2245634/setting-up-ruby-cgi-in-apache – Guilherme Viebig Nov 13 '14 at 03:22
  • 1
    Yes but why it does look like there are only 2 people using it in the real world and why I can't find tutorials, ecc.. on that ? – Redoman Nov 13 '14 at 03:26
  • @jj_ not sure why you care about "adding in another server" while your answer of "rack-server-pages" is doing pretty much the same thing. – lulalala Feb 19 '16 at 07:22
  • @lulalala no, it's not? When I said "adding in another server" I meant *web server*, but.. I am not doing it? I am using passenger to serve everything, as I was doing before for static content only, so I basically had want I wanted, that is "enable ruby support in HTML" without changing the techlonogy to replace it with a ruby-specific web server. – Redoman Feb 20 '16 at 18:23
  • They are both Rack application so can both be used with Passenger. You need to ensure your project root directory have this file https://github.com/jlong/serve/blob/master/lib/serve/bootstrap/config.ru and I assume it would work correctly with Passenger, without additional server. – lulalala Feb 21 '16 at 01:49
1

You can cram as much logic in a template as you wish, but you still need an application server. PHP has this as mod_php or through FastCGI, etc. Ruby offers many options. Consider serve or create a bare bones sinatra app.

Consider what ends you are trying to achieve. This is generally poor practice and many people moved from the old style PHP to more modern frameworks which avoid this pitfall. This may be why only 2 people use it and you can't find any tutorials.

Matt Walston
  • 121
  • 5
  • 1
    Consider I have an already running server. I don't want to modify it. It's doing its job well at serving PHP pages. I just want to add to this scenario the possibility to use Ruby inside of HTML as it happens for PHP. – Redoman Nov 13 '14 at 03:35
  • Well I want the Eastern Bunny to bring me a six pack during the Gators game but it ain't going to happen. If your web server does not have the capability you will need to add it. Consider following tutorial for how to use erb files in an old school php manner: http://iamjamesblog.wordpress.com/2011/11/14/configure-apache-to-serve-any-ruby-erb-file-like-a-mod_ruby-or-mod_erb/ – Matt Walston Nov 13 '14 at 03:49
  • So is your webserver. I don't think you will find many new solutions as the people looking to do this are missing the butthurt of old school php and asp. – Matt Walston Nov 13 '14 at 03:59
  • I am looking for an established way to do this. If you can't answer, just don't. Keep your personal opinions for yourself, sorry but I did not ask for those. – Redoman Nov 13 '14 at 04:05
  • 1
    If you want your webserver to run Ruby files, then you need to either use a webserver that can run Ruby files or you need to add the capability to run Ruby files to your webserver. Exactly the same as with PHP. You have been shown multiple webservers that can run Ruby files and you have been shown multiple ways of adding Ruby capabilities to your existing webserver. If you insist on not adding the capability of running Ruby to your webserver, then your webserver cannot run Ruby. Again, this is no different from PHP. – Jörg W Mittag Nov 13 '14 at 09:43
  • I am not saying I don't want to add that capability to the webserver, never said that! I would just like to see a suffieciently large adopted and updated solution to do so, and I am finding it hard to find that, to a point where one suspects that there might be something inherently bad with this approach.. as opposed to the widely adopted ruby framework/server approach. Yet I would find it so easy and immediate if I could just write ruby on pages that require small dynamic content without the overhead of Ruby frameworks/servers. See what I am saying? I'd be glad if you could elaborate on this. – Redoman Nov 14 '14 at 22:48