Passing a list as the parameter of a template - mediawiki

I have created a Template in my MediaWiki which I want to pass "free text" to. To keep it simple, imagine it as a table in which the first named parameter goes into cell 1 and the other named parameter goes into cell 2.
Writing the Template itself seemed easy enough, but I keep stumbling into errors when I try to pass certain structures in the arguments. My first issue was with a code block: if I wrote
{{MyTemplate|first=
// code...
|other=
// other code instead...
}}
then the wiki thought that the pipe character was part of the preformatted text block, and in the end I ended up without a second parameter. This issue was solved using <pre></pre>, but I thought it was more of a workaround than a solution.
Now I'm in trouble again when I try to pass a list:
{{MyTemplate|first=
* Item 1a
* Item 2a
|other=
* Item 1b
* Item 2b
}}
Again, I end up with the first item (Item 1a) ignored (not rendered as a list item), the second rendered ok (Item 2a, as a list item), then a double pipe, an asterisk and "Item 1b", then the list goes on with "Item 2b".
I am clearly doing something wrong, but I have no idea what to do, nor I could find any useful example in the docs. Please help me understand how to correctly pass any text (which may include lists or other markdown constructs) as a template argument.
EDIT
Here is the code of my Template:
{|
!First !!Other
|-
|{{{first|}}} ||{{{other|}}}
|}
Also, I'm sorry, but I typed the usage wrong: the four more spaces were a leftover from the snippet above. I've fixed that.
#Ivanhercaz I've tried your suggestion (which only differs from mine for the empty lines), and still got the same result.

In this case I have to suppose how is the template you created, because you didn't shared the structure, just how you call and two parameters. Because of this reason I am going to suppose that you create a template as:
{{{first|}}}
{{{other|}}}
About your first question, you can use {{!}} to escape the pipe character (|). From the version 1.24 this is a magic word, but before the solution to this issue was to create a template with a pipe and then replace the | by {{!}}. The use of the magic word is the same, but you is unnecessary to create the template.
About your second question, taking in account that you have a similar structure to what I wrote at the top of this answer, if I wrote a page with this:
{{MyTemplate|first=
* Item 1a
* Item 2a
|other=
* Item 1b
* Item 2b
}}
I get a two lists and two code blocks: list one with "Item 1a" and the first code block with * Item 2a; list two with "Item 2a" and the second code block with * Item 2b. It doesn't seem to what you get rendered:
the first item (Item 1a) ignored (not rendered as a list item), the second rendered ok (Item 2a, as a list item), then a double pipe, an asterisk and "Item 1b", then the list goes on with "Item 2b". This happened due to the extra space before the * in the parameters.
So I recommend to you to share the code of the template to check and answer to you better. Of course, if what you want is to pass two different lists in each parameter you could make something like:
{{Test|first=
* Item 1a
* Item 2a
|other=
* Item 1b
* Item 2b
}}
This structure solved the problem I mentioned. But I wait your comment to be sure and edit my answer if it is necessary.
Edit
Now, with the code of the template I understand your problem. To work correctly and without problems with templates, tables and lists you have to consider that the * need to have an line break after the | and ||. What I recommend to you is insert this line breaks
{|
! Wikitext !! Rendering
|-
|
{{{first|}}}
||
{{{other|}}}
|}
I think it is an easy and clean way to do what you are trying. So, if you use the template in this way:
{{Test
|first=* Item 1a
* Item 2a
|other=* Item 1b
* Item 2b
}}
Or in this other way:
{{Test
|first=
* Item 1a
* Item 2a
|other=
* Item 1b
* Item 2b
}}
You will have a table like that (I add a class=wikitable to have a more beautiful table to show):
I hope this trick has been helpful.

Related

What does the "ved" parameter in a google search refer to?

