(Silverstripe Version:5.4)
Hi,
I am trying to have the template StudentHolder_Cities.ss called and displayed when the action Cities in the controller StudentHolderController.php is in the URL request.
I add the following four documents below:
(1) the code of StudentHolderController.php
(2) 21-06-2025 screenshot 1.png showing where StudentHolder_Cities.ss is placed in order to have it displayed (Two separate locations because of two separate attempts. Once without a namespace, once with a namespace)
(3) 21-06-2025 screenshot 2.png showing that the URL request works.
(4) 21-06-2025 screenshot 3.png showing that StudentHolder_Cities.ss is never called, regardless of where it is placed
StudentHolderController.php
<?php
# --> Attempt number 1: without the namespace
# --> Attempt number 2: with the namespace, targetting:
# \community\themes\simple\templates\Silverstripe\SSBootstrap\StudentHolder_Cities.ss
#namespace Silverstripe\SSBootstrap;
use SilverStripe\ORM\ArrayList;
use SilverStripe\View\ArrayData;
use SilverStripe\ORM\PaginatedList;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Dev\Backtrace;
use SilverStripe\Dev\Debug;
use PageController;
class StudentHolderController extends PageController
{
private static $allowed_actions = [
'Students',
'Cities'
];
private static $url_handlers = [
'students' => 'Students',
'cities/$ID' => 'Cities'
];
protected $studentList;
protected function init()
{
parent::init();
$this->studentList = StudentPage::get()->filter([
'ParentID' => $this->ID,
'Active' => true,
'Verified' => true,
])->sort('Username ASC');
}
public function Students() {
$studentsCitiesArrayList = ArrayList::create();
$citiesArray = [ '1' => 'Aachen', '2' => 'Berlin', '3' => 'Bonn', '4' => 'Bremen',
'5' => 'Cologne', '6' => 'Dresden', '7' => 'Erfurt', '8' =>'Frankfurt',
'9' => 'Freiburg', '10' => 'Halle', '11' => 'Hamburg', '12'=> 'Heidelberg',
'13' => 'Jena', '14' => 'Leipzig', '15' => 'Magdeburg', '16' => 'Munich',
'17' => 'Stuttgart'
];
$studentsArray = array();
foreach ($citiesArray as $k => $c) {
$studentsArray[] = StudentPage::get()->filter(['Active' => 'Yes','Verified' => 'Yes','City' => $c]);
}
$studentsCountArray = array();
foreach ($studentsArray as $s) {
$studentsCountArray[] = $s->Count();
}
$studentsCitiesDataArray = array();
foreach ($citiesArray as $k => $v) {
$studentsCitiesDataArray['cityID'] = (int)$k;
$studentsCitiesDataArray['cityName'] = $v;
$studentsCitiesDataArray['studentsCount'] = $studentsCountArray[$k-1];
}
$studentsCitiesArrayList = new ArrayList;
foreach ($studentsCitiesDataArray as $arr) {
$studentsCitiesArrayList->push($arr);
}
return $studentsCitiesArrayList;
}
public function Cities(HTTPRequest $r)
{
$cityID = (int)$r->param('ID');
$cityName = match($cityID){
1 => "Aachen",
2 => "Berlin",
3 => "Bonn",
4 => "Bremen",
5 => "Cologne",
6 => "Dresden",
7 => "Erfurt",
8 => "Frankfurt",
9 => "Freiburg",
10 => "Halle",
11 => "Hamburg",
12 => "Heidelberg",
13 => "Jena",
14 => "Leipzig",
15 => "Magdeburg",
16 => "Munich",
17 => "Stuttgart",
default => "unknown"
};
if($cityName == "unknown") {
return $this->httpError(404,'The application cannot retrieve this city in the database.');
}
$this->studentList = $this->studentList->filter([
'City' => $cityName
]);
return [ 'SelectedCity' => $cityName ];
}
public function PaginatedStudents($num = 1)
{
return PaginatedList::create(
$this->studentList,
$this->getRequest()
)->setPageLength($num);
}
}
Could you please let me know where StudentHolder_Cities.ss has to be placed so that it can be accessed?
Thanks very much.
K.