Code Monkey home page Code Monkey logo

view-model's Introduction

Kohana-View

Kohana-View_Model is a replacement for the default Kohana v3 class.

Why you should use it

  • Class based views
    • Lighter controllers
    • Takes logic out of your view templates
  • Auto escaping of variable output
    • Automatically escapes all view variables automatically
    • Also lets you obtain "raw" data by prepending your variable with ^ ()

See the examples branch for examples and benchmarking.

This class is not backwards compatible with the stock view class. It completely replaces it.

Setup

Put your view class files in the classes/view/ directory and name them the same as your other classes.

// Note this is 'view' and not 'views'
// application/classes/view/foo/bar.php
class View_Foo_Bar extends View_Model

Create the associated view file (template) in your views directory with the same filename and path as your view class.

// application/views/foo/bar.php

Usage

Create your view template file the same as you would have before, but remember that any logic belongs in the view class. If you want to prevent the automatic variable escaping, prepend the variable with an '!'.

// application/views/foo/bar.php
<p>This view is <?=$adjective?>!</p>
<p>Another possible description is: <?=$random_adjective?></p>
<p>Here are my adjective notes: <?=^$adjective_note?></p>

Your view class will pass on properties and methods that begin with 'var_' to the template.

// application/classes/view/foo/bar.php
class View_Foo_Bar extends View_Model {

	public $adjectives = array('awesome', 'neato', 'cool beans');

	public $var_adjective = 'very nice';

	public function var_random_adjective()
	{
		return array_rand($this->adjectives);
	}

	public function var_adjective_note()
	{
		$note = new Model_Adjective($this->var_adjective);
		return $note->note;
	}
}

And in your controller you could do

$bar = View_Model::factory('foo/bar')->set('var_adjective', 'the best');
echo $bar;

which would render:

This view is the best!

Another possible description is: cool beans

Here are my adjective notes: This is Super!!

view-model's People

Contributors

zombor avatar slacker avatar dyron avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.