Is it possible to serve static HTML without the file extension? - ruby

I have a ruby + sinatra website where most of the files are static HTML, meaning they can be accessed by going to http://www.mydomain.com/features.html, http://www.mydomain.com/pricing.html, etc... (I am using sinatra for a few more dynamic pages)
What I would like to do is serve these static HTML files without their file extensions. So going to http://www.mydomain.com/features would show the same as if I currently visited http://www.mydomain.com/features.html currently.
I have tried simply removing my .html file extensions, however when trying to open these files in the browser, the browser downloads downloads the file instead of rendering it on screen.
Is it possible to continue serving my static .html files just without their file extensions? Thanks!

If you do this:
require 'sinatra'
get '/:file_name' do |fname|
send_file(
File.join(settings.public_folder, "#{fname}.html")
)
end
...then either url:
http://www.mydomain.com/features.html
http://www.mydomain.com/features
will return the same page.
settings.public_folder: By default, this is assumed to be a directory
named “public” within the root directory...http://www.sinatrarb.com/configuration.html
If you define the routes for your dynamic pages above that route, then they will match first.

Related

Why are view files in CodeIgniter saved with .php extension?

From the CodeIgniter docs, I noticed that the view files are saved with .php extension, even if they only contain html. Is there a logical reason behind this?
It is not compulsory to save view files with the only .php extension we can also save them as HTML files, But to load the HTML views we also need to specify the filename with an extension like $this->load->view('my_view.html').
Also, you can check this link for the same discussion view extension in CodeIgniter 3
Q: When we load any class in CodeIgniter like we load the models, libraries in our controllers without specifying its extensions so how CodeIgniter loads the exact file with that name.
A: For this, There are the system functions defined in the CodeIgniter which concat the .php extension with the class name we pass e.g $this->load->library('form_validation')
For Example: How CodeIgniter 3 actually loads liberaries If you can check the system file in system/core/Common.php line no ~141 function
load_class for liberaries
So it will load system/libraries/Form_validation.php file
I hope this will make you clear that why view files in CodeIgniter saved with .php extension.
Codeigniter is a PhP framework, so it assumes the user is (eventually) going to use PhP.
Also, it isn't necessarily bad practice to always use .php instead of .html since .php handles html just fine. Could be viewed as potential future proofing your code.
One advantage of using the .php extension is that a server can be configured to not display the contents of the file. In other words, the lines of text (php code) that are eventually output to the browser (as html) cannot be seen directly. This provides a layer of security by hiding implementation details from the website user.
Because Codeigniter is a PHP MVC (Model-View-Controller) Framework. The view files are .php because they use PHP scripts. That's the idea, but it's not mandatory. You can include pure HTML. The view files are called by "name-of-the-view-without-the-extension".

CodeIgniter: How to load css file that is WITHIN application folder

It appears that this question is asked often and answered the same way: store the css files outside of the application directory and then use base_url() . "path/to/file".
However, I want to keep my css files and js files inside my application/views/ directory, because the views directory is effectively the html space, and css and js belong to that space (in my opinion).
Below is the structure that I wish for:
root
- application
-- views
--- assets
---- css
---- js
- system
When I attempt to load css files from within this directory structure, I get a NetworkError: 403 Forbidden, which makes sense because of CI's framework protocol.
But I am guessing that there is a way.
Publicly reachable files like CSSes images and JS files need to be in public directory next to index.php file. So hierarchy would be:
root
- application
- system
- assets
-- css
-- js
You can aproach to those files with hard coded
Link
or using (loaded) url helper with it's function base_url() or site_url(). Don't forget to fill correct URL into application config file.
Link
Docs.
Hey i'm going to politely push back on this idea :-)
Your application and system folders should be ABOVE the root, so they are not publicly accessible. (Unless this is a really simple application and you are not doing any database interaction, etc). They should not be considered part of the HTML or public space because you do not want the public accessing them. Set the path for them once in the main index.php file and its done.
Also i suggest renaming your system and application folders, like "system302". Over the long term it makes versioning and upgrading (and reverting if needed) much easier.

Middleman - How to change the paths for generated html files?

I'm building a website using Middleman. There are source files placed like the following.
source/index.html.md
source/errors/404.html
source/docs/foo.html.md
source/docs/bar.html.md
By default, when I build static html files, the generated files are placed following to the original directory structure (with directory indexes).
build/index.html
build/errors/404/index.html
build/docs/foo/index.html
build/docs/bar/index.html
However, I want all html files to be served directly under /, so that they can be accessed like http://example.com/foo/ and http://example.com/404/.
How do you change the paths for generated html files?
You can use proxies to create duplicate pages, and then ignore the original pages.
https://middlemanapp.com/basics/dynamic-pages/
But the simplest way is, of course, to adjust the source structure to match the desired structure.
I think this part of Middleman doc could help you .
All you need is to activate directory indexes and configure it properly, and it should work.

Clean URLs from pages in a custom Jekyll folder

I'm building a Jekyll site made of static pages (e.g. nothing in the _posts directory), and rather than keeping each page in the root directory, I'd like to keep it in a custom pages directory (for organization's sake).
However, I don't want this structure reflected in my URLs, so in my _config.yml I have:
permalink: /:title
With this configuration, my desired URL would be site.com/pagename, but I don't get a rendered page unless I hit site.com/pages/pagename.html.
Could anyone help me configure my build to achieve the former? Thanks!
Have you tried adding YAML Front Matter to these static pages? For example:
---
title: "pagetitle"
---
This way, Jekyll should recognize it as a Page (instead of a StaticPage) and would use your permalink configuration.
These are static pages so the entry in _config.yml is going to be ignored.
From the sounds of it you will need to create a URL rewrite rule. How to do this will depend on the web server that is servering up the pages.

Simple "static" Ruby sites?

For early development, I typically build out a static version of the site. Previously I'd use PHP and have something like...
images
javascripts
stylesheets
templates
-- header.php
-- footer.php
index.php
users.php
And the index.php and users.php would have some basic PHP include code for those header and footer files.
I also got the added benefit of being able to use a few PHP functions.
But I haven't used PHP for anything in ages and use Ruby almost exclusively...so I'm wondering, is there a way to pull off something really basic like this in Ruby?
Primarily looking for something that allows me to:
Do basic file includes (so I can create simple templates)
Run Ruby inside the files
Ideally I could also use LiveReload with it.
Additional details: I'm running this locally on OS X and I typically use Pow as a server.
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.
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!
Perhaps Jekyll will fit the bill?
Its description:
Jekyll is a simple, blog aware, static site generator. It takes a
template directory (representing the raw form of a website), runs it
through Textile or Markdown and Liquid converters, and spits out a
complete, static website suitable for serving with Apache or your
favorite web server. This is also the engine behind GitHub Pages,
which you can use to host your project’s page or blog right here from
GitHub
Yes, i use Sinatra for this, see https://github.com/sinatra/sinatra/ for some samples.
Running ruby inside the files isn't very static but in Sinatra you can do both.
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.

Resources