Skip to content
ThePixelDeveloper edited this page Sep 14, 2010 · 5 revisions

Twig_View

This is a class for manipulating Twig views as if they were Kohana views. It implements all of the same methods as Kohana’s View class. Since the Twig view system and Kohana’s View system are rather different architecturally there are some implementation details to note:

  • Since Twig views can extend and include other views there is generally no need for multiple Twig_View objects to be instantiated, however it is possible to do so.
  • Although Twig_View::set_global() and Twig_View::bind_global() are implemented, there really isn’t much use for it; Twig has no concept of variable scope (at least between views).
  • Twig takes extensions into account, unlike Kohana’s views (which always end in .php). There are a few notes below in API modifications as well as Additional Methods that show how this is handled.

API Modifications

A few of the standard View methods have been modified slightly to better incorporate the Twig system.

Both factory() and __construct take a third, optional argument

  • It defaults to default.
  • It can either be a string or a Twig_Environment instance.
  • If a string is passed, it will be assumed to be a configuration key that is passed to Kohana_Twig::factory() to create the Twig_Environment instance that is used for rendering the view.
  • If it is an actual Twig_Environment instance, it will be used for rendering the view, however the Twig_View instance no longer knows what you want to use as your default extension. So, if you want to pass filenames without the extension, you will need to call $view->set_extension('html').

__get() does not throw exceptions for unset members

  • I always hated this behavior.

set_filename() does not throw an exception if the file does not exist.

  • However an exception will be thrown if the file can’t be found by Twig when it’s rendered.

Additional methods

A few additional methods are provided, mostly as setters and getters.

get($key [, $default = NULL]);

If the key exists in the local data, the value is returned. If not, the global data is searched. If it does not exist there, either, $default is returned.

set_extension($extension);

Sets the extension to be used when rendering. This allows a default one to be set at some other point in the application execution (for example, if a controller returns JSON and HTML based on the request), and then filenames can be passed without extensions.

set_default($data [, $value = NULL]);
h3. bind_default($data, $value = NULL);

This sets default data that overrides global data, but not instance data. It is only available to the instance. This is really only implemented so that Controller_Twig_Template can implement its context behavior.

If $data is a string, it will be used as the key and $value as the value. Otherwise, if $data is an array, each member will be used for adding data.

bind_default adds the data as a reference. An array cannot be passed for $data.

filename();

Returns the template’s filename (sans extension)

extension();

Returns the template’s extension without the leading period.

path();

Returns the full path of the current template (filename + extension)

as_array();

Returns the final data plus global data merged as an array.

environment();

Returns the environment this view is attached to.

Clone this wiki locally