I am trying to work out the best way to structure this page. It is the 'detail' part of a list-detail setup using Collections. The issue is I am constructing a variable based on data from the collection item, and need to then show that as part of the template. This is what I have so far:
Code
- $selectedVacancy = perch_get('s');
- if($selectedVacancy){
- $vacancy = perch_collection('Vacancies',[
- 'template' => 'vacancies/vacancy_single',
- 'skip-template' => true,
- 'return-html' => true,
- 'filter' => 'slug',
- 'match' => 'eq',
- 'value' => $selectedVacancy
- ]);
- // Checks for when the posting was and sets a variable
- $postDate = $vacancy[0]['post_date'];
- $date1 = date_create($today);
- $date2 = date_create($postDate);
- $diff = date_diff($date1,$date2);
- $days = $diff->format("%a");
- $months = $diff->format("%m");
- if ($months > 0) {
- $posted = 'Posted ' . $months . ' months ago';
- }
- elseif ($days > 0) {
- $posted = 'Posted ' . $days . ' days ago';
- }
- else {
- $posted = 'Posted today';
- }
- PerchSystem::set_var('posted', $posted);
- echo $vacancy['html'];
- }
So in the template vacancies/vacancy_single I want to use the $posted variable (e.g. <perch:content id="posted">), but of course I can't because it is being set after the perch_collection. There is probably a simple solution to this but I can't see it! Thanks for any help.
Mike