I need to set my body id and class in my layout template using a couple of functions. What I can't figure out is where to place these functions and how to call them. You can't put functions in templates. I tried using the application controller, but can't find the proper syntax to use within the layout template get akelos to recognize them.
CommentAuthorbermi
What about app/helpers/layout_helper.php ?
CommentAuthorpweil
Thanks Bermi. I take it that I have to create a new helper file named layout_helper.php, and then add
class LayoutHelper extends AkActionViewHelper
I did this and added a couple of simple functions to help determine the body id and class, and then called it in my layout template:
echo $layout_helper->body_class();
Obviously I need to do something else, but I don't know what, for I then get these errors:
Notice: Undefined variable: layout_helper in /Users/pweil/Documents/akelos/inventory/tmp/views/app/views/layouts/compiled/inventory.tpl.php on line 33
Fatal error: Call to a member function body_class() on a non-object in /Users/pweil/Documents/akelos/inventory/tmp/views/app/views/layouts/compiled/inventory.tpl.php on line 33
I so look forward to the day when I understand when and how variables and objects propagate through the akelos/MVC system.
CommentAuthorpweil
To be more specific, what I'm looking for is to get both the current controller and the current action, and use these to form the body id and class. E.g.,
I've now added var $app_helpers = 'layout' to my application controller. But the variables that work inside the template for getting the current controller and action ($controller_name and $action, respectively) don't work inside the layout helper. This is what can be so frustrating (if you don't know what you're doing). There is no (obvious) rhyme or reason to this.
CommentAuthorpweil
Ok, I think I've got this solved. thank God for coffee! What really made this difficult was the apparent need to use an underscore(!) to get the current controller. Only studying example functions in text_helper.php helped me to eventually figure this out. Why an underscore? (just curious)
this->_controller->getControllerName();
Summary:
1. created layout_helper.php in app/helpers/
class LayoutHelper extends AkActionViewHelper
2. wrote a simple function to create body id and class from controller and action, where certain actions need to be classified as "admin" actions:
6. Using these body IDs and classes, I can now use existing templates for both admin and public viewing. Anyone logged in as an admin (using http basic -- the admin plugin is overkill for my purposes) will see blocks conditioned by:
<? if (strpos($action, 'admin')!== FALSE) { ?>
[show some admin-specific link or information]
<? } ?>
I'm sure that there is a better way to do this (I would prefer an akelos version of acts_as_authenticated), but this works.