How does one add a conditional inside of a link in jade? - node.js

How does one add a conditional inside of a tag (link/anchor in my case) in jade?
Here's my pseudo code that of course won't work:
a(href="/foo", class="if (current_route[1] == 'foo'){active}") Go to Foo

How about
a(href="/foo", class=(current_route[1] === 'foo')? "active" : "") Go to Foo

Related

How can I emphasize an impex macro if it is part of a string?

How can I emphasize an impex macro if it is part of a string?
We can do something like this:
$prefix=alpha
$contentCatalog=$prefixContentCatalog
... and $contentCatalog will return "alphaContentCatalog".
Can I make the macro more explicit with something like:
$contentCatalog={$prefix}ContentCatalog
... so that I can immediately see that the macro is $prefix? Is there a syntax for this? (NOTE: The curly brace is just an example. This syntax/symbol doesn't exist for this purpose)
Another example: If I have something like below, it becomes confusing:
$prefix=electronics
$contentCatalog=$prefixContentCatalog
$contentCatalogFolderName=$contentCatalogFolder
But it can be easier to understand if it can be written as:
$prefix=electronics
$contentCatalog={$prefix}ContentCatalog
$contentCatalogFolderName={$contentCatalog}Folder
Hhmmm, unfortunately I don't think there is anything for this. I only see some workarounds like special naming for macro variables:
$_prefix_=electronics
$_contentCatalog_=$_prefix_ContentCatalog
$contentCatalogFolderName=$_contentCatalog_Folder
there is an alternate way to customize the micro via injecting property in local.properties and using ConfigPropertyImportProcessor.
UPDATE GenericItem[processor = de.hybris.platform.commerceservices.impex.impl.ConfigPropertyImportProcessor]; pk[unique = true]
$contentCatalog = $config-ly.br.content.catalog
$contentCV = catalogVersion(CatalogVersion.catalog(Catalog.id[default = $contentCatalog]), CatalogVersion.version[default = Staged])[default = $contentCatalog:Staged]
and entries should be added in local.properties.
ly.br.content.catalog=TestContentCatalog
Note:This is useful when we have multi-country.

IDX condition in a getresource tpl

I use getresource like this:
[[!getResources? &tpl=`coffrets_id` &parents=`1115`]]
inside coffrets_id tpl I'd like to be able to do something like this:
[[+idx:is=`1`:then=`show only first getressrouce result`:else=``]]
[[+idx:is=`2`:then=`show only second getressrouce result`:else=``]]
[[+idx:is=`3`:then=`show only third getressrouce result`:else=``]]...
but I have no idea how to show only the result that matches the idx. Is there any solution?
Just use Templating Properties
[[!getResources?
&tpl=`coffrets_id`
&tpl_1=`coffrets_id_1`
&tpl_2=`coffrets_id_2`
&tpl_3=`coffrets_id_3`
&parents=`1115`
]]

Hide a webpart using a query string parameter?

I have two web-parts on a same page template and I would like to hide one one of them using a value coming through my query string parameter.
How can I hide a web-part using a query string parameter in Kentico 8 and above?
I am assuming you know how to reach visibility section of the webpart.
Click on the little arrow icon highlighted.
Let's assume the querystring parameter name is cat and you want to show it if it's value is "Visible"
So you can do it like this
{% if( QueryString.GetValue("cat") = "Visible" {true}else{false} #%}
You can also do it in a reverse way like this
**{% if( QueryString.GetValue("cat") != "Visible" {false}else{true} #%}**
Edit:-
You can use this to check multiple values for a single clause like this
if( QueryString.GetValue("cat") != "Visible" && QueryString.GetValue("cat") != "")
You can also use this to combine multiple queries like I did in my case.
if( QueryString.GetValue("cat") != "" || QueryString.GetValue("Author") != "" || QueryString.GetValue("tagname") != "") {true}else{false} #%}
Of course, you can interchangeably use "||" and "&" by tweaking your logic.
I hope this is enough to handle to all your cases. Let me know if it works.

Jade conditional with select options

How to set the option (of select) to selected to match the current language of the page? Is there a way to inline it and simplify it something like this:
(value="en" #{Locale}==='en' ? ',selected="selected"': '')
I have tried some answers on this site, but they do not seem to work. Thank you.
This is the view:
if(#{Locale} ==='en')
option(value="en", selected="selected") #{English}
option(value="bg") #{Bulgarian}
else if(#{Locale} === 'bg')
option(value="en") #{English}
option(value="bg",selected="selected") #{Bulgarian}
Adapted from this answer, you could create a mix-in that handles the logic for you:
mixin lang-option(code, name)
if (Locale === code)
option(value=code, selected="selected")= name
else
option(value=code)= name
+lang-option('en', English)
+lang-option('bg', Bulgarian)
This solution works if you need to parameterize the 'disabled' attribute as well. Jade will not output attributes that evaluate to false.
//Selects the option when option.value == selectValue
mixin selectOption(option, selectValue)
option(value=option.value, disabled=option.disabled, selected=(option.value==selectValue))= option.label

Better way to ucfirst in a Jade template?

Is there a better way to capitalize the first character of a string in Jade than this?
for list in project.lists
- list.name = list.name.charAt(0).toUpperCase() + list.name.slice(1);
li #{list.name}
Doing this every time I want to capitalize a variable is ugly, is there any way in Jade that I can define a custom function that I have available in every template like:
for list in project.lists
li #{ucfirst(list.name)}
Thanks in advance!
The contents of #{} are executed as standard JS, so you can pass in helper functions for use with things like that. You haven't specified, but assuming you are using Jade along with Express, you can do something like this:
app.locals.ucfirst = function(value){
return value.charAt(0).toUpperCase() + value.slice(1);
};
That will expose a function called ucfirst within the Jade template. You could also pass it in as part of locals every time you render, but if you are using Express it will do it automatically.
If you're willing to resort to CSS, you can create a class that capitalizes the first letter of every word within the target element.
CSS
.caps {
text-transform: capitalize;
}
Jade
div.caps
each foo in ['one', 'two', 'three']
span #{foo}
Resulting HTML
<div class="caps"><span>one</span><span>two</span><span>three</span>
Resulting view
One Two Three
If you are using pug with gulp, this can be helpful:
mixin ucfirst(text)
- text = text.charAt(0).toUpperCase() + text.slice(1);
.
#{text}
Simply call this as any other mixin:
li
+ucfirst(list.name)

Resources