Inserting a variable argument into a Silverstripe template's function? - templates

I'm using a pagination example from balbus design. In the .ss template, there is a line of code:
<% control ProductList.PaginationSummary(5) %>
Is that possible to use a variable instead of hard-coding the value 5? For example:
<% control ProductList.PaginationSummary(PSSize) %>
The variable PSSize is defined in the model and will return the number set in the CMS.

The SS 2.4 templating language is very limited in terms of what it can do.
In this specific case, you could try working it out in the controller - try adjusting the $resultSet within ProductListPage_Controller::ProductList to preprocess the pagination summary to desired context size, so you can access it later from the template.
Try something like that:
$resultSet->AdjustedPaginationSummary = $resultSet->PaginationSummary($this->productsPerPage);
return $resultSet;
And then in the template you should be able to do:
<% control ProductList.AdjustedPaginationSummary %>

Related

#meditations.count works within folder but not in partial used elsewhere

views/meditations/_count.html.erb
<p><small><%= #meditations.count %></small></p>
when I try using that on the home page like so:
<%= render partial: "/meditations/count" %>
I get the error:
undefined method `count' for nil:NilClass
I tried to change the call to:
<%= render partial: "/meditations/count", object: #meditations %>
I finally got what I wanted but I'm thinking I violated some kind of law by doing this on the home page:
<p><%= Meditations.count %> </p>
What should I have done to get the partial working?
I get the error:
undefined method `count' for nil:NilClass
Any time you see undefined ____ for nil class it is b/c you are calling blah on something that you think has value, but is nil. In other words, #mediatations is coming in as nil.
So check your controller. In the method that matches this page, make sure you have something like #meditations = Meditation.all.
Lastly, yes - Meditations.count is a call to the db from the view. Very bad practice. Basically breaking MVC.

Getting same style in another page when click to login

I am having a login page for user generated by devise gem.where I removed the registerable from model for only login not for signup.I have also generated an admin which gives default login credentials for user in seed.rb file.I have done some css work in login page. next I have generated an employee page for further process for users.
Here my doubt is the styling part what I have done in login page is also coming in employee page. I dont want that so I wrote some condition in application.html.erb
<% unless user_signed_in? %>
<%= render 'layouts/heading' %>
<% end %>
The method is defined in application.controller.rb
def user_signed_in? %>
render :layouts => 'application'
end
I tried a lot by changing conditions but it is still displaying or showing errors.
You said that you're using "device gem", but since you're talking about authentication, I assume you mean "devise"?
The user_signed_in? method is a helper which should be provided automatically by devise, and you should be able to use it in your views. You shouldn't need to define it in the application controller.
You probably want to make sure you do "before_filter :authenticate_user!" in the relevant controller (you can use the ":only" option if you only want this to apply for certain methods).
It's difficult to give any more information because you haven't said what errors you are getting. I assume you're getting some kind of syntax error on the definition of the "user_signed_in?" method, since you have an invalid "%>"on the end of the line, which I guess you copied-and-pasted from an ERB file.

SilverStripe Using Field Values in Template

all! So I'm brand-spankin' new to SilverStripe, and while I've had success in modifying the 'Gallery' module to include an option to set the thumbnail size, I'm having issues using the thumbnail width/height values in my template. The code below does not show any of the gallery images (if I hardcode a width and height, such as 250,250 they display)-- but after the loop where I display the $ThumbWidth and $ThumbHeight values, those are displaying just fine...so I know those values are available to the template.
<% loop OrderedImages %>
<a class="fancybox" data-fancybox-group="gallery" href="$Filename" title="$Caption">
$SetSize($ThumbWidth, $ThumbHeight)
</a>
<% end_loop %>
Width: $ThumbWidth
Height: $ThumbHeight
So apparently, the $SetSize function is not liking the values I'm passing to it. Can someone shed some light on where my mistake is?
Thanks!
Bryan
From the SilverStripe forum:
Those variables are not set on the OrderedImage item which is the context of the loop. Use $Top.ThumbWidth and $Top.ThumbHeight as parameters.

How do I use Ruby refinements in Rails views?

