SilverStripe Templates: Displaying DataObject::Count without repeating - silverstripe

I have a Page, this Page has many, one or no comments.
At one point i'd like to display only the number of comments without iterating through all of them. However due to translation it needs to be distinguished between two Plural Versions of the comment count, so a simple call of $Comment.Count is out. Since the translator component does not seem to have pluralization support whatsoever, i need to do it on the template level.
So far all i got is this:
<% if Comments %>
<% control Comments %>
<span class="comments">
<% if Count == 1 %> ein Kommentar<% else %>$Count Kommentare<% end_if %></span>
<% end_control %>
<% end_if %>
This works but repeats Count-Times. Is there a workaround for this? Im aware of the possibility to create a function in my Page class like
function CommentCount(){
return $this->Comments->Count();
}
but this feels a little bit tedious.

I made a litle Decorator as this kind of thing was annoying me.
It's perhaps a bit heavyweight for such a small thing, but you could easily extend it to provide what you need.
Basically it adds a method to DataObjectSet so you can do:
$Comments.Count $Comments.Plural(SingularWord, PluralWord)
Currently if you don't provide PluralWord it will try to guess a plural using typical english spelling changes.
Code is here:
https://github.com/lingo/silverstripe-bits-and-bobs/blob/master/code/Pluraliser.php
Help and readme are here:
https://github.com/lingo/silverstripe-bits-and-bobs/

From the top of my head - it should work, but I'm not sure if there wasn't a problem with statements like these:
<% if Comments.TotalItems == 1 %>Singular<% else %>Plural (inklusive 0)<% end_if %>
Alternatively you could build something like this (also untested, if it doesn't work, comment and I'll try to fix it):
<% if Comments %>
<% control Comments %>
<% if TotalItems == 1 %>
Singular
<% else %>
Plural
<% end_if %>
<% end_control %>
<% else %>
0
<% end_if %>
However, that's pretty ugly and bloated (besides having to repeat the plural piece for 0). I'd prefer a method like SingleComment(), returning true for 1 entry and false for 0 or more as it's IMHO clearer.

Related

Inline server code in client page [duplicate]

This question already has an answer here:
Asp.net server-side code block explanations
(1 answer)
Closed 8 years ago.
Not a difficult question but i can't seem to find a decent answer to it:
inline server code in client page,
what are the usage/different of these:
<% %>
<%# %>
<%= %>
are there any more?
<% %>
Is used for executing arbitrary blocks of code on the server. Usually some type of control statement is place inside of them.
<%= %>
Writes content out to the response stream, similar to Response.Write().
Making something like (below) possible.
<table>
<%
for (int i = 0; i < n; i++)
%><tr><td><%= i %></td></tr><%
%>
</table>
The <% %> tag is used for code blocks. The tag doesn't output anything in itself. Example:
<% int answer = 42; %>
The <%# %> tag is used for data binding.
The <%= %> tag is used for value output. The tag evaluates the expression and the result is written to the page. Example:
<%= answer %>
There is also the <%: %> tag, which is the same as the <%= %> tag except that the output is HTML encoded.
<%$ %> is used for Resources.

What are these tag names <% and <%=? [duplicate]

This question already has answers here:
ASP.NET "special" tags
(5 answers)
Closed 10 years ago.
I tried searching SO, but I guess it doesn't like the syntax <%= in the query text so if this is a duplicate, I'll delete it.
My question is, what are the actual names of the following tags? What would i call them if describing them to someone else
<%
<%=
and if you know them
<%#
<%#
<% scriptlets
<%= expression
<%# expression
<%# directives
EDITED
Imagine like this
1. Whatever you write within <% %> are placed in some 'process' function
2. Whatever you write inside <%# %> are placed at class level
3. You can just write expressions inside <%= %> imagine something is written to print Response.Write (we also do not write ';' at the end eg. <%= x+5 %>) and <%# %> is same as <%= %> but the difference is the way they function internally
NOTE: This is just for your understanding and not taken from any source.
<% is a "classic-asp" tag which is used to write server-side code. You can name it scriptlets.You can use it in .net platform in c# also.
<%= is not really a tag. It is used to write something.
%# directives is used for directives

