Giter Club home page Giter Club logo

view's Introduction

view

A View class for php views. It includes the core features of a conventional view library, but is light weight and uses pure php syntax, no compiling and no need to learn a new syntax.

Installation

Download and unzip in your project directory.

Or install into a composer project using

composer require lydore/view

Usage

<?php

require_once 'path/to/View.php';

// Set directory containing all views
View::setBaseDir('path/to/views/dir');

The view files

All view files should be saved as file-name.view.php in the base view directory e.g homepage.view.php A view can contain regular html

A simple view

<!DOCTYPE html>
<html>
   <head>
      <title>View</title>
   </head>
   
   <body>
      <h1>Hello World</h1>
   </body>
</html>

In addition, views can have sections, vars and can be extended or yielded. The following example shows this functionality.

template.view.php is a master template view which home.view.php and about.view.php extend to use as their layout

template.view.php

<!DOCTYPE html>
<html>
   <head>
      <title><?= View::get('page_title') ?></title>
   </head>
   
   <body>
      <?php View::yield('content') ?>
   </body>
</html>

home.view.php

<?php View::set('page_title', 'View Home') ?>

<?php View::section('content') ?>
<h1>Hello World</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
<?php View::endsection() ?>

<?php View::extend('template') ?>

about.view.php

<?php View::set('page_title', 'About View') ?>

<?php View::section('content') ?>
<h1>About Us</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
<?php View::endsection() ?>

<?php View::extend('template') ?>

Using the view

Now the view has been defined, next is to call the view from our code. To do that, use:

<?php
View::render('home');

Variables can also be passed to the view by passing an associative array to the view method

<?php
$name = 'John Doe';

View::render('home', ['name' => $name]);

// OR use the compact method

View::render('home', compact('name'));

Including views

Views can be included in other views by using the include method in the view

<div>
<?php View::include('side-bar') ?>
</div>

Bind data

You may need a particluar set of data to be available to a view anytime the view is called, this can be done with the bindData method, however the method call needs to be placed such that it runs before the view is requested.

<?php 
View::bindData('home', function (){
// compact method can also be used
$name = 'Jane Doe';
  return [
  'site_name' => 'My site',
  'body' => 'Cotent in my site body',
  'name' => $name
  ];
});

In the above example, the variables $site_name, $body and $name would always be available in the home.view.php file

Sub directories

It is possible to place views in sub directories of the view base directory, for instance if there is a directory layouts in the base directory and it has a view master.view.php, it can be accessed this way

<?php 
View::render('layouts/master');

Escape values

To prevent html injection, values to be displayed in the view can be escaped using the e method

<div><?= View::e($_GET['user']) ?></div>

view's People

Contributors

joshuauyi 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.