Cakephp Classroom image

pankaj
 
To post your Question Join Classroom
 
Lesson Topics's No:-  First|3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11|Last
Lessons:-Views

Creating your own view classes

You may need to create custom view classes to enable new types of data views, or add additional custom
view-rendering logic to your application. Like most components of CakePHP, view classes have a few
conventions:

  • • View class files should be put in App/View. For example: App/View/PdfView.php
  • • View classes should be suffixed with View. For example: PdfView.
  • • When referencing view class names you should omit the View suffix. For example:

$this->viewClass = 'Pdf';.

You’ll also want to extend View to ensure things work correctly:

// In App/View/PdfView.php
App::uses('View', 'View');
class PdfView extends View {
public function render($view = null, $layout = null) {
// Custom logic here.
}
}

Replacingthe render method lets you take full control over how your content is rendered.

View API

View methods are accessible in all view, element and layout files. To call any view method use
$this->method()

Views have a set() method that is analogous to the set() found in Controller objects. Using set()
from your view file will add the variables to the layout and elements that will be rendered later. See
Controller Methods for more information on using set().

In your view file you can do:

$this->set('activeMenuButton', 'posts');

Then, in your layout, the $activeMenuButton variable will be available and contain the value
‘posts’.

get(string $var, $default = null)
Get the value of a viewVar with the name $var.

As of 2.5, you can provide a default value in case the variable is not already set.
Changed in version 2.5: The $default argument was added in 2.5.

Gets the value of the viewVar with the name $var.

Deprecated since version 2.3: Use View::get() instead.

View::getVars()
Gets a list of all the available view variables in the current rendering scope. Returns an array of
variable names.

View::element(string $elementPath, array $data, array $options = array())
Renders an element or view partial. See the section on Elements for more information and examples.

View::uuid(string $object, mixed $url)
Generates a unique non-random DOM ID for an object, based on the object type and URL. This
method is often used by helpers that need to generate unique DOM ID’s for elements such as the
JsHelper:

$uuid = $this->uuid(
'form',
array('controller' => 'posts', 'action' => 'index')
);
// $uuid contains 'form0425fe3bad'

View::addScript(string $name, string $content) Adds content to the internal scripts buffer. This buffer is made available in the layout as $scripts_for_layout. This method is helpful when creating helpers that need to add javascript or css directly to the layout. Keep in mind that scripts added from the layout and elements in the layout will not be added to $scripts_for_layout. This method is most often used from inside
helpers, such as the JsHelper and HtmlHelper Helpers.

View::blocks()
Get the names of all defined blocks as an array.

View::start($name)
Start a capturing block for a view block. See the section on Using view blocks for examples.

View::end()
End the top most open capturing block. See the section on Using view blocks for examples.

View::append($name, $content)
Append into the block with $name. See the section on Using view blocks for examples.

View::prepend($name, $content)
Prepend to the block with $name. See the section on Using view blocks for examples.

View::startIfEmpty($name)
Start a block if it is empty. All content in the block will be captured and discarded if the block is
already defined.

View::assign($name, $content)
Assign the value of a block. This will overwrite any existing content. See the section on Using view
blocks for examples.

View::fetch($name, $default = ‘’)
Fetch the value of a block. If a block is empty or undefined, ‘’ will be returned. See the section on Using view blocks for examples.

View::extend($name)
Extend the current view/element/layout with the named one. See the section on Extending Views for
examples.

property View::$layout
Set the layout the current view will be wrapped in.

property View::$elementCache
The cache configuration used to cache elements. Setting this property will change the default configuration
used to cache elements. This default can be overridden using the ‘cache’ option in the element
method.

property View::$request
An instance of CakeRequest. Use this instance to access information about the current request.

property View::$output
Contains the last rendered content from a view, either the view file, or the layout content.

Deprecated since version 2.1: Use $view->Blocks->get('content'); instead.

property View::$Blocks
An instance of ViewBlock. Used to provide view block functionality in view rendering.

Deprecated since version 2.1: Use the Using view blocks features instead.

More about Views

Themes

You can take advantage of themes, making it easy to switch the look and feel of your page quickly and
easily.

To use themes, specify the theme name in your controller:

class ExampleController extends AppController {
public $theme = 'Example';
}

Changed in version 2.1: Versions previous to 2.1 required setting the $this->viewClass =
'Theme'. 2.1 removes this requirement as the normal View class supports themes
You can also set or change the theme name within an action or within the beforeFilter or
beforeRender callback functions:

$this->theme = 'AnotherExample';

Theme view files need to be within the /app/View/Themed/ folder. Within the themed folder, create
a folder using the same name as your theme name. For example, the above theme would be found in
/app/View/Themed/AnotherExample.

Beyond that, the folder structure within the /app/View/Themed/Example/ folder is exactly the same
as /app/View/.

For example, the view file for an edit action of a Posts controller would reside at
/app/View/Themed/Example/Posts/edit.ctp. Layout files would reside in
/app/View/Themed/Example/Layouts/.

If a view file can’t be found in the theme, CakePHP will try to locate the view file in the /app/View/
folder. This way, you can create master view files and simply override them on a case-by-case basis within
your theme folder.

Theme assets

Themes can contain static assets as well as view files. A theme can include any necessary assets in its webroot
directory. This allows for easy packaging and distribution of themes. While in development, requests
for theme assets will be handled by Dispatcher. To improve performance for production environments,
it’s recommended that you either symlink or copy theme assets into the application’s webroot. See below
for more information.

To use the new theme webroot create directories like:

app/View/Themed/<themeName>/webroot<path_to_file>

in your theme. The Dispatcher will handle finding the correct theme assets in your view paths.
All of CakePHP’s built-in helpers are aware of themes and will create the correct paths automatically. Like
view files, if a file isn’t in the theme folder, it will default to the main webroot folder:

//When in a theme with the name of 'purple_cupcake'
$this->Html->css('main.css');

//creates a path like
/theme/purple_cupcake/css/main.css
//and links to
app/View/Themed/PurpleCupcake/webroot/css/main.css

Increasing performance of plugin and theme assets

It’s a well known fact that serving assets through PHP is guaranteed to be slower than serving those assets
without invoking PHP. And while the core team has taken steps to make plugin and theme asset serving
as fast as possible, there may be situations where more performance is required. In these situations it’s
recommended that you either symlink or copy out plugin/theme assets to directories in app/webroot
with paths matching those used by CakePHP.

  • app/Plugin/DebugKit/webroot/js/my_file.js becomes
  • app/webroot/debug_kit/js/my_file.js
  • app/View/Themed/Navy/webroot/css/navy.css becomes
  • app/webroot/theme/Navy/css/navy.css
 
 
 

pankaj

Skills    Cakephp

Qualifications :-
Location :-,,,
Description:-
Explore
 

  Students (0)