[ Index ]

PHP Cross Reference of Akelos Framework

title

Body

[close]

/ -> AkActionView.php (summary)

(no description)

Author: Bermi Ferrer
Copyright: Copyright (c) 2002-2006, Akelos Media, S.L. http://www.akelos.org
License: GNU Lesser General Public License
File Size: 469 lines (20 kb)
Included or required: 3 times
Referenced: 0 times
Includes or requires: 0 files

Defines 1 class

AkActionView:: (28 methods):
  _loadHelpers()
  _registerTemplateHandler()
  AkActionView()
  renderFile()
  render()
  renderTemplate()
  addSharedAttributes()
  pickTemplateExtension()
  delegateTemplateExists()
  fileIsPublic()
  getFullTemplatePath()
  _templateExists()
  _readTemplateFile()
  evaluateAssigns()
  _delegateRender()
  _assignVariablesFromController()
  _javascriptTemplateExists()
  renderPartial()
  renderPartialCollection()
  renderCollectionOfPartials()
  _partialPathPiece()
  _partialPathName()
  _partialCounterName()
  _extractingObject()
  _addObjectToLocalAssigns()
  _addObjectToLocalAssigns_()
  _addGlobalVar()
  _getGlobals()


Class: AkActionView  - X-Ref

Action View templates can be written in two ways. If the template file has a +.tpl+ extension then it uses 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:

<b>Names of all the people</b>
<?php foreach($people as $person) : ?>
Name: <?=$person->name ?><br/>
<?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 ==

_loadHelpers($helper_dir = null)   X-Ref
No description

_registerTemplateHandler($extension, $className)   X-Ref
Register a class that knows how to handle template files with the given
extension. This can be used to implement new template types.
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.


AkActionView($base_path = null, $assigns_for_first_render = array()   X-Ref
No description

renderFile($template_path, $use_full_path = true, $local_assigns = array()   X-Ref
Renders the template present at <tt>template_path</tt>. If <tt>use_full_path</tt> is set to true,
it's relative to the template_root, otherwise it's absolute. The array in <tt>local_assigns</tt>
is made available as local variables.


render($options = array()   X-Ref
Renders the template present at <tt>template_path</tt> (relative to the template_root).
The array in <tt>local_assigns</tt> is made available as local variables.


renderTemplate($____template_extension, $____template, $____file_path = null, $____local_assigns = array()   X-Ref
No description

addSharedAttributes(&$local_assigns)   X-Ref
No description

pickTemplateExtension($template_path)   X-Ref
No description

delegateTemplateExists($template_path)   X-Ref
No description

fileIsPublic($template_path)   X-Ref
Returns true is the file may be rendered implicitly.


getFullTemplatePath($template_path, $extension)   X-Ref
No description

_templateExists($template_path, $extension)   X-Ref
No description

_readTemplateFile($template_path)   X-Ref
This method reads a template file.


evaluateAssigns()   X-Ref
No description

_delegateRender($handler, $template, $local_assigns, $file_path)   X-Ref
No description

_assignVariablesFromController()   X-Ref
No description

_javascriptTemplateExists($template_path)   X-Ref
No description

renderPartial($partial_path, $object, $local_assigns = array()   X-Ref
Partial Views

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.

renderPartialCollection($partial_name, $collection, $partial_spacer_template = null, $local_assigns = array()   X-Ref
No description

renderCollectionOfPartials($partial_name, $collection, $partial_spacer_template = null, $local_assigns = array()   X-Ref
No description

_partialPathPiece($partial_path)   X-Ref
No description

_partialPathName($partial_path)   X-Ref
No description

_partialCounterName($partial_name)   X-Ref
No description

_extractingObject($partial_name, &$deprecated_local_assigns)   X-Ref
No description

_addObjectToLocalAssigns($partial_name, $local_assigns, &$object)   X-Ref
No description

_addObjectToLocalAssigns_($partial_name, &$local_assigns, $object)   X-Ref
No description

_addGlobalVar($var_name, $value, $_retrieve = false)   X-Ref
Variables assigned using this method will act on any controller or action. Use this in conjunction
with your application helpers in order to allow variable passing from inside your views.
This is used for example on the capture helper.


_getGlobals()   X-Ref




Generated: Mon Oct 27 12:43:49 2008 Cross-referenced by PHPXref 0.6