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.
Related
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 am looking at some older HTML in an .ascx file which has an embedded code block "<% Some Server Logic %>". I get that best practice is to control server logic on the server, for maintainability and debugging and.... I see that it is bad. Not my code I am just trying to understand why someone would put this in the code. Is it just old code that at the time was cutting edge and why it's implemented or is it just junk?
The code is testing a user's role and based on the role they then execute/render the HTML within the conditional statement. Does this prevent the HTML from rendering at all, or should it?
Here is a sample:
<% if (userRole.IsInRole("someRole") || userRole.IsInRole("AnotherRole")) { %>
<section id="someId">
<div>Some Html</div>
</section>
<% } %>
I'm trying to get css working on this rake task.
namespace :task do
task test: :environment do
ticketmaster_url = "http://www.ticketmaster.co.uk/derren-brown-miracle-glasgow-04-07-2016/event/370050789149169E?artistid=1408737&majorcatid=10002&minorcatid=53&tpab=-1"
doc = Nokogiri::HTML(open(ticketmaster_url))
#psec-p label
doc.css("#psec-p").each do |price|
puts price.at_css("#psec-p")
byebug
end
end
end
However i'm returning this:
#<Nokogiri::XML::Element:0x3fd226469e60 name="fieldset" attributes=[#<Nokogiri::XML::Attr:0x3fd2281c953c name="class" value="group-price widget-group">, #<Nokogiri::XML::Attr:0x3fd2281c9528 name="id" value="psec-p">] children=[#<Nokogiri::XML::Text:0x3fd2281c8d44 "\n ">, #<Nokogiri::XML::Element:0x3fd2281c8c7c name="legend" attributes=[#<Nokogiri::XML::Attr:0x3fd2281c8c18 name="id" value="psec-p-legend">] children=[#<Nokogiri::XML::Text:0x3fd2281c8614 "Price:">]>, #<Nokogiri::XML::Text:0x3fd2281c8448 "\n ">]>
I'm guessing i selected the wrong element as i have chosen psec-p
Could someone let me know where i'm going wrong?
I've been following the railscast 190
The prices on http://www.ticketmaster.co.uk are applied to the HTML dynamically, via Javascript. This is partially done to hinder scraping efforts. You really cannot use Nokogiri to scrape this type of content from this domain, as Nokogiri processes raw HTML/XML, and does not execute Javascript in the process. Other tools exist to do this, but those would require an entirely different approach.
For learning purposes, you should choose a less dynamic site. For instance, http://www.wallacesuk.com has a nice, parseable site. You could easily learn basic web scraping techniques with a site that presents information inline with the page, such as this.
Scraping from http://ticketmaster.co.uk would require advanced scraping techniques, well beyond what Railscast 190 is demonstrating.
This:
doc.css("#psec-p").each do |price|
puts price.at_css("#psec-p")
byebug
end
can be better written using:
puts doc.at('#psec-p')
#psec-p is an ID, which can only occur once in a page, so at or at_css will find that one occurrence.
I'm quite new to ruby and I'm trying to put the form to add a new post to the "application.html.erb" page.
Somehow I can't get it to work by just copying the code from the "app/views/things/_form.html.erb" page, and also I can't seem to get it to work by copying the code from the "app/views/things/new.html.erb" page.
If I do that I get the error:
First argument in form cannot contain nil or be empty
The error points towards:
<%= form_for(#thing) do |f| %>
This is a code that I find in the "app/views/things/_form.html.erb" page.
Can anybody point me in the right direction? To be honest, I don't even know where to start looking for the answer.
I think you should take a look at ruby on rails MVC model first, and also go through a basic tutorial. Just a quick review of you question:
application.html.erb is usually in the layout folder of rails,
it's job is to provide a root template/layout for the whole project if not specified.
usually you don't want to put any code that is not commonly
needed by all pages.
the reason it is giving you the error First argument in form cannot contain nil or be empty is because, the helper method form_for is expecting the controller to provide a #thing to the view file application.html.erb, in your case, it is obvious that #thing is not exist in your controller or your ApplicationController.
The #thing should be initialized in your controller. I suppose you've got a things_controller.rb that has a new action in it. In this action, controller should initialize #thing and therefore it is visible in app/views/things/new.html.erb.
You really shouldn't put forms in application.html.erb, but just to make the error disappear, you could do
<%= form_for(Thing.new) do |f| %>
(assuming you've got a Thing model in app/models/thing.rb).
This way you'll initialize an instance of Thing right in your application.html.erb view.
I let users embed videos from Youtube, Google, Vimeo etc. I thought about the best and most secure approach (I don't want them to be able to include any flash and I also want to restrict the Videosites to exclude free porn websites etc.).
So I thought the best and easiest thing would be to let the user just copy&paste the URL of the video into a text-field, store it in a ExternalVideo Model and then just generate the needed HTML to embed the video.
So my ExternalVideo Model has a function called "embed_html" which should return the proper HTML.
Of course I could do something like this:
def embed_html
# just a very short example to make my point
"<embed src='#{#video_source}'>"
end
But I think that's bad practice and very unreadable.
My Question: Is there a tool / Gem / Built-in function I can use to generate custom HTML, something like the View Helpers (link_to, image_tag, etc)?
Thanks for your help!
I would do the following
def embed_element(external_video)
content_tag(:embed, '', :src => external_video.video_source)
end
You should probably check the docs for more information on the content tag method.
Also note that the content_tag() method will insert a closing tag. Something you seem to be forgetting...
You almost answered your question. Use helper method:
def embed_html url
"<embed src='#{url}'>"
end
And use it in view:
<%= embed_html #video_source %>
Why don't you just write a view helper method that accepts a ExternalVideo model instance, asks it for its video URL and then returns the HTML?