Jade template how to add text after tag on the same level - node.js

I am try to get this to work in jade template:
html:
<li><i class="icon-comment"></i> 5</li>
jade:
li:i.icon-comment 5
ofcource 5 now is nested under i tag rather then li. Any way to do this?

In my templates I do it like this:
li
i.icon-comment
| 5

There is many ways like:
Like the one shared with david using |
li
i.icon-comment
| 5
Add it in single line:
li #[i.icon-comment] 5

Related

Semantic MediaWiki: Use template results in #switch

I want to #switch cases within an #ask-template but it looks like #switch ignores the given result value of {{{2}}} and always displays the #default.
Is there a way to accomplish this? Am I missing something?
Thank you very much!
List:
{{#ask: [[Kategorie:Cat1]]
| ?Arg1
| ?Arg2
| ?Arg3
| ?Arg4
| format=template
| template=TableContent
| introtemplate=TableHeader
| outrotemplate=TableFooter
| sort=Name
| link=none
}}
Tablecontent:
<includeonly>
<tr>
<td>[[{{{1}}}]]</td>
<td>
{{#switch: {{{2}}}
| Case1 = [[File:icon1.png|24px|link=]] Case1
| Case2 = [[File:icon2.png|24px|link=]] Case2
| Case3 = [[File:icon3.png|24px|link=]] Case3
| #default = bla
}}
</td>
<td>{{{3}}}</td>
<td>{{#if:{{{4|}}}|{{{4}}}| }}</td>
</tr>
</includeonly>
Single page:
<!-- SMW code -->
<span style="display:none">
[[Kategorie:Cat1]]
[[Kategorie:Cat2]]
[[Arg1::{{PAGENAME}}]]
[[Arg2::Case2]]
[[Arg3::3x2]]
[[Arg4::foo]]
</span>
{{{1}}} is always the page title in SMWs template results format, therefor in your switch you need to use {{{3}}}, see https://www.semantic-mediawiki.org/wiki/Help:Template_format#Using_templates_for_custom_formatting
If you set named args=yes as a parameter of your ask query things might get simpler and clearer.
see:
https://www.semantic-mediawiki.org/wiki/Help:Named_args
Instead of {{{2}}} you'd use {{{?Args2}}} or {{{Args2}}} depending on your SMW Version (see link above).
Also you might want to provide a default e.g. use {{{Args2|}}}. There is also a Sandbox Wiki at https://sandbox.semantic-mediawiki.org/wiki/Main_Page in which you can tryout your approach so that people can follow your example better.

Incorrect number of results found by XPath

Actually, the situation is a little more complex.
I'm trying to get data from this example html:
<li itemprop="itemListElement">
<h4>
one
</h4>
</li>
<li itemprop="itemListElement">
<h4>
two
</h4>
</li>
<li itemprop="itemListElement">
<h4>
three
</h4>
</li>
<li itemprop="itemListElement">
<h4>
four
</h4>
</li>
For now, I'm using Python 3 with urllib and lxml.
For some reason, the following code doesn't work as expected (Please read the comments)
scan = []
example_url = "path/to/html"
page = html.fromstring(urllib.request.urlopen(example_url).read())
# Extracting the li elements from the html
for item in page.xpath("//li[#itemprop='itemListElement']"):
scan.append(item)
# At this point, the list 'scan' length is 4 (Nothing wrong)
for list_item in scan:
# This is supposed to print '1' since there's only one match
# Yet, this actually prints '4' (This is wrong)
print(len(list_item.xpath("//h4/a")))
So as you can see, the first move is to extract the 4 li elements and append them to a list, then scan each li element for a element, but the problem is that each li element in scan is actually all the four elements.
...Or so I thought.
Doing a quick debugging, I found that the scan list contains the four li elements correctly, so I came to one possible conclusion: There's something wrong with the for loop aforementioned above.
for list_item in scan:
# This is supposed to print '1' since there's only one match
# Yet, this actually prints '4' (This is wrong)
print(len(list_item.xpath("//h4/a")))
# Something is wrong here...
The only real problem is that I can't pinpoint the bug. What causes that?
PS: I know, there's an easier way to get the a elements from the list, but this is just an example html, the real one contains many more... things.
In your example, when the XPath starts with //, it will start searching from the root of the document (which is why it was matching all four of the anchor elements). If you want to search relative to the li element, then you would omit the leading slashes:
for item in page.xpath("//li[#itemprop='itemListElement']"):
scan.append(item)
for list_item in scan:
print(len(list_item.xpath("h4/a")))
Of course you can also replace // with .// so that the search is relative as well:
for item in page.xpath("//li[#itemprop='itemListElement']"):
scan.append(item)
for list_item in scan:
print(len(list_item.xpath(".//h4/a")))
Here is a relevant quote taken from the specification:
2.5 Abbreviated Syntax
// is short for /descendant-or-self::node()/. For example, //para is short for /descendant-or-self::node()/child::para and so will select any para element in the document (even a para element that is a document element will be selected by //para since the document element node is a child of the root node); div//para is short for div/descendant-or-self::node()/child::para and so will select all para descendants of div children.
print(len(list_item.xpath(".//h4/a")))
// means /descendant-or-self::node()
it starts with /, so it will search from root node of the document.
use . to point the current context node is list_item, not the whole document

Jade mixin trouble

I'm using jade's mixin and got some trouble:
code:
mixin renderLink(linkName,linkUrl,linkClass,other)
- var active = req.url==linkUrl?'active':''
li(class=[active,linkClass])
a(href=linkUrl) #{linkName}
#{other}
....
.nav-collapse
ul.nav
+renderLink('HOME','/')
+renderLink('CHAT','/chat',null,'span.badge.badge-warning 2')
what I want is:
li
a(href="#")
CHAT
span.badge.badge-warning 2
how to modify #{other} to get what I want?
thanks
---thanks, use this:
mixin renderLink(linkName,linkUrl,linkClass)
- var active = req.url==linkUrl?'active':''
li(class=[active,linkClass])
a(href=linkUrl) #{linkName}
block
and got what I want:
<li class=" ">
消息<span class="badge badge-warning">2</span>
</li>
Well first of all, I'm assuming you want CHAT on the same line as a since you don't want a <chat></chat> element.
It's not documented (in the official docs), but what you want is to use a block. Try this:
mixin renderLink(linkName,linkUrl,linkClass,other)
- var active = req.url==linkUrl?'active':''
li(class=[active,linkClass])
a(href=linkUrl) #{linkName}
if block
block
....
.nav-collapse
ul.nav
+renderLink('HOME','/')
+renderLink('CHAT','/chat')
span.badge.badge-warning 2
I'm not sure if the if block statement is necessary.

Sparkup Syntax for repeating more than just a single element?

Say I have this:
...
<li class='tab'>7</li>
<li class="tab">8</li>
...
...and I'd like to use Sparkup in my editor to add another say 6 tabs...so I run the sparkup command:
li.tab > a[href=#tab2-$]{$}*6
but it comes out all wrong,
<li class="tab">8</li>
<li class="tab">
1
2
3
...
</li>
My first thought was that my syntax should have been:
(li.tab > a[href=#tab2-$]{$})*6
But that did pretty much the same thing...except this time it didn't insert the second number:
<li class="tab">8</li>
<li class="tab">
$
$
$
...
</li>
Now the range problem (starting at 9 instead of 1) is just a minor annoyance, but what if I want it to repeat the li as well as the a tag?
And yes, before you go off about it, I am indeed aware that I could create all of this stuff just using a simple for loop; but that wasn't part of the question now was it?
You are almost there:
li.tab*6 > a[href=#tab2-$]{$}
You want to create 6 <li> so that's where you should put your multiplier.
No need to be defensive.

Jade template engine, rendering HTML as text

I have this piece of code in one of my .jade files:
each item in items
li= item.name + " " + item.inStock + " <a href='/item/"+item.uniqueId+"'>buy now!</a>"
What this renders is:
Of Mice and Men 1000 <a href='/item/1'>buy now!</a>
[...]
As you can see the <a href='/item/1'>buy now!</a> is not rendered as HTML but as plain text. Is there a way to render it as HTML, so that it creates a link?
Thanks!
each item in items
li
| #{item.name} #{item.inStock}
a(href="/item/"+item.uniqueId) buy now!
try the unescaped operator:
li!= ...
it should do the trick.

Resources