SIlverstripe check page name in .ss file - templates

I want to check – in the template .ss file – the page name or title in order to show a different image. I'd like to do something like that:
<% if $SiteConfig.Title == 'video' %>
<img src="themes/blackcandy/images/image.jpg" />
<% else if $SiteConfig.Title == 'bio' %>
<img src="themes/blackcandy/images/image1.jpg" />
...
...
<% end_if %>
The code above of course doesn't work. How do I do it? Is there a more efficient way of doing it?
Thanks a lot.
Mauro

If you want to check it throu the title use $Title (without the SiteConfig controller)?
<% if $Title == 'video' %>
<img src="themes/blackcandy/images/image.jpg" />
<% else if $Title == 'bio' %>
<img src="themes/blackcandy/images/image1.jpg" />
<% end_if %>
If you want to check PageType use ClassName
<% if ClassName = PageType1 %>
<img src="themes/blackcandy/images/image1.jpg" />
<% end_if %>
If you want to check throu Url use UrlSegment (see Clints answer)

It's best to use URLSegment for this.
<% if URLSegment = video %>
<img src="themes/blackcandy/images/video-image.jpg" />
<% else_if URLSegment = bio %>
<img src="themes/blackcandy/images/bio-image.jpg" />
<% end_if %>
Thanks to banal at: http://www.silverstripe.org/themes-2/show/11325

assuming you're on silverstripe 2.4:
<% control SiteConfig %>
<% if Title = video %>
video
<% else_if Title = bio %>
bio
<% end_if %>
<% end_control %>
note not to put quotes around the values (bio instead of 'bio').
i supposed the following to be valid too:
<% if SiteConfig.Title = bio %>
but for some reason it doesn't work - not sure about this.

Related

Rails URL Generation Error but route exists

