Reputation: 162
I have a custom class which I store in /app/Lib and I'd like to use the htmlhelper but as the class is not extending anything the reference to $this as in $this->Html->link
gives the error: Call to a member function link() on a non-object
how can I use this helper from within my own class?
which is:
<?php
class Tree {
private $level = 0;
public function show_tree($tree_array) {
$this->level++;
$style = ($this->level==1) ? ' class="sortable"':'';
echo "<ol".$style.">\n";
foreach ($tree_array as $t) {
echo "<li id=\"list_".$t['Category']['id']."\">\n";
echo "<div>".$t['Category']['name'];?>
echo $this->Html->link(__('View'), array('action' => 'view', $t['Category']['id']));
echo $this->Html->link(__('Edit'), array('action' => 'edit', $t['Category']['id']));
echo $this->Form->postLink(__('Delete'), array('action' => 'delete', $t['Category']['id']), null, __('Are you sure you want to delete # %s?', $t['Category']['id']));
echo "</span>\n";
echo "</div>\n";
if (!empty($t['children'])) $this->show_tree($t['children']);
echo "</li>\n";
}
echo "</ol>\n";
$this->level--;
}
}
Upvotes: 0
Views: 453
Reputation: 25698
By looking at your code you clearly want a helper, not a lib.
Extend the html helper or use it within your custom helper, named NestedListHelper for example. This is the right way in the MVC context and will also be the most less code to write.
Take a look at this TreeHelper, it will also generated nested lists based on a tree structure, it might be similar to what you try todo: https://github.com/CakeDC/utils/blob/master/View/Helper/TreeHelper.php
Upvotes: 1