I have the following situation. I have one content element in my backend layout where I add my records and I can successfully display them on the front end. However, I would like to have each new record inside my content element to get wrapped by a <li></li>, instead of having all in one.
My understanding is it would be best to use a foreach loop, but I'm not sure where to start. I know how to create the loop, but don't know how to get my content element inside it.
This is how I call the content:
styles.content.content2 < styles.content.get
styles.content.content2.select.where = colPos=1
lib.content2 < styles.content.content2
And in the HTML:
<li>
<f:cObject typoscriptObjectPath="lib.content2"></f:cObject>
</li>
you can use the VHS extension
https://fluidtypo3.org/viewhelpers/vhs/1.8.0/Content/RenderViewHelper.html
Related
I have written a ruby code where the browser object finds all links and then I store them one by one in an array if they match a specific regex.
#browser.links.collect(&:href).each do |link|
matches = regex.match(link)
array_of_multimedia << matches[:multimedia_id] if matches
end
I am trying to create a filter where I only iterate over those links where the span inside the second child div contains the aria-label as Multimedia.
Attached is the screenshot of the HTML structure.HTML structure
I tried a few approaches like finding all spans and then going bottom up to the parent's parent of the span but its not giving me the href.
#browser.spans(aria_label: "Multimedia").each do |span|
span.parent.parent.a.hreflang #Didn't work
span.parent.parent.a.link.href #Didn't work
span.parent.parent.href.text #Didn't work
element.tag_name #This shows "a" which is correct though
end
I also tried a top down approach by doing
#browser.links.collect(&:href).each do |link|
link_element = #browser.link(href: link)
link_element.children.following_sibling(aria_label: "Multimedia").present? #Didn't work
end
So far, no luck in getting the actual hrefs. Will appreciate any help!
Because the span is inside the link tag, it's going to be easier to go bottom up
Do as much as you can with the Watir locators rather than multiple loops.
The parent method takes arguments:
#browser.spans(aria_label: 'Multimedia').map {|span| span.parent(tag_name: 'a').href }
As for what you tried:
# parent.parent is the link, so calling `#a` is looking for a link nested inside the link
span.parent.parent.a.hreflang
span.parent.parent.a.link.href
# href should give you a String, you shouldn't need to call #text method on it
span.parent.parent.href.text
# element isn't defined here, but try just element.href
element.tag_name
Also note that Element#href method is essentially a wrapper for Element#attribute_value('href').
I installed Nokogiri into a Rails project and it can currently run "Nokogiri HTML Parser Example" with no issues.
I'm trying to create a Rails project that will parse a movie script from IMDB, conduct a word count, then display the most occurring words from that section. I've identified that the scripts are kept in a "table":
<table width=100% border=0 cellpadding=5 class=scrtext><tr><td class=scrtext><pre><html><head></head><body>
<b>PERSON1</b>
They say some dialogue
<b>PERSON2</b>
They say some more
</pre></table>
I would like to exclude the text within the <b>/<b> brackets as well.
I've been setting this up like the example above in the controller, and have gotten as far as taking in the URL:
#Save as a temp. file
tmp_file = open('http://www.imsdb.com/scripts/Authors-Anonymous.html')
#Parse the temp. file
doc = Nokogiri::HTML(tmp_file)
I'm having difficulty understanding how to set the CSS constraints to grab this table. I understand that it's between those <pre>/<pre> tags, and I've followed a number of tutorials for this but I still don't understand how to set up those constraints.
I feel that the code following this should be something like this, but I'm not awfully sure:
doc.search("//pre")
How do I set up Nokogiri's CSS constraints to pull the content between two tags such as <pre></pre>, and then filter out irrelevant tags such as <b></b> that will occur within the output?
You can use the css method selector: doc.css('pre b') which will get every <b> tag(s) inside every <pre> tag(s):
doc.css('pre b').each do |b_tag|
# b_tag will be a String containg like `<b>this text is bold</b>`
end
It might not be the most elegant solution but it did the trick for me.
In the controller, I defined the following:`
def index
page = [THE_URL]
doc = Nokogiri::HTML(open(page))
#content = doc.css('b').remove
#content = doc.css('pre')
puts #content
end
and then in the View;
<%=#content %>
I have a simple controller that was created through a scaffold and has a "Show" function.
In the view that was created through the scaffold, I have an image that appears only on a certain condition.
I want to set or evaluate the condition in the controller and send it to the view.
I am not sure how to do that or whether this is the correct approach.
The condition should generally be dealt with in the view file using erb, or haml. If you update your question with the condition, then I'll see about updating my answer to reflect it. For now, I'll use a common condition.
Say you only want to show an image if an object is featured. Let's imagine there is a featured field in your object that acts as a flag (1,0).
If this object is say an Article, we can then check the condition in the view file. The controller would obtain the article from the model:
-# articles_controller show action
#article = Article.find(params[:id])
..
-# views/articles/show.html.erb
<% if #article.featured? %>
// show image here
<% end %>
Remember this is an example condition that is not necessarily correct. It is just to illustrate my initial approach.
I wouldn't suggest you use javascript to hide/show depending on this condition, because you are then putting your logic in javascript, when it can be easily managed from within your view files.
If the condition is complex, you would then move it to the model, and perform something like:
if #article.some_complex_condition?
..rather than having that complex condition in your controller file. This allows you to reuse the condition away from the specific controller and makes it more testable.
If you just want to show and hide an image based on a certain condition, than you can do that with JQuery. You shouldn't put anything in the controller that is view-centric.
You can also get the id of whatever data element is in 'show' and pass it to the JavaScript.
JQuery has show() and hide() methods that would work for you. Here's the documentation on the hide method: http://api.jquery.com/hide/
Basically, if you had a certain id for your image, you'd do something like this:
$(document).ready(function() {
$("#myImage").hide();
if (some_condition === true) {
$("#myImage").show();
}
});
You can put that code in your application.js file.
I whipped up a simple JsFiddle demonstrating a way to show and hide with buttons:
http://jsfiddle.net/phillipkregg/92RDS/
Of course, the code may be different depending on what you are trying to do.
If you need to get the 'id' of the object in the 'show' view, than you can put a script tag at the bottom of your show view like this:
<script type="text/javascript">
var my_show_object = <%= #some_object.id %> //this will be the specific id of whatever the object is that you passed from the controller
alert(my_show_object); //this helps with debugging to see if you have the right id
</script>
If you need more than the id, and you want to get the entire Rails object and represent it as Javascript - just do this at the bottom of your rails 'show' view:
<script type="text/javascript">
var my_object = <%= #your_rails_object.to_json %>;
console.log(my_object); //This code allows you to look in the console and see exactly what your object looks like.
</script>
Just like the last one, #your_rails_object is the variable that you have in your show view in the controller. This will return it as a json object so that you can get the id or whatever properties it has.
Also, if you are just learning Rails, I would recommend this online book: http://ruby.railstutorial.org/ruby-on-rails-tutorial-book
That's how I learned Rails - the book is excellent and free.
I have some pages which are filled dynamically by content loaded with Ajax. My problem is that each time I go to this page, the old content is still there if it hasn't been replaced by new content...
I've thought about 2 home solutions like:
Creating a "template" page. By calling "pagebeforeshow", I'll copy the code from the template in the target page, and add there the dynamic content...
Each DOM where dynamic content must be put into, I had a class "clearcache" and by calling "pagebeforeshow" I do a $(".clearcache").empty();
I don't know how to deal with that. Have you ever got the same issue?
EDIT:
I bind the "tap" event to store the block-id into localstorage, to load dynamic content in the #PageBlock
Everything works very well (tap event, localstorage for the var, ajax loading). The issue comes really when I go from block to other blocks. The new content overwrite old content instead of beginning from a new "blank" page.
For example I have a list where I append datas I get from Ajax. If I switch to another block, the list is completed and not refreshed..
I could do something like empty the list, and then appending content, but I'd like something better because I have several pages/lists/dom like that...
Thanks for your help ;)
I faced a similar problem where the new contents where not shown on the page when i tried to append it.There is a simple solution where you can just replace append with prepend.
Example:
Replace
$("#divid").append(content)
with
$("#divid") .prepend(content)
So I followed the tutorial and screencast but can't seem to get this to work. I can see the div id's called map container and gmaps4rails_map where presumably, the map is supposed to be but nothing else.
I put in the yield :head and yield :script in the header and footer of the application layout view and #charger = Charger.all.to_gmaps4rails (Charger is my model) in the chargers_helper.html.erb file (I want to use in User show view).
I also put <%= gmaps4rails(#charger) %> in the show.html.erb view.
How do I get this to work?
-Update-
I just got it to work. To fix the problem I put in the #charger = ... code in a method in the helper and called the method instead of #charger.
Now the map shows up but I still cannot see the insert new marker code like in the screencast - I don't need it for my app but am curious as to what happened to it.
In the screencast, I create model + controller with a scaffold generator. That's why I have ready to use links.
Besides, I create buttons during the screencast. Nothing is made behind the scene: the whole app is created from scratch :)