call render partial from view in elixir - rendering

I'm learning Elixir with Phoenix and just got stuck in a pretty dumb point. I want to call the render of a partial from inside my index template the following way:
#index.html.slim
- for element_here <- array_here do
= render MyApp.SharedView, "_game.html.slim", element: element_here
For this I created a View called shared_view.ex that looks like this:
defmodule MyApp.SharedView do
use MyApp.Web, :view
def render("game", _assigns), do: "/shared/_game.html.slim"
end
I expected it go through the loop rendering shared/_game.html.slim, which I copy here:
.col-md-4.portfolio-item
a href="#"
img.img-responsive alt="" src="http://placehold.it/700x400" /
h3
a href="#" Project Name
p Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam viverra euismod odio, gravida pellentesque urna varius vitae.
But nothing is being rendered. And I'm not getting an error neither. It just render the stuffs before and after that.
I'm not sure what I'm missing here. There's no route or controller action connected to "_game" partial because I didn't think it was neccesary (I'm used to rails and it works this way there).

Turned out to be a spelling issue. There were two problems:
Slim extension should not be explicit, as #radubogdan explained.
Loop should be added using = for instead of - for, as #Dogbert said.
In the end it looks like this:
index.html.slim
= for element_here <- array_here do
= render MyApp.SharedView, "_game.html", element: element_here
shared_view.ex
defmodule MyApp.SharedView do
use MyApp.Web, :view
def render("game", _assigns), do: "/shared/_game.html.slim"
end
_game.html.slim
.col-md-4.portfolio-item
a href="#"
img.img-responsive alt="" src="http://placehold.it/700x400" /
h3
a href="#" Project Name
p Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam viverra euismod odio, gravida pellentesque urna varius vitae.

Related

How to extract value of all classes in beautiful Soup

I have a HTML file with a structure like this:
<p id="01">... EU legislation and the <em>monetary power</em> of the
<span class="institution" Wikidata="Q8901" name="European Central Bank">ECB</span>.</p>
<p id="02"><span class="person" Wikidata="Q563217">Guido Carli</span>, Governor of the
<span class="institution" Wikidata="Q806176">Bank of Italy</span> ...</p>
I need to have a Python dict like this:
{'institution': ['Q8901', 'Q806176'], 'person': ['Q563217']}
So I need to get the value of the class attribute of all span tags, along with their text. How can I do this with bs4?
Select your elements and iterate the ResultSet while appending the values to your dict. To extract the values of an attribute use .get(). Because class will give you a list pick yours by index or key.
Example
from bs4 import BeautifulSoup
html = '''
<p id="01">... EU legislation and the <em>monetary power</em> of the
<span class="institution" Wikidata="Q8901" name="European Central Bank">ECB</span>.</p>
<p id="02"><span class="person" Wikidata="Q563217">Guido Carli</span>, Governor of the
<span class="institution" Wikidata="Q806176">Bank of Italy</span> ...</p>
'''
soup = BeautifulSoup(html)
d = {
'institution':[],
'person':[]
}
for e in soup.select('span[wikidata]'):
d[e.get('class')[0]].append(e.get('wikidata'))
d
Output
{'institution': ['Q8901', 'Q806176'], 'person': ['Q563217']}
This is the way I solved my problem thanks to #HedgeHog.
from bs4 import BeautifulSoup
from collections import defaultdict
def capture_info(soup: 'BeautifulSoup') -> defaultdict:
info = defaultdict(list)
for i in soup.select('span[Wikidata]'):
info[i.get('class')[0]].append(i.get('wikidata'))
return info
html = '''
<p id="01">... EU legislation and the <em>monetary power</em> of the
<span class="institution" Wikidata="Q8901" name="European Central Bank">ECB</span>.</p>
<p id="02"><span class="person" Wikidata="Q563217">Guido Carli</span>, Governor of the
<span class="institution" Wikidata="Q806176">Bank of Italy</span> ...</p>
'''
soup = BeautifulSoup(html, 'html.parser')
info = capture_info(soup)
The output is:
{'institution': ['Q8901', 'Q806176'], 'person': ['Q563217']})

translate pipe inside v-html directive i18 vue with vuex

I'm using vuex-i18n package. Basically I'm translating most of my content by filter pipe like so:
{{ header | translate }}
but sometimes I need to translate text which in fact is just html and I bind it like so:
<p v-html="paragraph"></p>
but this doesn't work with pipe:
<p v-html="paragraph | translate"></p> // display ()
paragraph in above example is:
<p class="balears__text">Jest największym miastem Majorki, kosmopolityczną stolicą Balearów i prężniedziałającym, śródziemnomorskim ośrodkiem turystycznym.</p><p class="balears__text"> Katedra La Seu to zdecydowanie najsłynniejsza budowla Palmy. Ta gotycka świątynia z elementami architektury zaprojektowanej przez słynnego Gaudiego stanowi wizytówkę miasta i jest symbolem całej wyspy. </p><p class="balears__text"> Nieopodal katedry znajduje się pałac królewski La Almudaina. Spacer po komnatach i dziedzińcu sprawi, że poczujesz się, jakbyś wędrował po tych zabudowaniach w czasach ich świetności. </p><p class="balears__text"> Doskonałym pomysłem na rodzinne popołudnie jest wizyta w jednym z największych kompleksów akwariowych na świecie. W Palma Aquarium możesz przeżyć niesamowitą przygodę, nurkując z rekinami w najgłębszym zbiorniku w Europie, tzw. Big Blue. </p>
I found solution like so:
<p v-html="$options.filters.translate(paragraph)"></p>