I've spent like two hours or more trying to figure out what a "ved" parameter on a Google search means. A curious person I am.
My finds so far:
$ved value changes-
1 - every different search result (diff keywords)
2 - every different resulted block (the url blocks/boxed on the resulted google search, but they are quite similar, as I'll write down below)
3 - every different geolocation perhaps
Consider these tests or lookups:
1-
Diff keywords, but first block/position in list:
&ved=2ahUKEwidsaSd4M_1AhVlk_0HHUxOCQYQFnoECAsQAg
&ved=2ahUKEwj2pZyN5s_1AhVRmuYKHZ5IB5EQFnoECAcQAg
I thought the "ved" value refers to the block/position of a url in the result list, but no.
2-
Twree different urls, first and second from the 1st and 2nd blocks of first page, then third from a "much farther on the list" block:
ved=2ahUKEwjq1-Wb1s_1AhW6SWwGHZwpBMwQFnoECD8QAQ
ved=2ahUKEwjq1-Wb1s_1AhW6SWwGHZwpBMwQFnoECCAQAQ
ved=2ahUKEwiZ2NDe1s_1AhVaTmwGHThIA5U4PBAWegQIGRAB
The same website url, from different countries (not considering blocks or position in list):
&ved=2ahUKEwiopK2X08_1AhUgxzgGHQEbDkcQFnoECBIQAQ
&ved=2ahUKEwjpueqC1M_1AhWJq3IEHYEDAfc4FBAWegQIDBAB
&ved=2ahUKEwih09Wz08_1AhUY7WEKHQYdBB8QFnoECEIQAQ
Very similar they are.
I'd really love to know what they mean. Any ideas are appreciated too!
I found an interesting article explaining the subject : https://moz.com/blog/inside-googles-ved-parameter
TL;DR:
A ved code contains up to five separate parameters, which each tell you something about the link that was clicked on:
1st (parameter1: Link index) gives you an idea of where the link was on the page.
2nd (parameter2: Link type) is a number that corresponds to the 'type' of the link that was clicked.
3rd (parameter7: Start result position) is the cumulative result position of the first result on the page.
4th (parameter 6: Result position) indicates the position of your page in the search results.
5th (parameter 5: Sub-result position) like the (parameter 6), except it tells you the position in a list of sub-results, such as breadcrumbs, or one-page sitelinks.

Printing a table with sage math

The assignment is to construct a two-column table that starts at x= -4 and ends with x= 5 with one unit increments between consecutive x values. It should have column headings ‘x’ and ‘f(x)’. I can't find anything helpful on html.table(), which is what we're supposed to use.
This what I have so far. I just have no idea what to put into the html.table function.
x = var('x')
f(x) = (5 * x^2) - (9 * x) + 4
html.table()
You might want to have a look at sage's reference documentation page on html.table
It contains the following valuable information :
table(x, header=False)
Print a nested list as a HTML table. Strings of html will be parsed for math inside dollar and double-dollar signs. 2D graphics will be displayed in the cells. Expressions will be latexed.
INPUT:
x – a list of lists (i.e., a list of table rows)
header – a row of headers. If True, then the first row of the table is taken to be the header.
There is also an example for sin (instead of f) with x in 0..3 instead of -4..5, that you can probably adapt pretty easily :
html.table([(x,sin(x)) for x in [0..3]], header = ["$x$", "$\sin(x)$"])
#Cimbali has a great answer. For completeness, I'll point out that you should be able to get this information with
html.table?
or, in fact,
table?
since I would say we want to advocate the more general table function, which has a lot of good potential for you.

D3 Key Function

As readers might gather from the following...I am fairly new to D3....I am experimenting at the moment using .enter() and .exit().remove(). I am trying to understand what the key function does...? I am viewing the DOM with Chrome > Console and cannot see any obvious differences between .data(dataSet, keyFunction) and without the key function .data(dataSet).
Can anybody please suggest any experiments to try (or console expressions) that may help me understand exactly what the mysterious key function does....
I'm also new to d3 and was struggling with the key function. I didn't find Tristan Reid's answer illuminating, because it doesn't really talk much about the key function.
Let's work through an example, first without a key function, and then with.
Here's our initial html before applying javascript. We've got two divs, and there is no data attached to anything.
<body>
<div>** First div **</div>
<div>** Second div **</div>
</body>
Calling data() with no key function
Let's add a couple lines of javascript.
var arr1 = [35, 70, 24, 86, 59];
d3.select("body")
.selectAll("div")
.data(arr1)
.enter()
.append("div")
.html(function(d) { return d });
What does our html look like now? Here is the html along with the values of the associated data (comments added).
<body>
<div>** First div ** </div> <!-- data: 35 -->
<div>** Second div ** </div> <!-- data: 70 -->
<div>24</div> <!-- data: 24 -->
<div>86</div> <!-- data: 86 -->
<div>59</div> <!-- data: 59 -->
</body>
The data() call matched an array of divs with an array of values by use of a key. The default keys used for the arrays is the indexes. So these are the keys that were used.
selected divs (by text) key data elements key
----------------------- --- ------------- ---
** First div ** 0 35 0
** Second div ** 1 70 1
24 2
86 3
59 4
Going by the keys, two of the data elements have matches in the selected divs -- those with keys 0 and 1. Those matching divs get bound to data, but nothing else happens them.
All the data elements without a matching key get passed to enter(). In this case, there is no match for the keys 2, 3, and 4. So those data elements get passed to enter(), and a new div is appended for each of them. The appended divs are also bound to their respective data values.
Calling data() with a key function
Let's change our javascript, keeping what we have but adding a couple more lines. We'll perform the same selects with a data call (with a different array), but this time using a key function. Notice the partial overlap between arr1 and arr2.
var arr1 = [35, 70, 24, 86, 59];
d3.select("body")
.selectAll("div")
.data(arr1) // no key function
.enter()
.append("div")
.html(function(d) { return d });
var arr2 = [35, 7, 24, 2];
d3.select("body")
.selectAll("div")
.data(arr2, function(d) { return d }) // key function used
.enter()
.append("div")
.html(function(d) { return "new: " + d});
The resulting html looks like this (with comment added):
<body>
<div>** First div** </div> <!-- data: 35 -->
<div>** Second div ** </div> <!-- data: 70 -->
<div>24</div> <!-- data: 24 -->
<div>86</div> <!-- data: 86 -->
<div>59</div> <!-- data: 59 -->
<div>new: 7</div> <!-- data: 7 -->
<div>new: 2</div> <!-- data: 2 -->
</body>
The second call to data() used the value returned by the function for the keys. For the selected elements, the function returns a value derived from the data that had already been bound to them by the first call to data(). That is, their key is based on their bound data.
For the second data() call, the keys used for matching look like this.
selected divs (by text) key data elements key
----------------------- --- ------------- ---
** First div ** 35 35 35
** Second div ** 70 7 7
24 24 24 24
86 86 2 2
59 59
The data elements without matching keys are 7 and 2. Those data elements are passed to enter(). So we get two new divs appended to the body.
Okay, so now let's look back at the original post. The OP said that there was no difference between the data() call with a function and without. That's probably because -- as Tristan Reid suggests -- the key function was being used on html elements that had no bound data. When there's no bound data, there will be no matching keys, so all of data elements will get passed to the enter() function.
Working through this example helped illuminate for me the relationships between selections, keys, and bound data. Hopefully it will be helpful to someone else.
The key function explains how to join the data to the elements. The default key function if you don't supply one is to use the index.
To understand all this, consider that a d3 selectAll/data is performing two phases of matching. The first is the selector, e.g. d3.selectAll('div'), which will match all divs. The second is the data join, data([1,2,3]), which is looking for elements with data properties which match the data you pass in. The emphasis is because I think understanding this is fundamental to getting full benefit from d3.
Here's an example (also a fiddle) that demonstrates the difference.
function id (d) { return d; }
d3.select('.no-key').selectAll('div')
.data([1,2,3])
.enter().append('div')
.text(id);
d3.select('.yes-key').selectAll('div')
.data([1,2,3], id)
.enter().append('div')
.text(id);
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
<div class='no-key'>
<div class='a'>already here</div>
</div>
<br/>
<div class='yes-key'>
<div>already here</div>
</div>
I applaud the efforts of the other answer, but this answer doesn't require parsing a console out, it shows the actual difference in functionality.
Why does this difference happen? Here are the gory details:
If you do a d3.selectAll('div') you are selecting all divs. If you then do a .data([1,2,3]), you are joining that data to those divs: but the join doesn't have a key function, so it isn't looking to see if the divs have [1,2,3] as data elements, it's just going to use the first 3 divs that it finds.
If you instead do .data([1,2,3], function(d){return d;}), your key function says to match [1,2,3] against the data in the divs, so unless you have existing divs that have data elements, you won't match any existing divs.
The illustration of all this is in the .enter().append('div'), which of course adds any necessary divs that weren't found in the above matches. That's the bottom line of all this enter().append business: It adds (number of data elements) - (number of existing elements that match the key function)
Hope this helps!
Per request from my SO hero, let me try to address #markthekoala 's desire for a console experimentation that clearly demonstrates the effect of specifying a key function within the .data() operator.
http://bl.ocks.org/migurski/4601038
I actually consider this an even more pointed example than the excellent one I linked to in my comments above! NOTE: the long title of the block has a good explanation on what is happening under the hood, so read it carefully.
You can play with the block's code in this FIDDLE.
Honestly, I find it very hard to improve on the explanations in these two examples, and particularly on the experimentation so cleverly carried out on the second one.

Semantic mediawiki #ask query: Displaying nested properties on the same query

I would like to display in the same query properties of a page which is related to the pages im querying for.
Let's say I would like to query all the pages in the City category, which are located in Germany, and I want to display the title of the page, but also I want to display the surface data of Germany, for example.
Something like this: {{#ask: [[Category:City]] [[location::Germany]] |?mainlabel |?Location.surface }}
I know this wont work, but you can see what I want to achieve.
I'm not sure if there's a way to nest queries directly inside other queries. The normal method of doing it is using a template. So you might define a template (or subpage of the template if this going into a template) called {{tablerow}} that consists of:
<includeonly>
|- valign="top"
| [[{{{1|}}}]]
| {{#show: {{{1|}}} | ?surface }}</includeonly>
The <includeonly> tags are important for reasons I don't really understand, it produces errors sometimes if you leave them out. Then you just run an #ask query with format = template. (You can build the header into the query, but I find it simpler to just put it outside.)
{| class="wikitable smwtable sortable"
|- valign="bottom"
! [[City]]
! [[Surface]]
{{#ask: [[Category:City]] [[location::Germany]]
| format = template
| template = tablerow
| link = none
}}
|}
That will punch each result returned by the query through the template as {{{1}}} and generate a row based on it. If you have other data to return from the main query, additional properties that you ask for will come out as consecutive unnamed parameters (so if you include | ?population, that will go into the template as {{{2}}} and will need to be added to the row structure or it will be dropped).

Flex MenuBar : Stay open on itemclick

I have a MenuBar with objects that have children of type="check". E.g.:
Menu
Menu Item A
/ List item Check 1A
/ List item Check 2A
/ List item Check 3A
Menu Item B
/ List item Check 1B
/ List item Check 2B
My question is as follows: How can i avoid the MenuBar from hiding when the user clicks an item (itemClick event)? I want the user to be able to "check" several items at once, without the menu hiding/closing.
Bonus question: What is the easiest way to reset all the checked items? :-)
Best Regards,
Sebastian
The only way I can advise you is to do monkey patching of the class Menu. I have tried to do it by me, it works as you described in your task.
The main reason to do it is, that the functions we need to redefine use private members of this class. So we can't just override them.
The aim of our mission is to patch the function
function mouseUpHandler(event:MouseEvent):void{...}
At its end you can see the call
hideAllMenus();
We should add a new variable to control whether or not our item is of type "check". So you need to add this line in the beginnig
var isCheck:Boolean = _dataDescriptor.getType(item) == "check";
and this condition at the end
if (!isCheck) hideAllMenus();
Don't forget to do a trick to let your patched class be loaded before the SDK's one.
You can read about it here.
Here is my working example. Menu1 has only check items and Menu2 only normal ones.
The whole project can be found here