Making a basic phonebook app and made some changes while figuring out the has_many and belongs_to relationships. I must have broke something because I have no idea why I'm getting this error. When I access my root, I get the following-->
ActionController::UrlGenerationError in ContactsController#index
No route matches {:action=>"show", :controller=>"contacts"} missing required keys: [:id]
The error shows mistakes in lines:
app/views/contacts/index.html.erb:10:in `block in _app_views_contacts_index_html_erb___2771775118522806317_70170309989460'
app/views/contacts/index.html.erb:7:in `_app_views_contacts_index_html_erb___2771775118522806317_70170309989460'
This is my contacts/index.html.erb
<p id="notice"><%= notice %></p>
<% if user_signed_in? %>
<h1>Listing Contacts</h1>
<% #contacts = current_user.contacts %>
<% #contacts.each do |contact| %>
<div class="link row clearfix">
<h2>
<%= link_to contact.name, contact_path %>
</h2>
</div>
<% end %>
<% end %>
<%= link_to "New Contact", new_contact_path %>
<% else %>
<h5> Welcome. Make an account or sign in above! </h5>
<% end %>
This is my config/routes
Rails.application.routes.draw do
resources :controllers
devise_for :users
resources :contacts so
resources :numbers
end
end
end
This is my contacts/show.html.erb
<div class="page-header">
<h1><%= #contact.name %><br> </h1>
</div>
<p>
<strong>Name:</strong>
<%= #contact.name %>
</p>
<p>
<strong>Email:</strong>
<%= #contact.email %>
</p>
<br>
<%= link_to 'Edit', edit_contact_path(#contact) %> |
<%= link_to 'Back', contacts_path %>
The output of my rake routes:
contacts GET /contacts(.:format) contacts#index
POST /contacts(.:format) contacts#create
new_contact GET /contacts/new(.:format) contacts#new
edit_contact GET /contacts/:id/edit(.:format) contacts#edit
contact GET /contacts/:id(.:format) contacts#show
PATCH /contacts/:id(.:format) contacts#update
PUT /contacts/:id(.:format) contacts#update
DELETE /contacts/:id(.:format) contacts#destroy
As you can see, I have a route for contacts#show so that's not the mistake. I'm unclear as to what it could be. Any ideas?
Seems like you're missing :id from the contact_path.
In contacts/index.html.erb, change this:
<%= link_to contact.name, contact_path %>
to this:
<%= link_to contact.name, contact_path(contact.id) %>

Ruby on rails Tutorial Missing partial articles/_form

Hi I have just started using Ruby on rails and have been following the tutorial # http://guides.rubyonrails.org/getting_started.html.
The problem I have got, is with 5.12 Using partials to clean up duplication in views.
I am getting the error ActionView::MissingTemplate in Articles#new.
It is looking for _form.html.erb , which is in the directory /app/views/articles/. So not sure why it cannot find it.
articles controller for new
def new
#article = Article.new
end
articles view for new
<h1>New article</h1>
<%= render 'form' %>
<%= link_to 'Back', articles_path %>
articles view for _form.html.erb
<%= form_for #article do |f| %>
<% if #article.errors.any? %>
<div id="error_explanation">
<h2>
<%= pluralize(#article.errors.count, "error") %> prohibited
this article from being saved:
</h2>
<ul>
<% #article.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<p>
<%= f.label :title %><br>
<%= f.text_field :title %>
</p>
<p>
<%= f.label :text %><br>
<%= f.text_area :text %>
</p>
<p>
<%= f.submit %>
</p>
< % end %>
Any help would be appreciated, need to know how to solve before going on to next part.
Hi I have got it to work by renaming _form.html.erb to_form as the error is looking for /apps/views/_form. I don't know if this is the correct way to solve the problem, as its says in the tutorial to name the file _form.html.erb.
would this cause issues in future doing it this way?
Check the file name, maybe you copied it wrong
I changed the file name to '_form.erb' and it worked. Still puzzled why the full suffix of 'html.erb' didn't work.

How to show spree product variant images only?

Right now I've the following piece of code, which retrieves all images attached to Spree products.
<% #product.images_by_variant.each do |variant_id,images| %>
<ul class="gallery gallery-<%= variant_id %>" data-variant="<%= variant_id %>">
<% images.each do |image| %>
<li>
<%= image_tag(image.attachment.url(:single)) %>
</li>
<% end %>
</ul>
<% end %>
However, there are also images displayed that represent all variants. I want to hide those.
How can I only show variant (different colors for example) for products?
Cheers!
I found the solution. In the products_controller_decorator.rb, I added the following piece of code to the show method:
variants = #product.variants
if variants.blank?
#variants = #product
elsif variants.length > 1
#variants = variants.reject {|variant| variant.is_master? }
end
In the view I rendered the following partial:
<% if #variants == #product %>
<%= render partial: 'product_single/product_gallery', locals: {variant: #product} %>
<% else %>
<% #variants.each do |variant| %>
<%= render partial: 'product_single/product_gallery', locals: {variant: variant} %>
<% end %>
<% end %>

Rails 4.0 link_to overlaying an image with a link

I have a thumbnail image that links to a blog post entry which is accompanied by a text title. The CSS shades the image slightly and when the user hovers over the image, it re-sizes the image slightly. The user can click on the title or on the thumbnail to take them to the entry.
I cannot get the overlay tag here:
to be generated within rails link_to
<%= link_to(:class=>"overlay overlay-primary", id: entry) %>
The image is part of a figure
<figure class="entry-thumbnail">
<!-- to disable lazy loading, remove data-src and data-src-retina -->
<% if entry.cover_photo_link.blank? %>
<%= image_tag "placeholder.gif" %>
<% else %>
<%= image_tag entry.cover_photo_link %>
<% end %>
<!--fallback for no javascript browsers-->
<noscript>
<% if entry.cover_photo_link.blank? %>
<%= image_tag "placeholder.gif" %>
<% else %>
<%= image_tag entry.cover_photo_link %>
<% end %>
</noscript>
</figure>
The full thumbnail codes is in this gist
How do I get the image to link to use link_to render this?
The solution was simple. Putting the a blank "" in link_to did the trick
<%= link_to "", entry, :class => "overlay overlay-primary" %>
renders:
<a class="overlay overlay-primary" href="/entries/5"></a>
This worked well.

Silverstripe - Is it possible to use a variable in an include statement?

I am using the silverstripe templating and I want to loop through the child pages of the current page and dynamically input the template name in the 'include' control depending on that child's page type.
Here is the code I have so far:
<div id="tertiary-content">
<% if $Children %>
<% loop $Children %>
<% include $ClassName %>
<% end_loop %>
<% end_if %>
</div>
(I have ss files in my templates/Includes directory that relate to the $ClassName variable)
Here's the error I get:
Error was: Unknown open block "loop" encountered. Perhaps you missed the closing tag or have mis-spelled it?
I found this article from a silverstripe forum which makes me think it should work:
http://www.silverstripe.org/archive/show/1023
Is it actually possible to have a variable in an include control?
You could write a function in your Page class that loads a ss template based on the current class name. In your Page.php file.
class Page extends SiteTree {
/**
* Returns a template based on the current ClassName
* #return {mixed} template to be rendered
**/
public function getIncludeTemplate(){
return $this->renderWith($this->ClassName);
}
}
and then in your Template
<div id="tertiary-content">
<% if $Children %>
<% loop $Children %>
$IncludeTemplate
<% end_loop %>
<% end_if %>
</div>
You could call renderWith directly from the template, e.g.:
<div id="tertiary-content">
<% if $Children %>
<% loop $Children %>
$renderWith($ClassName)
<% end_loop %>
<% end_if %>
</div>
Did some test and could not get <% include $ClassName %> to work. But you could work around it with something like:
<% if $ClassName = 'SomeClass' %>
<% include SomeClass %>
<% else_if $ClassName = 'SomeOtherClass' %>
<% include SomeOtherClass %>
<% else %>
<% include DefaultClass %>
<% end_if %>
Not as pretty, but does the job.