FriendsOfCake/bootstrap-ui

Name: bootstrap-ui

Owner: Friends Of Cake

Description: CakePHP3: Transparently use Bootstrap 3

Created: 2015-01-01 22:44:43.0

Updated: 2018-01-19 17:34:26.0

Pushed: 2018-01-20 16:31:06.0

Homepage:

Size: 367

Language: PHP

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

Bootstrap UI

Build Status Coverage Status Total Downloads License

Transparently use Bootstrap 3 with CakePHP 3.

Requirements
What's included?
Installing Using Composer

cd to the root of your app folder (where the composer.json file is) and run the following command:

oser require friendsofcake/bootstrap-ui

Then load the plugin by adding the following to your app's config/boostrap.php:

e\Core\Plugin::load('BootstrapUI');

or using CakePHP's console:

n/cake plugin load BootstrapUI
Usage

You will need to modify your src/View/AppView class to either extend BootstrapUI\View\UIView or use the trait BootStrapUI\View\UIViewTrait.

AppView Setup

For a quick setup, just make your AppView class extend BootstrapUI\View\UIView. The base class will handle the initializing and loading of the BootstrapUI layout.ctp for your app.

The src\View\AppView.php will look something like the following:

space App\View;

BootstrapUI\View\UIView;

s AppView extends UIView


/**
 * Initialization hook method.
 */
public function initialize()
{
    //Don't forget to call the parent::initialize()
    parent::initialize();
}

AppView Setup Using UIViewTrait

If you're adding BootstrapUI to an existing application. It might be easier to use the trait, as it gives you more control over the loading of the layout.

space App\View;

BootstrapUI\View\UIViewTrait;
Cake\View\View;

s AppView extends View

use UIViewTrait;

/**
 * Initialization hook method.
 */
public function initialize()
{
    //render the initializeUI method from the UIViewTrait
    $this->initializeUI();
}

BootstrapUI Layout

BootstrapUI comes with its own layout.ctp file and examples taken from the Bootstrap framework.

When no layout for the view is defined the BootstrapUI\View\UIViewTrait will load its own layout.ctp file. You can override this behavior in two ways.

Loading the Bootstrap framework

If you wish to use your own layout template, just make sure to include:

n the <head>
 $this->Html->css('path/to/bootstrap.css');
 $this->Html->script(['path/to/jquery.js', 'path/to/bootstrap.js']);

When using the BootstrapUI layout (or a copy of it), extra layout types (directly taken from the Bootstrap examples). You just need to copy them to your application's layouts directory:

R vendor/friendsofcake/bootstrap-ui/src/Template/Layout/examples src/Template/Layout/TwitterBootstrap

You can then simply extend them in your views like so:

s->extend('../Layout/TwitterBootstrap/dashboard');

Available types are:

NOTE: Remember to set the stylesheets in the layouts you copy.

Installing Bootstrap via Bower

A quick way of getting the Bootstrap assets installed is using bower. Assuming you are in ROOT:

r install bootstrap
r -p webroot/css/bootstrap webroot/js/bootstrap webroot/js/jquery webroot/css/fonts
ower_components/bootstrap/dist/css/* webroot/css/bootstrap/.
ower_components/bootstrap/dist/js/* webroot/js/bootstrap/.
ower_components/jquery/dist/* webroot/js/jquery/.
ower_components/bootstrap/dist/fonts/* webroot/css/fonts/.
 /bower_components >> .gitignore
add .gitignore \
r.json \
oot/css/bootstrap \
oot/js/bootstrap \
oot/js/jquery
Console Bake

For those of you who want even more automation, some bake templates have been included. Use them like so:

n/cake bake.bake [subcommand] -t BootstrapUI
Helper Usage

At the core of BootstrapUI is a collection of enhancements for CakePHP core helpers. These helpers replace the HTML templates used to render elements for the views. This allows you to create forms and components that use the Bootstrap styles.

The current list of enhanced helpers are:

When the BootstrapUI\View\UIViewTrait is initialized it loads the above helpers with the same aliases as the CakePHP core helpers. That means that when you use $this->Form->create() in your views. The helper being used is from the BootstrapUI plugin.

Basic Form
 $this->Form->create($article);
 $this->Form->control('title');
 $this->Form->control('published', ['type' => 'checkbox']);

will render this HTML:

m method="post" accept-charset="utf-8" role="form" action="/articles/add">
<div style="display:none;"><input type="hidden" name="_method" value="POST"></div>
<div class="form-group">
    <label class="control-label" for="title">Title</label>
    <input type="text" name="title" required="required" id="title" class="form-control">
</div>
<div class="form-group">
    <input type="hidden" name="published" value="0">
    <label for="published">
        <input type="checkbox" name="published" value="1" id="published" class="form-control">
        Published
    </label>
</div>
Horizontal Form
 $this->Form->create($article, ['align' => [
'sm' => [
    'left' => 6,
    'middle' => 6,
    'right' => 12
],
'md' => [
    'left' => 4,
    'middle' => 4,
    'right' => 4
]

 $this->Form->control('title');
 $this->Form->control('published', ['type' => 'checkbox']);

will render this HTML:

m method="post" accept-charset="utf-8" role="form" class="form-horizontal" action="/articles/add">
<div style="display:none;"><input type="hidden" name="_method" value="POST"></div>
<div class="form-group">
    <label class="control-label col-sm-6 col-md-4" for="title">Title</label>
    <div class="col-sm-6 col-md-4">
        <input type="text" name="title" required="required" id="title" class="form-control">
    </div>
</div>
<div class="form-group">
    <div class="col-sm-offset-6 col-sm-6 col-md-offset-4 col-md-4">
        <input type="hidden" name="published" value="0">
        <label for="published">
            <input type="checkbox" name="published" value="1" id="published" class="form-control">
            Published
        </label>
    </div>
</div>
Configuration

You can configure each of the helpers by passing in extra parameters through the AppView.php.

Here is an example of changing the prev and next labels for the PaginatorHelper.

s->loadHelper(
'Paginator',
[
    'className' => 'BootstrapUI.Paginator',
    'labels' => [
        'prev' => 'previous',
        'next' => 'next',
    ]
]

To style auth flash messages properly set the flash key in AuthComponent config as shown:

    $this->loadComponent('Auth', [
        'flash' => [
            'element' => 'error',
            'key' => 'auth'
        ],
        ...

NOTE: Check tests for more examples.

Testing

You can run the tests for BootstrapUI by doing the following:

$ composer install
$ ./vendor/bin/phpunit
Patches & Features

To ensure your PRs are considered for upstream, you MUST follow the CakePHP coding standards. A pre-commit hook has been included to automatically run the code sniffs for you. From your project's root directory:

/contrib/pre-commit .git/hooks/
d 755 .git/hooks/pre-commit
Bugs & Feedback

http://github.com/friendsofcake/bootstrap-ui/issues

License

Copyright (c) 2015, Jad Bitar and licensed under The MIT License.


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.