I have a Rails 4 project using Ruby 2.0. I've defined some refinements. Putting
<% using MyRefinements %>
at the top of the view files causes the error "undefined method 'using'".
When I add:
using MyRefinements
At the top of my controller (above the class declaration), I can successfully use the refinement in the controller, but I get an 'undefined method' error if I try to use it in the view.
Thanks!
Unfortunately, this does not seem possible. For posterity, I'm documenting the things I tried that didn't work:
In the view:
<% using MyRefinements %>
In the controller (each tried separately):
using MyRefinements
helper :MyRefinements
helper_method :MyRefinements
helper { using MyRefinements }
In the helper:
using MyRefinements
Note that refinements become available in the controller and in the helper, but never in the view.
Too bad.
By using 'using' you can import class refinements from module into the current class or module definition.
You can not include it in view file by 'using'.
if you want to use it in view you can do following in your controller(I have not tested it):
using MyRefinements
helper :MyRefinements OR helper_method :MyRefinements

Django template blocks: how can i reuse them and how do i pass HTML into them?

Being a frontend dev familiar with Ruby, i'm trying to learn Django templating system.
It appears to be an inside-out version of what i'm used to. I struggle to comprehend its reverse ideology: instead of declaring reusable blocks and including them where necessary, in Django you mark some parts of your template as overridable.
Here are two things that i don't understand about this.
With Ruby's Padrino, i would declare a partial (a reusable snippet of templated HTML) and then include it in multiple places. Wherever i call it, it would output its HTML.
According to Django's templating documentation, each block can be used on a page only once: Finally, note that you can’t define multiple block tags with the same name in the same template.
Another feature of Padrino that i find extermely useful is that partials can accept HTML and output (yield) it in a certain place inside them. Below are a couple examples, one for Padrino and one for Jade.
Please note that partails HTML not as a string (awkwardly passed via an argument) but in a template language via nesting.
Padrino (Ruby) example
With Padrino i can pass HTML template code into partials:
_container.erb
<div class="container <%= myclass %>">
<div class="container-inner">
<%= yield %>
</div>
</div>
layout.erb
<%= partial 'container', locals: { myclass: 'container--header' } do %>
<h1><%= Sitename %></h1>
<p>Welcome to my humble place</p>
<% end %>
Resulting HTML
<div class="container container--header">
<div class="container-inner">
<h1>Sitename</h1>
<p>Welcome to my humble place</p>
</div>
</div>
Jade example
In Jade, partials are called mixins and are processed directly by the template engine rather than the backend framework:
Jade source
mixin article(title)
.article
.article-wrapper
h1= title
if block
block
else
p No content provided
+article('Hello world')
p This is my
p Amazing article
Resulting HTML
<div class="article">
<div class="article-wrapper">
<h1>Hello world</h1>
<p>This is my</p>
<p>Amazing article</p>
</div>
</div>
Is it possible with Django?
Questions:
How do i reuse a block multiple times in Django? I would like to declare a snippet of template code and include it in multiple places on the page.
How do i pass HTML (template code) into a block? I would like to reuse it with different content.
The use case that i'm trying to cover is a reusable partial/mixin/block that would serve as a container wrapper for each section of the site.
Note that with Padrino, i can even make the partial in such a way that it will let me choose which wrapper tag (div, nav, header...) should be used for each instance of the partial, by passing an argument when including the partial:
<% partial 'container', myclass: 'container--header', tag: 'nav' %>
I wonder how to do that with Django.
In your answer, please comment on whether it is possible with both a) basic Django functionality; b) some Django extensions.
Thank you.
I’m not familiar with Padrino, so I’m not 100% sure I understand what you’re looking for.
However, Django template blocks definitely aren’t the equivalent of Padrino’s partials. From your description, I think the equivalent would be custom template tags.
In outline, to create a custom template tag, you:
Create a templatetags module within your Django app, with a file in it to contain the Python code for the tags e.g.
yourapp/
models.py
templatetags/
__init__.py
mytemplatetags.py
Within that file, create a variable called register that’s an instance of django.template.Library:
# mytemplatetags.py
from django import template
register = template.Library()
Within that file, write a function for each custom tag that you want.
For a tag that includes a template snippet, you’d want to write an inclusion tag.
Inclusion tags can take arguments, which could include HTML (but only as a string).