Silverstripe Blog Tags add class for current tag filter - tags

Sorry I'm kinda new to Silverstripe.
Is there a way to add a active/current class to the BlogTagsWidget.ss list items?
Cheers

Assuming that the BlogController is the current controller when BlogTagsWidget.ss is rendered, you may be able to use this method directly to check it in the template, e.g.
<% loop $Tags %>
<% if $CurrentTag && $CurrentTag.ID == $ID %>
Current
<% else %>
Not current
<% end_if %>
<% end_loop %>
If it's not available then you may need to add some extra code to provide the current tag boolean in BlogTagsWidget::getTags().

Related

Filtering #getFilesAtPath results in Docpad

In Docpad, the following code (using a Query-Engine helper and eco) pulls a list of file names from a directory tree and addds their url to an array:
<% images = []; %>
<% for file in #getFilesAtPath({relativeOutDirPath: 'images/'}).toJSON() : %>
<% images.push(file.url) %>
<% end %>
How might I limit the query to a subset of files, say only PNGs?
So like stated in my answer to your other question: What methods can be called on Docpad's Query tools?
Object returned by your query has some additional default metadata you can't see. As you can see here http://docpad.org/docs/meta-data, one of the metadata is "extension". So you can query with condition like:
extension:'png'
So your code might look like (notice findAll part that gives you a possibility to set search condidtions):
<% images = []; %>
<% for file in #getFilesAtPath({relativeOutDirPath: 'images/'}).findAll(extension:'png').toJSON() : %>
<% images.push(file.url) %>
<% end %>
Or if you want to return all files and trigger different actions on different extensions you could:
<% images = []; %>
<% for file in #getFilesAtPath(relativeOutDirPath: 'images/').toJSON() : %>
<% if file.extension is 'png' : %>
<% images.push(file.url) %>
<% end %>
<% end %>

Simple foreach loop in MVC

<% foreach (var car in Model.AvailableCars)
{ %>
<label><%car.Text; %></label>
<% } %>
The above code throws the error
Only assignment, call, increment, decrement, and new object expressions can be used as a statement.
I know I can do it with html helpers, but why won't the above code work?
<label><%car.Text; %></label>
should read
<label><%= car.Text; %></label>
^
or you can use
<label><%: car.Text; %></label>
^
which will automatically HTML.Encode the value for you.
Add a colon to the car.Text tag to write it to the document, such as:
<label><%: car.Text %></label>
Here's a good explanation of <%: versus <%= asp.net mvc tags: <%: %> vs. <%= %>

Silverstripe loop over ALL children of the sitetree

I´m trying to create a sitemap of all children of the sitetree. For some pages ShowInMenus is set to false. But still these pages should be shown in the sitemao. I know you can loop over all children of a page like this (ignoring ShowInMenus):
<% loop AllChildren %>
$Title
<% end_loop %>
Can I do something similar on the top Level of the sitetree?
Thx,
Florian
i do not know any build in loop you can use for this but you can do in php
public function AllPagesWithParentIDZero() {
return SiteTree::get()->filter('ParentID', 0);
}
and use in template
<% loop $AllPagesWithParentIDZero %>
$Title
<% end_loop %>

Silverstripe: LinkingMode with Custom Controller

I have following problem:
Some links that show up in the Menu (the children of "Portfolio") are links to custom controllers. Of course now the LinkingMode is not available for that Links. Thats a image of the Menu:
So the children of Portfolio (Website, Application, etc.) are actually Category-DataObjects, which do not have a SiteTree Representation. The Submenu of Portfolio is created via checking and looping for all found categories in the Database.
The menu creation looks like that:
<ul>
<% loop Menu(1) %>
<li class="$LinkingMode">
[$LinkingMode] $MenuTitle.XML
<% if Children %>
<ul class="secondary">
<% if ClassName == 'ProjectsPage' %>
<% loop $Top.Categories %> <!-- loop all found categories, every found item links to the custom category controller -->
<li class="$LinkingMode">$Name</li>
<% end_loop %>
<% else %>
<% loop Children %>
<li class="$LinkingMode"><span class="text">$MenuTitle.XML</span></li>
<% end_loop %>
<% end_if %>
</ul>
<% end_if %>
</li>
<% end_loop %>
</ul>
Every Category (Website, Mobile) in the Menu links to a custom controller, which looks basically like that:
class Category_Controller extends Page_Controller {
public function show($arguments) {
return $this; //there will be more code to display all projects of a category
}
}
I expect that I have to add some custom code for the Category_Controller which tells the Portfolio Page which linkingmode it has...
Many thx,
Florian
I´ve found good tips here:
http://www.ssbits.com/snippets/2009/extending-linkingmode-to-handle-controller-actions/
http://www.ssbits.com/tutorials/2010/dataobjects-as-pages-part-1-keeping-it-simple/
Thats the Category_Controller.php (a public var CategoryID is set there):
class Category_Controller extends Page_Controller {
public $CategoryID;
public function index($arguments) {
$slug = $arguments->param("Slug");
$category = Category::get()->filter(array('Slug' => $slug))->First();
$this->CategoryID = $category->ID;
}
}
DataObject Category (LinkingMode function checks if the current CategoryID set in the Controller equals the ID of the Category DateObject):
class Category extends DataObject {
public function LinkingMode(){
$categoryID = Controller::curr()->CategoryID;
return ($categoryID == $this->ID) ? 'current' : 'link';
}
}
In the template you can check then the linking mode as usual:
<% loop $Categories %>
<li class="$LinkingMode">$Name</li>
<% end_loop %>
Cheers,
Florian

Hide check box from controller-MVC

How can i hide/show a check box based on a value obtained from controller.
I am using the given below code for check box
<%= Html.CheckBoxFor(m =>m.IncludeCallWithNoAgents) %>
Your view model could contain a property which would be set by the controller action and which should indicate whether this checkbox should be shown or not:
<% if (Model.ShouldShowCheckBox) { %>
<%= Html.CheckBoxFor(m => m.IncludeCallWithNoAgents) %>
<% } %>