AkObject | --AkActionView
Located in File: /AkActionView.php
= PHP
You trigger PHP by using embeddings such as <? ?>, <?php ?> and <?= ?>. The difference is whether you want output or not. Consider the following loop for names:
Names of all the people <?php foreach($people as $person) : ?> Name: <?=$person->name ?>
<?php endforeach ?>
== Using sub templates
Using sub templates allows you to sidestep tedious replication and extract common display structures in shared templates. The classic example is the use of a header and footer (even though the Action Pack-way would be to use Layouts):
<?= $controller->render("shared/header") ?> Something really specific and terrific <?= $controller->render("shared/footer") ?>
As you see, we use the output embeddings for the render methods. The render call itself will just return a string holding the result of the rendering. The output embedding writes it to the current template.
But you don't have to restrict yourself to static includes. Templates can share variables amongst themselves by using instance variables defined using the regular embedding tags. Like this:
<?php $shared->page_title = "A Wonderful Hello" ?> <?= $controller->render("shared/header") ?>
Now the header can pick up on the $page_title variable and use it for outputting a title tag:
<title><?= $page_title ?></title>
== Passing local variables to sub templates
You can pass local variables to sub templates by using an array with the variable names as keys and the objects as values:
<?= $controller->render("shared/header", array('headline'=>'Welcome','person'=> $person )) ?>
These can now be accessed in shared/header with:
Headline: <?= $headline ?> First name: <?= $person->first_name ?>
== JavaScriptGenerator ==
Fully implement Javascript Generators
JavaScriptGenerator templates end in +.js.tpl+. Unlike conventional templates which are used to render the results of an action, these templates generate instructions on how to modify an already rendered page. This makes it easy to modify multiple elements on your page in one declarative Ajax response. Actions with these templates are called in the background with Ajax and make updates to the page where the request originated from.
An instance of the JavaScriptGenerator object named +page+ is automatically made available to your template, which is implicitly wrapped in an AkActionView/Helpers/PrototypeHelper::update_page method.
When an .js.tpl action is called with +linkToRemote+, the generated JavaScript is automatically evaluated. Example:
linkToRemote(array('url' => array('action' => 'delete')));
The subsequently rendered +delete.js.tpl+ might look like:
<% replace_html 'sidebar', :partial => 'sidebar' %> <% remove "person-#{person.id}" %> <% visual_effect :highlight, 'user-list' %>
This refreshes the sidebar, removes a person element and highlights the user list.
See the AkActionView/Helpers/PrototypeHelper/JavaScriptGenerator documentation for more details.
Static Method _addGlobalVar (line 447)
This is used for example on the capture helper.
Constructor AkActionView (line 135)
Method addSharedAttributes (line 216)
Method delegateTemplateExists (line 235)
Method fileIsPublic (line 248)
Method getFullTemplatePath (line 253)
Method pickTemplateExtension (line 221)
Method render (line 171)
The array in <tt>local_assigns</tt> is made available as local variables.
Method renderCollectionOfPartials (line 386)
Method renderFile (line 149)
Method renderPartial (line 346)
There's also a convenience method for rendering sub templates within the current controller that depends on a single object (we call this kind of sub templates for partials). It relies on the fact that partials should follow the naming convention of being prefixed with an underscore -- as to separate them from regular templates that could be rendered on their own.
In a template for AdvertiserController::account:
<?= $controller->render(array('partial' => 'account')); ?>
This would render "advertiser/_account.tpl" and pass the instance variable $controller->account in as a local variable $account to the template for display.
In another template for Advertiser::buy, we could have:
<?= $controller->render(array('partial' =>'account','locals'=>array('account'=>$buyer))); ?>
<?php foreach($advertisements as $ad) : ?> <?= $controller->render(array('partial'=>'ad','locals'=>array('ad'=>$ad))); ?> <?php endforeach; ?>
This would first render "advertiser/_account.tpl" with $buyer passed in as the local variable $account, then render "advertiser/_ad.tpl" and pass the local variable $ad to the template for display.
== Rendering a collection of partials
The example of partial use describes a familiar pattern where a template needs to iterate over an array and render a sub template for each of the elements. This pattern has been implemented as a single method that accepts an array and renders a partial by the same name as the elements contained within. So the three-lined example in "Using partials" can be rewritten with a single line:
<?= $controller->render(array('partial'=>'ad','collection'=>(array)$advertisements)); ?>
This will render "advertiser/_ad.tpl" and pass the local variable +ad+ to the template for display. An iteration counter will automatically be made available to the template with a name of the form +partial_name_counter+. In the case of the example above, the template would be fed +ad_counter+.
== Rendering shared partials
Two controllers can share a set of partials and render them like this:
<?= $controller->render(array('partial'=>'advertiser/ad', 'locals' => array('ad' => $advertisement ))); ?>
This will render the partial "advertiser/_ad.tpl" regardless of which controller this is being called from.
Method renderPartialCollection (line 357)
Method renderTemplate (line 195)
Method _addObjectToLocalAssigns (line 425)
Method _addObjectToLocalAssigns_ (line 430)
Method _delegateRender (line 282)
Method _extractingObject (line 416)
Method _javascriptTemplateExists (line 295)
Method _loadHelpers (line 101)
Method _partialCounterName (line 411)
Method _partialPathName (line 406)
Method _partialPathPiece (line 392)
Method _readTemplateFile (line 269)
Method _registerTemplateHandler (line 130)
The constructor for the class must take the AkActionView instance as a parameter, and the class must implement a "render" method that takes the contents of the template to render as well as the array of local assigns available to the template. The "render" method ought to return the rendered template as a string.
Method _templateExists (line 259)
AkObject::AkObject() - A hack to support __construct() on PHP 4
AkObject::__construct() - Class constructor, overriden in descendant classes
AkObject::freeMemory() - Unsets circular reference children that are not freed from memory when calling unset() or when the parent object is garbage collected.
AkObject::log() -
AkObject::toString() - Object-to-string conversion
AkObject::__clone() - Clone class (Zend Engine 2 compatibility trick)
AkObject::__destruct() - Class destructor, overriden in descendant classes
AkObject::__toString() -