ASP.NET / MVC Syntax [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
ASP.NET “special” tags
I am trying to understand an MVC application. It has multiple user controls. In these controls I see syntax like:
<%= ...some text ... %>
I have also seen:
<%: ...some text ... %>
<# ...some text ... %>
<% ...some text ... %>
<%# ...some text ... %>
I can see that it enables me to write code in the control/javascript but I don't fully understand the difference between %, %:, %= and %#.
When are they "executed/evaluated"?
Is there a difference if <%= ... => is in a user control or enclosed in single quotes in a Javascript function?
I am not even sure if my title is correct, I don't get anything when googling it.
So I would be delighted with an explanation, more than happy with a link to documentation and happy with the correct terminology.
Assuming you are using the WebForms view engine in ASP.NET MVC all you need to know are the following :
<%= %> - Output the result of the evaluation of the input to the response. For example <%= "<div>foo</div>" %> outputs <div>foo</div>.
<%: %> - Same as the first one except that it HTML encodes the output. Thus <%: "<div>foo</div>" %> will output <div>foo</div>.
<% %> - Evaluates the expression on the server but doesn't output anything in the response. For example you could declare a variable: <% string foo = "foo bar"; %> that you could use later to output with one of the 2 previous methods
<%# %> - This is only used to define the Page or Control directives of the view. It also allows you to bring namespaces and assemblies into scope for the given view. For example <%# Import Namespace="System.IO" %> will bring the System.IO namespace into scope.
As far as <%# is concerned, this is not used in ASP.NET MVC. It is a concept called data binding and works only in classic WebForms applications.
<%= ...some text ... %>: Contains an expression. The result of that expression will have .ToString() called on it, and the resulting string is output directly.
<%: ...some text ... %>: Contains an expression. If the expression result is an IHtmlString, .ToHtmlString() will be called on it, and the results will be output directly. Otherwise, the result of that expression will have .ToString() called on it, and the resulting string is escaped before being output.
<# ...some text ... %>: Contains a page directive for things like namespaces to be included. This is a compiler directive, and is evaluated when the view is getting compiled (usually the first time the view is invoked).
<% ...some text ... %>: Contains one or more statements. These statements are executed.
<%# ...some text ... %>: This is a data-binding expression in ASP.NET WebForms. I don't think it's valid syntax for an MVC View. However, you keep mentioning "user controls", which are not really part of the MVC paradigm, so you might want to reconsider whether this question is really about MVC.
<%# ...some text ... %> is for data-binding expressions; like when you have something like:
<asp:itemtemplate>
<asp:label Text='<%#Eval("Property")%>' .../>
</asp:itemtemplate>
<%= some text %> is basically a shortcut for Response.Write()
<%# some text %> are for application directives; for example when you need to import a namespace: <%# Import namespace="value" %>
I just noticed Stripling warrior answer which compliments this one...

Web Forms Inline Code Begin Tags

I just wan to to know why these different begin tags exist, if there are still more and if they can be used interchangeably:
<%# Do.Something() %>
<%= Do.Something() %>
...
The tags do different things. For example <%= %> will write the result of the expression within the tags to the output, while <%# %> will use data-binding expressions to reference data from a data source. A fuller explanation can be found at http://weblogs.asp.net/ahmedmoosa/archive/2010/10/06/embedded-code-and-inline-server-tags.aspx

Html Encoded Code Expressions

In ASP.NET 4.0
should I use new syntax <%: expression %> or 2.0 <%= HttpUtility.HtmlEncode(expression) %>
Less keystrokes, less chance to make a mistake, easier to read => use the first option.
That's sort of like asking if you should do this:
<%= DateTime.Now %>
or this
<% Response.Write(DateTime.Now) %>
There's no right answer, but one certainly looks better to me.

Resources