Render HTML file with CSS and local variables - ruby-on-rails

I have the following content in an HTML file placed under public/company/ with a CSS file css/style.css:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>
<link href="css/style.css" rel="stylesheet" type="text/css" media="all"/>
</head>
<body>
<div class="title">Name {name}</div>
</body>
</html>
I want to render this HTML file with the CSS stylesheet from an action and replace {name} without changing the physical file content and place. I can render the HTML file but the CSS file would not be found. Can anyone help me render the HTML file with the CSS file and replace the {name}?

css/style.css when called from company/file.html will try to load company/css/style.css.

Click here to see an article the way you can do that. Hope it will help!
But, I'm not sure why you are looking to render that way where Rails has all the features in place.

If your CSS file is located at /public/company/css/style.css, then your static HTML file should link it with an absolute path. Use a leading / in the resource's path indicate an absolute path.:
<link href="/company/css/style.css" rel="stylesheet" type="text/css" media="all"/>
Note that the Rails router will likely catch this request and raise an exception. Depending on the web server, you may need to enable static assets or serve directly, bypassing Rails altogether.
Changing the content of a static HTML file server-side without actually modifying it (ie. adding ERb) isn't straightforward. You could:
inject some javascript into the response to perform modifications client-side. (ugly)
load the static HTML file using Nokogiri, et. al., modify the content, and send the output to the client. (expensive)
Sounds to me like these vanilla HTML/CSS files are third party (designer? client?) and you would like to drop it in /public and have it work automatically with Rails. That would be nice, but it's not that easy. You'd be better off using Rails' built-in templating system, but that means modifying files and moving them to the expected locations.

Related

Automatically embed HTML files at page load

I'm looking to embed html files at page load (of home page, index). All html files are in the same directory as index. The html files are random file names, and are always being added. They need to be automatically loaded at page load, for that reason.
I'm not great with HTML, but it seems you can't do it with just HTML. I've been searching for some kind of method, but haven't found anything. What do I use? Javascript? PHP?
Using HTML:
Include an import on your page by declaring a <link rel="import">
<head>
.....
<link rel="import" href="/path/to/imports/stuff.html">
</head>
Using PHP
<?php include("/path/to/imports/stuff.html"); ?>
Or
<?php require_once("/path/to/imports/stuff.html"); ?>

Path to folder outside of folder

I have a folder for all my css in my main folder called "main." In "main" I have another folder called "math." I would like to use my css in the "math" folder, but when I type:
<link rel="stylesheet" href="css/basic.css" type="text/css" media="screen" />
on the index.html of the "math" folder it doenst work. I think it's because it's looking for the css folder inside of the math folder. How can i link to the css folder, which is not in the math folder?
If I am understanding, your directory structure is as follows:
siteroot
main
math
css
You are looking to link to a style sheet in /main/css from /main/math/index.html.
There are two solutions, the easiest is to use an absolute path:
<link rel="stylesheet" href="/main/css/basic.css" type="text/css" media="screen" />
Absolute paths are a better solution and will cause less headache down the road. I do not know of any drawbacks, either.
Alternatively, you could use '..' to traverse up a directory
<link rel="stylesheet" href="../css/basic.css" type="text/css" media="screen" />
For example your directory is like this:
Desktop >
ProjectFolder >
index.html
css >
style.css
images >
img.png
You are at your style.css and you want to use img.png as a background-image, use this:
url("../images/img.png")
Works for me!
Use absolute path:
href="/main/css/..."
You must go up to the same directory as css and math using ../
So it would look like ../css/basic.css
If you Want To locate a file inside of that folder use this
<link rel="stylesheet" href="./css/basic.css" type="text/css" media="screen" />
and if you want to locate a file outside of that folder you could use this snippet
<link rel="stylesheet" href="../css/basic.css" type="text/css" media="screen" />
The answer also has some dependency on whether or not the site is being developed and tested locally or if your publishing your content to a web server and testing links on the Live Site.
If your href attribute's URL is using a Relative URL and has a domain name being mapped to your Document Root on a Live Site, the above answers are correct.
However, if your testing your links where all the files are on a local PC's file system where no web server is being used locally, then you might find the following information helpful too.
If you are testing locally, there is usually no domain associated with your Document Root and the pathing changes in a number of ways.
If you link to a file in any child directories, you will NOT have an initial slash ("/") in your href or in this case a src attribute because there is no domain and the Relative URL is relative to the current page. (The same rules apply to your href URLs as well)
Local testing URLS:
<img src="images/image-1.jpg" alt="my-Image-1">
..as opposed to a Relative URL on a page in a Live Site that has a domain name where you WOULD use an initial forward slash because it will be relative to the domain.
Remote Live Testing:
<img src="/images/image-1.jpg" alt="my-Image-1">
I am aware this is not the situation you ask about but it should help with the following two examples where the folders are adjacent and parent directories on a local file system. Your situation is that your Relative URL is to a file located in an adjacent directory. See below if your developing and testing locally.
For Relative URLs linking to your css stylesheet files in an adjacent directory or even links to files a parent directory above your current page on a local filesystem (usually without a domain), your href attribute URL pathing should use a double-dot-slash (../). So your situation (if testing locally without a domain) is:
<link rel="stylesheet" href="../css/basic.css" type="text/css" media="screen" />
...as indicated by #Cory Dolphin above.
I experienced this and found that my issue was that I kept finding different results depending on where I was testing and it was driving me mad.
I originally thought the pathing difference had to do with my local files being on my local Windows system and the reason it was slightly different was becuase of the file system of my remote Linux Apache VPS.
I found that it has more to do with whether it has a domain mapping to your site files or not. This is addressed somewhat in W3Schools.com's Page in the section that's titled The src Attribute and Relative URLs.

