rails: multiple OR statements in current_page? - ruby-on-rails-4

Why does this code set li class active for only the spree.account_url and ignores the rest even though when only one of the others are valid?
<li class='<%= "active" if current_page?(spree.account_url || edit_user_newsletter_path || orders_path || nilecard_path) %>'>

this doesn't work the way you think it does.
spree.account_url || edit_user_newsletter_path
edit_user_newsletter_path will only be returned if spree.account_url is nil. It's not nil, so spree.account_url is always returned in preference for anything else.
Better would be...
<li class='<%= "active" if [spree.account_url, edit_user_newsletter_path, orders_path, nilecard_path].map{|p| current_page?(p)}.any? %>'>

Related

Prestashop, Smarty template renders too many buttons

I've got this code in my prestashop template, there is no loop, only conditional, and I get 5 back buttons (elseif section, first li tag), why is it happen?
{if $node.children|#count > 0 && ($smarty.get.controller!='product' && $smarty.get.controller!='category')}
<li class = "li-parent">
<asset class="menu-arrow-left"></asset>
<p><span>{$node.name|escape:'htmlall':'UTF-8'}</span></p>
{elseif $node.children|#count > 0 && ($smarty.get.controller=='product' || $smarty.get.controller=='category')}
<li class="li-back"><asset class="menu-arrow-right"></asset><p class="class="border-bottom-grandiet-small"><span>Back</span></p></li>
<li class = "li-parent">
<p><span>{$node.children[0].name|escape:'htmlall':'UTF-8'}</span></p>
{/if}
I don't see anything in this code that could cause displaying 5 back buttons. I suspect this code is included in some kind of loop and that's why it's displayed 5 times.
You should change the whole above code with:
testonly
and then look at page or page source and check how many testonly texts will appear.
It's also possible if you really use loop that you should use some extra condition. For example instead of:
<li class="li-back"><asset class="menu-arrow-right"></asset><p class="class="border-bottom-grandiet-small"><span>Back</span></p></li>
you should use
{if $node.children|#iteration eq 1}
<li class="li-back"><asset class="menu-arrow-right"></asset><p class="class="border-bottom-grandiet-small"><span>Back</span></p></li>
{/if}
and probably the rest should be more similar to the first condition so instead of:
<li class = "li-parent">
<p><span>{$node.children[0].name|escape:'htmlall':'UTF-8'}</span></p>
you should use:
<li class = "li-parent">
<p><span>{$node.name|escape:'htmlall':'UTF-8'}</span></p>
but it's really hard to say if we don't know what's the data structure and what exactly you want to achieve. If it still doesn't work you should provide more details to your question, explain what you want to achieve, what data you have in your variables and so on.

Expression Engine - Hide Item if No Entry in Category

I am building a site in Expression Engine with a hard-coded navigation. I need to hide one of the nav items until the client makes an entry under a certain category, here's what I have right now:
{exp:channel:entries category_name="name" limit="1"}
{if count > 0}
<li> • Link</li>
{if:else}
(don't show anything)
{/if}
{/exp:channel:entries}
Any thoughts? Thanks!
yeah, exactly. If there is nothing there, it just won't show anything. If you needed to add a placeholder or something, you could use the {if no_results} tag.

JsRender if statement support

Does jsRender supports if statments like below ?
{{ if val > 1 }}
....
{{ /if }}
I couldn't find any examples.
Also I understand that there is a variant of using helper functions.
Yes, see www.jsviews.com/#iftag.
In fact you can write {{if pathOrExpr}} where path can be any combination of 'data paths' and reqular javascript operators etc.
The example shows {{if members && members.length}} and similarly you can do all the expected conditional expressions such as:
address.zip === '88888', or:
foo.count < 3 && foo.total >= bar.total,
etc. etc.

Expression Engine categories channel outputting empty list

I have a problem with outputting categories in to a list.
It seems to create another ul(one for the channel:categories) within the my ul, and also creates empty lists before each list.
I used the exact same code for entries and it worked fine.
Is this a categories problem?
Here is the Code:
<ul>
<li><a {if segment_2 == ""} class="selected" {/if} href="">News & Events</a></li>
{exp:channel:categories
channel="news_events"
disable="pagination|member_data|trackbacks"
dynamic="no"}
<li>{category_name}</li>
{/exp:channel:categories}
<li><a {if segment_2 == "gallery"} class="selected"{/if} href="">Image Gallery</a></li>
Any help would be appreciated!
With regard to the code output, take a look at the style parameter for the channel categories tag: http://ellislab.com/expressionengine/user-guide/modules/channel/categories.html#channel-categories-style. You'll want to change it to style="linear" to use your own markup.
As for the blank output, it's going to be tough to diagnose without seeing your install but try getting rid of dynamic="no".

Dust if condition

I'm having trouble with a dust if condition. I have a partial that points to 2 different dust templates depending on the Country code
{>"receipt/merchantInfo/merchantInfo_{countryCode}"/}
I'm trying to make an if else condition that will figure out if the {countryCode} is US.
Example:
{#if cond="'{countryCode}' == 'US'"}
<p>is country code US</p>
{:else}
<p>is NOT country code US</p>
{/if}
This isn't working. Anyone have an idea where I went wrong with this?
The #if helper has been deprecated because of a potential security hole (it uses eval on the condition). I would recommend using the #eq helper instead.
{#eq key=countryCode value="US"}
<p>is country code US</p>
{:else}
<p>is NOT country code US</p>
{/eq}
If that doesn't work, make sure countryCode is available in your context (you can use {#contextDump/} for that).
I would do this:
{#select key=countryCode }
{#eq value="US"}<p>is country code US</p>{/eq}
{#eq value="UK"}<p>is country code UK</p>{/eq}
{#default}<p>is NOT country code US or UK</p>{/default}
{/select}
Make sure you are using version 1.x or later.
Make sure you have the Helpers loaded. I ran into this the other day. You need it for this to work and it won't error telling you that it isn't.
If you are enamored of #if but don't like the security issues around its use of eval, you can use my alternative #if helper. It provides an attribute test="expr" to specify your if condition. eval is NOT used to evaluate the expression.
Variables in the expression are restricted to dust names and path used to access values from the context. Constants are JavaScript integer, float, hex and string forms ("xx" or 'xx'). Operands can be a "variable", a constant, or a binary or unary expression yielding a value. Relational operators are <, >, <=, >=, ==, !=. Boolean operators are ! (unary), || and &&.. Operator precedence is the same as JavaScript and parentheses are allowed for clarity or for when the precedence is not what you want.
Here is an example:
{#if test="state == 'CA' || state == 'NY'"}
true stuff goes here
{:else}
false stuff goes here
{/if}
Note that it still has code to allow the cond="expr" attribute that uses eval(). This provides a migration path for existing code.
You can install it as an npm module (https://npmjs.org/package/dustmotes-if).
You can also use:
{#key}
Some content
{:else}
Some other content, if key doesn't have a value
{/key}
For example:
Data
{
firstName: "Mickey",
lastName: "Mouse",
website: null,
phone: "1-800-DISNEYWORLD"
}
Dust Template
<p>First Name: {firstName}</p>
<p>Last Name: {lastName}</p>
{#website}
<p>Website: {website}</p>
{:else}
<p>Phone: {phone}</p>
{/website}
Output:
First Name: Mickey
Last Name: Mouse
Phone: 1-800-DISNEYWORLD