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?

5
  • 2
    there is also this similar question 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.. Commented Nov 13, 2014 at 2:43
  • There are tons of templating frameworks for Ruby: ERB, HAML, Slim, and more. Commented Nov 13, 2014 at 2:47
  • I know and I've used them but I am asking if it is possible to use them as it happens with <?php ?> , without the need to run a Ruby framework behind them, big or tiny that it is. Commented Nov 13, 2014 at 2:49
  • Yes, they're independent of frameworks. Look at the documentation for ERB. Or HAML documentation Commented Nov 13, 2014 at 2:52
  • I also meant "as immediate as PHP tags" ... Commented Nov 13, 2014 at 3:14

3 Answers 3

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".

Sign up to request clarification or add additional context in comments.

Comments

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

7 Comments

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?
No, I want it to run without adding in another server. It just has to execute on web request, as with PHP in Apache.
you can always use cgi... check stackoverflow.com/questions/2245634/…
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 ?
@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.
|
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.

6 Comments

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.
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: iamjamesblog.wordpress.com/2011/11/14/…
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.
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.
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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.