How would SquishIt render a file using to a local directory but write the file reference to a different subdomain

Is there a way using SquishIt to render a combined file to a local directory, keeping the filename SquishIt generated, but changing the domain?
So, something like this:
Bundle.Css().Add("~/file1.css").Add("~/file2.css").Render("~/content/combined-css_#.css")
Normally, SquishIt would render that to a link, replacing the # with a key signature, as something like:
<link rel="stylesheet" type="text/css" href="/content/combined-css_697C70D68EA1DCBE1903A58032BDB305.css" />
However, I serve my css and js file from a static cookieless domain. I want the file to be written to the same local directory and still use SquishIt's generated key signature, but I want to output the stylesheet link to:
<link rel="stylesheet" type="text/css" href="http://static.mydomain.com/content/combined-css_697C70D68EA1DCBE1903A58032BDB305.css" />
I think you would want to use the WithOutputBaseHref method. Here is what it looks like:
Bundle.Css()
.Add("~/file1.css")
.Add("~/file2.css")
.WithOutputBaseHref("http://static.mydomain.com")
.Render("~/content/combined-css_#.css")
Does it work for you?

Symfony: question about paths

in the cover page (login, register...) of my app i have this line:
<link rel="stylesheet" type="text/css" href="/css/formularios.css">
When i deploy my app, the that css rule is not loaded because, as i can
see in Firebug, it's looking for that rules in
www.tirengarfio.com/css/formularios.css instead of
www.tiregarfio.com/rs2/web/css/formularios.css.
What should i do?
Javi
You should use the view.yml config file and the include_stylesheets() helper. However, if you'd like to create the link tags by hand, use the public_path() helper to get the correct path.

Asp.net MVC Routing Problem

how many methods for adding style sheets in a page using Asp.net MVC
Wherever you're specifying the CSS for your details page instead of a relative path e.g.
<link href="../../Content/CSS/details.css" rel="stylesheet" type="text/css" />
try using the content helper and specifying a virtual path instead
<link href="<%= Url.Content("~/Content/CSS/details.css") %>" rel="stylesheet" type="text/css" />
It seems that the site is having trouble loading getting to the CSS file based on a relative link.
Use absolute links to css instead of relative (eg /Content/site.css" instead of "../Content/site.css"). Also you may use Html.Stylesheet("~/Content/site.css") extension (in MvcContrib library) to specify a stylesheet.
Is the problem not getting the right CSS? If so, then I would check your Details.aspx file and make sure the link to the CSS is the right path. Most likely, your Details.aspx file got moved into a new subdirectory or into another directory, thus making the relative paths between the aspx file and the CSS file different.
I would check the page source from the browser and look to see what the path to the CSS file is. The way I would solve the problem would be to modify the aspx file to use a fully-qualified path to the css file. Make sure that works. Then try changing the full path to use relative path.
I had the same issue when working through an example in an MVC book, it mentioned something about the '~' character only working because the <head> tag has the runat="server" attribute on it. So, I tried adding the runat attribute to the link tag itself, as seen below, and it worked:
<link runat="server" href="~/Content/styles.css" rel="stylesheet" type="text/css" />

Resources