Code Monkey home page Code Monkey logo

kohana-partials's Introduction

Kohana Partials

Partial templates – usually just called “partials” – are another device for breaking the rendering process into more manageable chunks. With a partial, you can move the code for rendering a particular piece of a response to its own file.

Basic Usage

You can render a partial in your views with Partial::factory() like you would normally do with the View class. The big difference is that the filename you specify will be prefixed with an underscore.

For example, Partial::factory('contact/form') will render a partial named contact/_form.php in your application's views directory.

Collections

A lot of the time, we have an array of objects we need to iterate through to display. For example, in a blog, we want to list a few posts. In your view you might be using something like this:

<?php foreach ($posts as $post): ?>
<div>
    <h3><?php echo $post->title; ?></h3>
    <?php echo $post->content; ?>
</div>
<?php endforeach; ?>

Using partials, you can simplify your views a bit by using the collection() method:

<?php echo Partial::factory('posts/post')->collection($posts); ?>

This one-liner will render the partial named posts/_post.php for every item in $posts. Each item is accessible in your partial as a variable named after the partial. In this case, the variable is $post:

<div>
    <h3><?php echo $post->title; ?></h3>
    <?php echo $post->content; ?>
</div>

Installation

Clone the Git repository into your modules directory:

$ git clone git://github.com/gevans/kohana-partials.git modules/partials

Or, clone the repository as a submodule:

$ git submodule add git://github.com/gevans/kohana-partials.git modules/partials

You can now enable the module in your application's bootstrap.php:

<?php
Kohana::modules(array(
    // ...
    'partials' => MODPATH.'partials', // Partial templates
    // ...
));

kohana-partials's People

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.