Is there a framework or something out there so that I can develop webpages in Ruby the same way I can as PHP. Something like
<html><head></head><body>
<?ruby
puts '<p> Hello there!</p>'
?>
</body></html>
The only thing I'm seeing for using Ruby in webpages is huge complex frameworks that is completely different from how PHP works. I mean, sure that's all fine and dandy with the 3 tier model and such but when your just wanting a few simple things done(which are trivial in PHP) in a webpage, to setup such a large framework just doesn't seem right. Especially when you only really want like 1 page made in Ruby and the rest being plain HTML.
The default behavior for PHP is to run as CGI scripts, which means that the web server calls php-cgi <path/to/php-script> or something similar, passing quite a lot of environment variables. To do the same with Ruby, you need to setup a script to handle .rb files. This varies wildly depending on your web server, but if you are using Apache 2.2, put this in your httpd.conf or .htaccess file:
Action ruby-cgi /path/to/ruby-cgi
AddHandler ruby-cgi .rb
# You might want to add this too:
DirectoryIndex index.rb index.html
You could either specify the path to your ruby executable (run which ruby to get the path), or to any other script that accepts a filename as the first parameter. If you use the ruby executable, nothing magical happens, and you can't insert erb into the file without adding some ERB compiling yourself. However, you could use my ruby-cgi script, which does several things:
First, it takes the file and interprets it as ERB, this make the syntax look more like PHP (see below for an example).
Second, it initializes the CGI object into the global variable $CGI. See below for an example on how to use this.
This is a simple example script on how you can use the ruby-cgi "magic":
<% header "Content-Type" => "text/html" %>
<html>
<head>
<title><%= $CGI['title'] %>
</head>
<body>
<h1><%= $CGI['title'] %>
</body>
</html>
Let's say you put this into the webroot with the name example.rb. If you then access this with a URL similar to http://example.com/example.rb?title=Hello%20world it should set the title to "Hello world", and it should display a <h1> with "Hello world" in it.
If you find any bugs with the script, feel free to fork the gist and update it.
Two words: Sinatra and ERB
(For lightweight sites, at least).
Sinatra is a simple HTTP server, ERB is a templating system that acts similar to templating in PHP.
Have you seen Nanoc? It is very simple ruby compiler that outputs static pages through a template engine. May be too simple for your needs but it is handy some times.
You can, with https://github.com/migrs/rack-server-pages
Instruction at that page.
Summarizing:
gem install rack-server-pages
Create config.ru in the root folder of your "application":
require 'rack-server-pages'
run Rack::ServerPages
Create public/index.erb:
<h1>Hello rack!</h1>
<p><%= Time.now %></p>
execute rackup, your Ruby powered pages are ready to be served.
It also works with other application servers, for example, for passenger standalone, you just need to go to that folder and run passenger start.
Related
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?
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".
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
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.
I am working on a project that is in asp. My client wants me to convert ASP to ASP.NET.
The project has got many .inc files which is placed inside an include folder and these inc files are called
<!-- #include file="../includes/example.inc" -->
in many pages. Most of these inc files have either variables or constants declared that are used in many pages or functions(written in VBScript). Now my client had asked me to try converting these inc files to php files(Don't have any clue about php as I have never worked on it before).
My question here is
How effective would it be converting these inc files to php.?
How can I call the functions in the php page from aspx page?
If not converting to php what would be the better method?
Thanks in advance!
This will require the re-writing of all of these flies as PHP has no VBScript support. Therefore, the 'effectiveness' is at best an objective statement being that it is contingent on the competency and skill sets of the individual performing the migration.
You cannot call functions in a php page from an aspx page (inheritly). You can (attempt) to do this, but I would highly recommend not doing this.
My only recommendation would be to start from scratch.
Abort!
Don't go gown this rabbit hole! I agree with everything that #ohgodwhy said, and I'll add some details.
First, a little background. ASP files [usually] have a .ASP file extension and are always written in VbScript. PHP files have a .PHP extension and are always written in PHP. .NET files have a .ASPX extension are usually written in C# or VB.NET. Notice that there are no overlaps between technologies or file extensions. I'll talk about INC files in a second.
When IIS interprets the code, it picks the correct processor based on the file extension and then interprets the code/script, does whatever it's programmed to do, and generates an HTML output for the browser to render. In this case, the first pass of the ASP processor is to gather up all of the include files and process them in the same language as the parent file. I'm going to assume that your INC files are actually ASP files (regardless of their file extension), so they get included and then processed as ASP files. If they contain PHP code, you're going to get an error right off the bat.
If you try to combine technologies in one project, the server has to use multiple interpreters/engines to get this all to process and get to that final HTML. IF (and this is a big IF), but IF you can get them all to work together, you still have the problem of passing data back and forth between the technologies because they're being run in different parts of the server. You can't have the ASP processor include a PHP file and have it magically work.
It is possible to have PHP, ASP, and/or .NET coexist in the same project, but not the way you want it to. I've done it a few times and the project (and server) have to be configured very carefully and then passing the data back and forth becomes the challenge because each of the technologies is working in its own memory space and is not accessible to the other. Again, it CAN be done, but it's going to be a MAJOR challenge and it's going to involve some creative programming - probably far more than what your client wants to do.
Bottom line: go tell the client that converting to PHP is a waste of time and money. You have ASP files and INC (which are really ASP) files. Why involve another technology? Your options are to rewrite the whole thing in ASP, rewrite the whole thing in PHP, or rewrite the whole thing in .NET, but don't mix and match.
(A note for some ubertechnical downvoters: I simplified this a tad to make it clearer for this example!)
Okay this seems like a real noob question.
I currently have a simple html and javascript news reader running on my MAMP server. You can see it on Github. Everything runs client side except for fetching and caching the feeds which is done by a really small php file. I have an ajax call which requests the news feed from the php script by passing some parameters to it.
I have of late been learning Ruby and have started to redo this simple php script in Ruby. The problem I have is that I can't just request the ruby script via ajax with parameters like I would do with php.
So what would be the simplest steps to take to be able to do this?
I feel like a full blown framework, even as small as Sinatra, would be overkill. So any help would be much appreciated. Am I completely thinking about this the wrong way round? Thanks.
Update
I went the Ruby CGI way in the end. Here's what I did.
In the folder I had my script I added a .htaccess file with the following to make apache execute the .rb file.
AddHandler cgi-script .rb
Options +ExecCGI
Then I started my ruby file off like this.
#!/usr/bin/ruby
require 'cgi'
cgi = CGI.new('html4')
cgi.out {
cgi.html {
"Hello World" #started content here
}
}
I then made sure the file was executable. chmod 774.
Thanks for all your help.
The simplest solution here would be Ruby CGI.
If you think sinatra is still big for you, go deeper and try to write pure Rack app.
For example see this minimal app - https://github.com/stevenwilkin/ip.stevenwilkin.com
Rack needs only 3 elements array like this
[200 , {"Content-Type" => "text/plain"}, [env["REMOTE_ADDR"]]]
[return status , headers hash , body string ]
Rails, Sinatra and a lot of another ruby web frameworks are using Rack inside.
And if you need request params nicely formated, you can just use http://rack.rubyforge.org/doc/classes/Rack/Request.html for example.
I am using Sinatra to design a web interface for some research we are doing. However, I also want to be able to use phpMyAdmin for database administration. Is there any way to get Sinatra to serve up php? I know that it can be done with some tweaks to Apache, but since I do not control our setup, I was hoping to be able to do it from within Sinatra.
The server I'll be working on is Windows (don't know what version), has Ruby 1.9.2, PHP 5.3.5, and Apache 2.2, and there are no other web facing or database related projects on it.
My goal is to be be able to access it like this:
researchserveraddress/app/admin/index.php
where
researchserveraddress/app/ would be the main page of our app (served by Sinatra).
I'm sorry if I'm unclear, I do not have very much experience with servers and deploying an app, so far, everything I have done has been locally.
You could use rack-legacy, which allows Sinatra to serve PHP files. It simply uses php-cgi to run the scripts. For example, put phpMyAdmin under directory admin and put something along these lines to config.ru:
require 'app'
map "/admin" do
use Rack::Legacy::Php, 'admin'
use Rack::Static, :urls => ['/'], :root => 'admin'
run lambda{|env| [200, {'Content-type' => 'text/plain'}, 'OK']}
end
map "/" do
run Sinatra::Application
end
(If you're not familiar with using config.ru with your Sinatra app, see this part of Sinatra docs).
I'd suggest to configure Apache instead if possible. It strikes me as a cleaner solution and it would be also more efficient, but that's probably not a problem if you're only using it for phpMyAdmin.
Sinatra can't interpret the PHP files so any embedded variables will be left unprocessed.
You COULD use Sinatra to redirect the requests to the appropriate PHP page, which is then handled in the normal fashion by the PHP processor.
I would like to add a preprocessor to HTML pages. Basically, I have a program that takes the name of an HTML file containing preprocessor instructions and outputs the contents of the file after preprocessing to stdout. This mechanism could change if it makes things easier. All I want to do is hook this into Apache so that all the files that my website serves get put through the preprocessor before going out to the browser. A solution that works with other HTTP servers than Apache would be preferred, but is not required.
If my understanding is correct, this is roughly what PHP does.
If it makes any difference, the preprocessor is written in Python.
If you have Apache and a "preprocessor" written in python, why not go for mod_python?