Selenium find all <em> and the following <a> tags

I want to get all em and the following a tags, but these are seperated:
<em style="color: #FF2500;">ITEM:</em> LINK <br />
<em style="color: #FF2500;">ITEM2:</em> LINK2 <br />
<em style="color: #FF2500;">ITEM3:</em> LINK3 <br />
I need to save the ITEM and the correspinding LINK, because they must be together, but I only managed to find the text of the links:
elems = driver.find_elements_by_xpath("//em/following-sibling::a[#href]")
printing this gives me only the contents of the link:
elems = driver.find_elements_by_xpath("//em/following-sibling::a[#href]")
for link in elems:
print (link.text) # LINK, LINK2, LINK3
I could of course find all em and links by themself, but I wouldnt know if they fit together. So I need to find:
All <em> where an <a> with a certain text follows. This way I should know for sure that they are together.
Searches em preceding to a with some text
//a[text()=' LINK2 '] | //a[text()=' LINK2 ']/preceding-sibling::em[1]
to get just text concatenated from both elements
concat(//a[text()=' LINK2 ']/preceding-sibling::em[1], //a[text()=' LINK2 '])
result:
"ITEM2: LINK2 "

IntelliJ: wrap javadoc text but not markup

I'm trying to format the following Javadoc, but I can't figure out how.
Example input:
/**
* Headline.
* <p>
* Lorem ipsum dolor sit amet,
* consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
* <p>
* A list:
* <ul>
* <li>The description above should be wrapped at the right margin, and broken lines should be joined.</li>
* <li>A line starting or ending in a tag should not be joined.</li>
* </ul>
*
* #author Mark Jeronimus
*/
When I press 'format' I want to see this:
/**
* Headline.
* <p>
* Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt
* ut labore et dolore magna aliqua.
* <p>
* A list:
* <ul>
* <li>The description above should be wrapped at the right margin, and broken lines should
* be joined.</li>
* <li>A line starting or ending in a tag should not be joined.</li>
* </ul>
*
* #author Mark Jeronimus
*/
Eclipse and NetBeans do this easily. IntelliJ, if I configure it to wrap text (which I require) it also joins tags except <p>. (Settings -> Editor -> Code Style -> Java -> JavaDoc -> Other -> Wrap at right margin). It looks like this:
/**
* Headline.
* <p>
* Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt
* ut labore et dolore magna aliqua.
* <p>
* A list: <ul> <li>The description above should be wrapped at the right margin, and broken
* lines should be joined.</li> <li>A line starting or ending in a tag should not be joined.
* </li> </ul>
*
* #author Mark Jeronimus
*/
I tried changing the other settings in the hope some of them interfere with each other, to no avail.
What I don't want is to use the Eclipse Formatter plugin. I feel IntelliJ should be able to handle such basic behavior itself, or how is anyone supposed to format their Javadoc in a normal way?
Open Preferences
Select Editor > Code Style > Java
Open the JavaDoc tab
Set the following options
Blank lines
 ✓ After description This is a javadoc standard, other blank lines are optional
Other
 ✓ Wrap at right margin
 ✓ Enable leading asterisks
 ✓ Generate "<p>" on empty lines
 ✓ Keep empty lines
 ✗ Preserve line feeds
Click OK
Then just use Code > Reformat Code
Of course you can tweak the preferences according to your own...preference. This setup will realign the text to fit in the margins but you will have to put the <ul> tag on a new line before formatting.

How to process JSON output from WCF in View( MVC3)

I have a scenario where the WCF returns the follwing data ( in the function given below) to a VIEW.
private List<KeyDatesCalendar> GetKeyDatesCalendarData()
{
//Dummy Data for BrandsCalendar CheckList
var keyDatesCalendar = new List<KeyDatesCalendar>()
{
new KeyDatesCalendar()
{
EventText = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
EventDate = new DateTime(2011, 02, 09),
EventType = 3
},
new KeyDatesCalendar()
{
EventText = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
EventDate = new DateTime(2011, 03, 05),
EventType = 3
},
new KeyDatesCalendar()
{
EventText = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
EventDate = new DateTime(2011, 03, 06),
EventType = 4
},
};
The processing of the data in view is done by the following code:
initCalendars({
from : '02/01/2011',
to : '01/31/2013',
dates : [
#for(int i=0, l=#Model.KeyDatesCalendar.Count; i<l; i++)
{
#Html.Raw("['" + #Model.KeyDatesCalendar[i].EventDate.ToString("yyyy/MM/dd") + "'," + #Model.KeyDatesCalendar[i].EventType + ",'" + #Model.KeyDatesCalendar[i].EventText + "']" + (i < (l-1) ? "," : ""));
}
]
});
Instead of the hardcoded values in WCF method, how Do i recieve a JSON output and process the same in View.
I am a beginner here, appreciate your detail answers.
Thanks,
Adarsh
I would agree with many of the previous comments, if you're using ASP.NET MVC you might as well do the JSON conversion from there (have a look at JsonResult class). However, if you really want the WCF service to return the result in JSON format, this blog post I wrote a while back might help.
Iain