I have stored html code in a database field which is passed to the rails app.
<%= raw #exercise.explanation %>
The normal text is rendered correctly, it´s in the <p> .. </p>; but inside there the image codes are just displayed so output on the page is:
This is correct rendered text.
<%= image_tag ("exercises/picture.png"), style: 'height:auto; width:50%;' %>
Further text.
When I use <img src="exercises/picture.png"> or <img src="picture.png">inside the database entry, no picture is loaded either, just the broken image symbol of the browser.
Do you have the Assets Pipeline on? If you do, this could happen because of the generated hash that it put on your static assets to avoid cache problems. The thing is that the image_tag helper resolves this. It's a trade-off, but you could disable the Assets Pipeline and see if it works. To keep it on and do this, you will need a more elaborated solution.
You can try if it is that by disabling the assets digesting:
config.assets.digest = false
Alternatively you can have ERB interpret #exercise.explanation
<%= ERB.new(#exercise.explanation).result(binding) %>
This will take your #exercise.explanation and process it through ERB which appears to be what you are hoping for.
Caveat:
Be very careful what you allow to be stored in these "template" fields as things can go bad if you do not/can not sanitize this input. ERB#result is essentially a call to eval (which can be very dangerous)
I've read similar questions, but none seems working for me.
I have an asp site, i think classic (don't even know difference between classic and net), with few pages all with asp extension, and some html includes, one of these being a slider which I want to only display when in homepage (default.asp), and if not default then add a div to the markup.
I know the following is wrong, but just to better explain my need.
<%
if page == default.asp
include file="slider.html"
if page != default.asp
<div class="spacer-top"></div>
%>
Like many of your predecessors in ASP-classic-land, what you're wanting is conditional includes, and the problem you're bumping into is that classic ASP doesn't do conditional includes. (The reason why is that the #include directive is handled long before any script on the page is parsed.)
There are various workarounds involving Execute or other dangerous-in-the-wrong-hands commands; search for "asp conditional include" and you'll find more than you were bargaining for. However, in your case, it might be simpler to encase the slider display in a subroutine that you can call or not.
Slider.html:
<%
Sub DisplaySlider()
'code to display the slider (probably JavaScript, I'm guessing?)
%>
<script ...>
</script>
<%
End Sub
%>
Other pages:
<!-- #include virtual="/slider.html" -->
<%
scriptname = Request.ServerVariables("Script_Name")
If InStr(scriptname, "default.asp") > 0 Then
DisplaySlider
Else
Response.Write "<div class='spacer-top'></div>"
End If
%>
Martha is bang on the nail.
To augment her answer I feel I should point out that system design comes into play, here. Try keeping your modules small and succinct, targeting their functionality to a particular aspect of your application's requirements. For example:
one to deal with your data layer
one to deal with more advanced form handling
one to deal with blah
You get the idea.
Another idea is to include common functions in your global.asa so they're available to all modules within your application instantly.
I have an asp.net application and in that I have a html page.
I need to get the Windows username in that html page.
I understand its not possible in normal page.
I need to achieve this without using ActiveX objects.
you will need to create an AJAX call on your page to some WebMethod in your ASP.NET application that call will return you user name.
it basicaly comes down to placeing request to the WebMethod like this
the call below gets data from WebMothod and displays it in the div in the page
$.get('jqueryintro.htm', function (data) {
$('#maindiv').html(data);
});
If you will need more information you can use this page very easy to follow article.
And don't forget to include jQuery ($) in your page
Assuming you have Windows authentication enabled on the site and the user is authenticated you can put this in your ASPX:
<p><%= User.Identity.Name %></p>
The above code embeds the username in the HTML that is sent to the client.
An AJAX request is not required.
Try something like this...
<div runat="server"><%= System.Security.Principal.WindowsIdentity.GetCurrent().Name %></div>
<div runat="server"><%= User.Identity.Name %></div>
<div runat="server"><%= Request.ServerVariables["LOGON_USER"].ToString() %></div>
As long as you are using Windows Auth it should work, otherwise you would just get the account info for whatever IIS is running under.
Any container with runat="server" should work really it doesn't have to be a div.
I have the following VBS code in my webpage:
<body>
<%
Randomize
response.write(Rnd)
%>
</body>
But instead of outputting a random number, I get a webpage that says this:
<% Randomize response.write(Rnd) %>
Why won't my code execute?
Thank you.
You need to make sure your webpage extension is managed by the ASP handler mapping. The steps to configure handler mappings depend on the version of IIS you are using.
See this page for information on configuring ASP under IIS 7 or 7.5.
Is there a decent way with static HTML/XHTML to create common header/footer files to be displayed on each page of a site? I know you can obviously do this with PHP or server side directives, but is there any way of doing this with absolutely no dependencies on the server stitching everything together for you?
Edit: All very good answers and was what I expected. HTML is static, period. No real way to change that without something running server side or client side. I've found that Server Side Includes seem to be my best option as they are very simple and don't require scripting.
There are three ways to do what you want
Server Script
This includes something like php, asp, jsp.... But you said no to that
Server Side Includes
Your server is serving up the pages so why not take advantage of the built in server side includes? Each server has its own way to do this, take advantage of it.
Client Side Include
This solutions has you calling back to the server after page has already been loaded on the client.
JQuery load() function can use for including common header and footer. Code should be like
<script>
$("#header").load("header.html");
$("#footer").load("footer.html");
</script>
You can find demo here
Since HTML does not have an "include" directive, I can think only of three workarounds
Frames
Javascript
CSS
A little comment on each of the methods.
Frames can be either standard frames or iFrames. Either way, you will have to specify a fixed height for them, so this might not be the solution you are looking for.
Javascript is a pretty broad subject and there probably exist many ways how one might use it to achieve the desired effect. Off the top of my head however I can think of two ways:
Full-blown AJAX request, which requests the header/footer and then places them in the right place of the page;
<script type="text/javascript" src="header.js"> which has something like this in it: document.write('My header goes here');
Doing it via CSS would be really an abuse. CSS has the content property which allows you to insert some HTML content, although it's not really intended to be used like this. Also I'm not sure about browser support for this construct.
The simplest way to do that is using plain HTML.
You can use one of these ways:
<embed type="text/html" src="header.html">
or:
<object name="foo" type="text/html" data="header.html"></object>
You can do it with javascript, and I don't think it needs to be that fancy.
If you have a header.js file and a footer.js.
Then the contents of header.js could be something like
document.write("<div class='header'>header content</div> etc...")
Remember to escape any nested quote characters in the string you are writing.
You could then call that from your static templates with
<script type="text/javascript" src="header.js"></script>
and similarly for the footer.js.
Note: I am not recommending this solution - it's a hack and has a number of drawbacks (poor for SEO and usability just for starters) - but it does meet the requirements of the questioner.
you can do this easily using jquery. no need of php for such a simple task.
just include this once in your webpage.
$(function(){
$("[data-load]").each(function(){
$(this).load($(this).data("load"), function(){
});
});
})
now use data-load on any element to call its contents from external html file
you just have to add line to your html code where you want the content to be placed.
example
<nav data-load="sidepanel.html"></nav>
<nav data-load="footer.html"></nav>
The best solution is using a static site generator which has templating/includes support. I use Hammer for Mac, it is great. There's also Guard, a ruby gem that monitors file changes, compile sass, concatenate any files and probably does includes.
The most practical way is to use Server Side Include. It's very easy to implement and saves tons of work when you have more than a couple pages.
HTML frames, but it is not an ideal solution. You would essentially be accessing 3 separate HTML pages at once.
Your other option is to use AJAX I think.
You could use a task runner such as gulp or grunt.
There is an NPM gulp package that does file including on the fly and compiles the result into an output HTML file. You can even pass values through to your partials.
https://www.npmjs.com/package/gulp-file-include
<!DOCTYPE html>
<html>
<body>
##include('./header.html')
##include('./main.html')
</body>
</html>
an example of a gulp task:
var fileinclude = require('gulp-file-include'),
gulp = require('gulp');
gulp.task('html', function() {
return gulp.src(['./src/html/views/*.html'])
.pipe(fileInclude({
prefix: '##',
basepath: 'src/html'
}))
.pipe(gulp.dest('./build'));
});
You can try loading them via the client-side, like this:
<!DOCTYPE html>
<html>
<head>
<!-- ... -->
</head>
<body>
<div id="headerID"> <!-- your header --> </div>
<div id="pageID"> <!-- your header --> </div>
<div id="footerID"> <!-- your header --> </div>
<script>
$("#headerID").load("header.html");
$("#pageID").load("page.html");
$("#footerID").load("footer.html");
</script>
</body>
</html>
NOTE: the content will load from top to bottom and replace the content of the container you load it into.
No. Static HTML files don't change. You could potentially do this with some fancy Javascript AJAXy solution but that would be bad.
Short of using a local templating system like many hundreds now exist in every scripting language or even using your homebrewed one with sed or m4 and sending the result over to your server, no, you'd need at least SSI.
The only way to include another file with just static HTML is an iframe. I wouldn't consider it a very good solution for headers and footers. If your server doesn't support PHP or SSI for some bizarre reason, you could use PHP and preprocess it locally before upload. I would consider that a better solution than iframes.