Skip to content

Instantly share code, notes, and snippets.

Created July 17, 2010 09:13
Show Gist options
  • Save anonymous/479392 to your computer and use it in GitHub Desktop.
Save anonymous/479392 to your computer and use it in GitHub Desktop.
<?php
require_once('Smarty/libs/Smarty.class.php');
$Smarty = new Smarty();
$Smarty->template_dir = '.';
class Note {
protected $id;
protected $text;
protected $place_id;
public function __construct($id, $text, $place_id = null) {
$this->id = $id;
$this->text = $text;
$this->place_id = $place_id;
}
public function get_id() {
return $this->id;
}
public function get_text() {
return $this->text;
}
public function get_place_id() {
return $this->place_id;
}
}
class Place {
protected $id;
protected $name;
public function __construct($id, $name) {
$this->id = $id;
$this->name = $name;
}
public function get_id() {
return $this->id;
}
public function get_name() {
return $this->name;
}
}
$places = array(
new Place(1, 'Washington'),
new Place(2, 'Boston'),
new Place(3, 'Iowa') // Nothing ever happens in Iowa, so no notes.
);
$notes = array(
new Note(1, 'General Note'),
new Note(2, 'Another general note'),
new Note(3, 'Washington note', 1),
new Note(4, 'Washington note 2', 1),
new Note(5, 'Boston note', 2),
new Note(6, 'I love the Boston area', 2)
);
$placesNotes = array();
$generalNotes = array();
array_filter($notes, function($note) {
global $placesNotes, $generalNotes;
if($note->get_place_id() == null) {
$generalNotes[] = $note;
} else {
$placesNotes[$note->get_place_id()][] = $note;
}
});
$Smarty->assign('general_notes', $generalNotes);
$Smarty->assign('places_notes', $placesNotes);
$Smarty->assign('places', $places);
$Smarty->display('notesByPlace.tpl');
// EOF
<html>
<head><title>Test</title>
<style>
{literal}
.hide {color: #CCC;}
{/literal}
</style>
</head>
<body>
{if count($general_notes)}
<ul id="list-general">
<h4>General</h4>
{foreach from=$general_notes item=note}
<li id="note-{$note->get_id()}">{$note->get_text()}</li>
{/foreach}
</ul>
{else}
<ul id="list-general" class="hide">
<h4>General</h4>
</ul>
{/if}
{foreach from=$places item=place}
{assign var=curr_place_id value=$place->get_id()}
{if isset($places_notes.$curr_place_id)}
<ul id="list-{$curr_place_id}">
<h4>{$place->get_name()}</h4>
{foreach from=$places_notes.$curr_place_id item=note}
<li id="note-{$note->get_id()}">{$note->get_text()}</li>
{/foreach}
</ul>
{else}
<ul id="list-{$curr_place_id}" class="hide">
<h4>{$place->get_name()}</h4>
</ul>
{/if}
{/foreach}
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment