Ruby on Rails liquid theme detect current page and either display banner or not

185 views Asked by At

In the "view" only... I don't have access to the controller or anything else.

I'm trying to figure out how to detect the page and display a banner or not display the banner depending on the page.
Nothing is passed down. What's needed is IF you're on this page > display the banner. ELSE - You're on this page - do not display banner.

<div class="thebnr"><img src="{{ 'thebnr.png' | asset_url }}" alt="{{ the.name }}" itemprop="bnr"></div>

I've searched and search but only see things like currentpage? ... not sure how to get this to work.

2

There are 2 answers

3
Itay Grudev On

You can declare a helper method in your:

# app/helpers/application_helper.rb

module ApplicationHelper

  def page?(page, returnval = true)
    controller, action = page.split('#')
    return returnval if params[:controller] == controller and ( action.nil? or action. params[:action] == action)
    nil
  end

end

Then in your view, you can use it like this:

<% if page?('controller#action') %>
...
<% end %>

returnval is a neat argument which you can use for modifying the return value of the page? method if the action matches the one specified. Otherwise the method returns nil and nil == false so you can safely use it in an if.

An example of using the returnval argument is:

<li class="<%= page?('controller#action', 'active') %>">
3
Jad On

Here's the solution -

{% if page.url == "/pages/about" %}
   <div class="thebnr"><img src="{{ 'thebnr.png' | asset_url }}" alt="{{ the.name }}" itemprop="bnr"></div>
{% else %}
   other thing
{% endif %}