Tutorial do framework Kohana: Views


Autor/fonte: Marcelo Araujo
Tags: [ tutorial kohana ]




As views devem estar localizadas em application/views. O nome do arquivo (sem a extensão) torna-se o nome da view. Existe a possibilidade de se criar subdiretórios para armazenar logicamente a estrutura de renderização da aplicação.

Exemplos:

$view = new View('home');
$view = new View('products/list');

Maneiras de carregar uma view:

Novo objeto:

$view = new View('welcome');

Factory:

$view = View::factory('welcome');

Enviando dados para uma view

Veja o controller abaixo:

class Welcome_Controller extends Controller { 
    function index()
    {
        $view = new View('yourview');
        $view->title   = "Welcome to Kohana !";
        $view->heading = "My Heading";
        $view->content = "My content here.";
        $view->render(TRUE);
    }
}

Agora, abra a sua view e adicione as seguintes variáveis:

<html>
    <head>
        <title><?php echo $title;?></title>
    </head>
    <body>
        <h1><?php echo $heading;?></h1>
        <p><?php echo $content;?></p>
    </body>
</html>

Views em Views

Para carregar views em outras views:

$view = new View('template');
$view->header  = new View('header');
$view->content = new View('content');
$view->footer  = new View('footer');

$view->header->title     = 'Title of page';
$view->content->heading  = 'Heading of your page';
$view->footer->copyright = 'Copyright';
$view->render(TRUE);
View: template.php 
<?php echo $header; ?>
<?php echo $content; ?>
<?php echo $footer; ?>

View - header.php:

<html>
  <head>
    <title><?php echo $title; ?></title>
  </head>
View: content.php 
  <body>
  <h1><?php echo $heading; ?></h1>
View: footer.php 
  <?php echo $copyright; ?>
  </body>
</html>
Output: 
<html>
  <head>
    <title>Title of page</title>
  </head>
  <body>
  <h1>Heading of your page</h1>
  Copyright
  </body>
</html>

Escopo de dados

Exemplos

Exemplo 1: Renderizar na instância da view:

$view = new View('sample');
$view->render(TRUE);

Exemplo 2: Renderizar utilizando View::factory:

View::factory('sample')->render(TRUE);

Exemplo completo

Controller - products.php:

$products = array(
    array(
        'name' => 'Product1',
        'quantity' => '3'
    ),
    array(
        'name' => 'Product2',
        'quantity' => '7'
    )
);
 
$view = new View('products/list');
$view->title = 'Products';
$view->set('products', $products);
$view->render(TRUE);

Note o uso do método set() para atribuir arrays nas views.

View - products/list.php:

<html>
<head>
    <title><?= $title ?></title>
</head>
<body>
    <h1><?= $title ?></h1>
    <table>
        <?php
        foreach ($products as $product)
        {
            echo '<tr><td>';
            echo $product['name'];
            echo '</td><td>'
            echo $product['quantity'];
            echo '</td></tr>';
        }
        ?>
    </table>
</body>
</html>

Output:

<html>
<head>
    <title>Products</title>
</head>
<body>
    <h1>Products</h1>
    <table>
        <tr><td>Product1</td><td>3</td></tr>
        <tr><td>Product2</td><td>7</td></tr>
    </table>
</body>
</html>




Rec6 Digg del.icio.us

Enviado por xKuRt em 09/02/2009 às 20:25


Itens relacionados

Tutorial do framework Kohana: Models
Tutorial do framework Kohana: Sistema de arquivos
Tutorial do framework Kohana: Biblioteca Input
Tutorial do framework Kohana: Biblioteca ORM (Mapeamento Objeto-relacional)
Tutorial do framework Kohana: Instalando o Kohana
Tutorial do framework Kohana: Módulo Forge
Tutorial do framework Kohana: Controllers
Tutorial do framework Kohana: Entendendo o funcionamento básico do Kohana
Tutorial do framework Kohana: Métodos especiais
Tutorial do framework Kohana: Biblioteca Database

Listar todos itens relacionados

Avaliação

Esta publicação ainda não foi avaliada!


Avaliar:


A avaliação de publicações é restrita a membros cadastrados e logados no nosso site.



Comentários

Este artigo ainda não foi comentado ou o(s) comentário(s) que foi(ram) enviado(s) a ele ainda não foi(ram) publicado(s).


Envio de comentário:




  

Terça, 09 de Fevereiro de 2010

Top 5 membros

Últimos membros online

Últimos membros cadastrados