Giter Club home page Giter Club logo

slim-views's Introduction

Slim Views

This repository contains custom View classes for the template frameworks listed below. You can use any of these custom View classes by either requiring the appropriate class in your Slim Framework bootstrap file and initialize your Slim application using an instance of the selected View class or using Composer (the recommended way).

Slim Views only officially support the following views listed below.

  • Smarty
  • Twig

How to Install

using Composer

Install in your project by running the following composer command:

$ php composer require slim/views

Smarty

How to use

<?php
require 'vendor/autoload.php';

$app = new \Slim\Slim(array(
    'view' => new \Slim\Views\Smarty()
));

To use Smarty options do the following:

$view = $app->view();
$view->parserDirectory = dirname(__FILE__) . 'smarty';
$view->parserCompileDirectory = dirname(__FILE__) . '/compiled';
$view->parserCacheDirectory = dirname(__FILE__) . '/cache';

Twig

How to use

<?php
require 'vendor/autoload.php';

$app = new \Slim\Slim(array(
    'view' => new \Slim\Views\Twig()
));

To use Twig options do the following:

$view = $app->view();
$view->parserOptions = array(
    'debug' => true,
    'cache' => dirname(__FILE__) . '/cache'
);

In addition to all of this we also have a few helper functions which are included for both view parsers. In order to start using these you can add them to their respective view parser as stated below:

Twig

$view->parserExtensions = array(
    new \Slim\Views\TwigExtension(),
);

Smarty

$view->parserExtensions = array(
    dirname(__FILE__) . '/vendor/slim/views/Slim/Views/SmartyPlugins',
);

These helpers are listed below.

  • urlFor
  • siteUrl
  • baseUrl
  • currentUrl

urlFor

Twig

Inside your Twig template you would write:

{{ urlFor('hello', {"name": "Josh", "age": "19"}) }}

You can easily pass variables that are objects or arrays by doing:

<a href="{{ urlFor('hello', {"name": person.name, "age": person.age}) }}">Hello {{ name }}</a>

If you need to specify the appname for the getInstance method in the urlFor functions, set it as the third parameter of the function in your template:

<a href="{{ urlFor('hello', {"name": person.name, "age": person.age}, 'admin') }}">Hello {{ name }}</a>

Smarty

Inside your Smarty template you would write:

{urlFor name="hello" options="name.Josh|age.26"}

or with the new array syntax:

{urlFor name="hello" options=["name" => "Josh", "age" => "26"]}

You can easily pass variables that are arrays as normal or using the (.):

<a href="{urlFor name="hello" options="name.{$person.name}|age.{$person.age}"}">Hello {$name}</a>

If you need to specify the appname for the getInstance method in the urlFor functions, set the appname parameter in your function:

<a href="{urlFor name="hello" appname="admin" options="name.{$person.name}|age.{$person.age}"}">Hello {$name}</a>

siteUrl

Twig

Inside your Twig template you would write:

{{ siteUrl('/about/me') }}

Smarty

Inside your Smarty template you would write:

{siteUrl url='/about/me'}

baseUrl

Twig

Inside your Twig template you would write:

{{ baseUrl() }}

Smarty

Inside your Smarty template you would write:

{baseUrl}

currentUrl

Twig

Inside your Twig template you would write:

{{ currentUrl() }}

Smarty

Inside your Smarty template you would write:

{currentUrl}

Authors

Josh Lockhart

Andrew Smith

License

MIT Public License

slim-views's People

Contributors

silentworks avatar nockieboy avatar kachar avatar tassoevan avatar thijsw avatar itsgoingd 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.