Giter VIP home page Giter VIP logo

twitter-bootstrap-helper's Introduction

Twitter Bootstrap CakePHP Helper

CakePHP helper for rendering bootstrap appropriate markup. Uses the Twitter Bootstrap 2.0.

Requirements

Installation

Check out the the repo in the plugins directory

git clone git://github.com/loadsys/twitter-bootstrap-helper TwitterBootstrap

Add the plugin inclusion in the project bootstrap file

echo "CakePlugin::load('TwitterBootstrap');" >> Config/bootstrap.php

Then add helper to the $helpers array in a controller (AppController.php to use in all views)

public $helpers = array("Html", "Form", "TwitterBootstrap.TwitterBootstrap");

Now available in your views is $this->TwitterBootstrap. If you'd like to make the helper name shorter, remember the alias functionality:

public $helpers = array(
	"Html",
	"Form",
	"TB" => array(
		"className" => "TwitterBootstrap.TwitterBootstrap"
	)
);

Methods

TwitterBootstrapHelper::input(array $options)

The form inputs in Twitter Bootstrap are quite different from the default html that the FormHelper::input() method. The TwitterBootstrap->input() aims to do the same type of thing that the FormHelper::input() would, but with the markup the bootstrap styles work with.

echo $this->TwitterBootstrap->input("field_name", array(
	"input" => $this->Form->text("field_name"),
	"help_inline" => "Text to the right of the input",
	"help_block" => "Text under the input"
));

The method will handle errors and update the class on the div surrounding the input and label to show the text and input border as red.

TwitterBootstrapHelper::basic_input(mixed $field, array $options)

While the input() method outputs the more verbose markup; the basic_input() method just outputs the minimum markup (the labal and input tags).

echo $this->TwitterBootstrap->basic_input("field_name", array(
	"label" => "Custom label text"
));

TwitterBootstrapHelper::search(string $name, array $options)

Twitter Bootstrap offers a new search specific text input field, and this method will ouput that input with its special class.

echo $this->TwitterBootstrap->search();

TwitterBootstrapHelper::radio(array $options)

This method will render groups of radio inputs. Internally it will call TwitterBootstrap->input(), so the options are the same.

echo $this->TwitterBootstrap->radio("field_name", array(
	"options" => array(
		"yes" => "Yes",
		"no" => "No",
		"maybe" => "Maybe"
	)
));

TwitterBootstrapHelper::button(string $value, array $options)

TwitterBootstrap->button() will render the submit button for forms.

echo $this->TwitterBootstrap->button("Save", array("style" => "primary", "size" => "large"));

Valid styles for the button are "primary", "success", "info", and "danger". Valid sizes are "small" and "large". If either are left out, then the default styles will apply (A grey button and a medium size). There is another option, "disabled", that takes a bool. If true it will apply the disabled styles.

TwitterBootstrapHelper::button_dropdown(string $value, array $options)

The method will build a button dropdown menu. The dropdown js plugin is required for the dropdown functionality. The list of links for the dropdown is supplied in any array of string links (the output of HtmlHelper::link()) or arrays (that are used as params for HtmlHelper::link()). A null value in the array will add a divider in the list of links. Other unique options are 'split', 'dropup', and 'right'. The 'split' option will make the caret appear in its own button instead of part of the main button. The 'dropup' option (if the value is true) will cause the list of links to appear above the button instead of below. The 'right' option will make the list be pulled right instead of the default left.

echo $this->TwitterBootstrap->button_dropdown("Button Value", array(
	"split" => true,
	"dropup" => true,
	"right" => true,
	"links" => array(
		$this->Html->link("Link 1", "#"),
		array("Link 2", "#"),
		null, // Will produce a divider line
		array("Link 3", "#")
	)
));

TwitterBootstrapHelper::button_link(string $title, mixed $url, array $options, string $confirm)

The TwitterBootstrap::button_link() builds a button similar to the TwitterBootstrap::button() but it uses Html->link() to make an anchor tag.

echo $this->TwitterBootstrap->button_link("Edit", "/resource/edit/1", array("style" => "info", "size" => "small"));

Like the TwitterBootstrap->button(), the "disabled" option can be passed to apply the disabled styles.

TwitterBootstrapHelper::button_form(string $title, mixed $url, array $options, string $confirm)

The TwitterBootstrap->button_form() is the same as TwitterBootstrap->button_link(), but it uses Form->postLink() to create the link.

echo $this->TwitterBootstrap->button_form("Delete", "/resource/delete/1", array("style" => "danger", "size" => "small"), "Are you sure?");

Like the TwitterBootstrap->button(), the "disabled" option can be passed to apply the disabled styles.

TwitterBootstrapHelper::breadcrumbs(array $options)

TwitterBootstrap->breadcrumbs() utilizes HtmlHelper::getCrumbs() to build the breadcrumbs markup specific to Twitter Bootstrap.

$this->TwitterBootstrap->breadcrumbs(array("divider" => ">"));

TwitterBootstrapHelper::add_crumb(string $title, mixed $url, array $options)

TwitterBootstrap->add_crumb() delegates to HtmlHelper::addCrumb().

$this->TwitterBootstrap->add_crumb("Crumb Item", "/path");

TwitterBootstrapHelper::label(string $message, string $style, array $options)

Twitter Bootstrap has some reusable label styles, and TwitterBootstrap->label() simply returns this small html fragment.

echo $this->TwitterBootstrap->label("Recent", "warning");

The valid values for the second parameter are "success", "important", "warning", "info", "inverse". Passing no second param will use the default (grey) style.

TwitterBootstrapHelper::badge(int $num, string $style, array $options)

The Twitter Bootstrap badges are similar to labels, but are meant to contain an integer.

echo $this->TwitterBootstrap->badge(4, "error");

The valid values for the second parameter are "success", "warning", "error", "info", "inverse". Passing no second param will use the default (grey) style.

TwitterBootstrapHelper::icon(string $name, string $color)

This method will output the icon markup with the proper clases. The valid colors are 'white' and 'black' (default is 'black'). Valid icon names are detailed on the Twitter Bootstrap docs. Pass the name of the icon without 'icon-'.

echo $this->TwitterBootstrap->icon("fire", "white");

TwitterBootstrapHelper::progress(array $options)

The progress() method makes progress bars by specifying a width style inline. You can pass a 'width' option with a value from 0 to 100 and it will be applied as the beginning width. Passing the "striped" option will apply the striped styles, and passing the "active" option will make the progress bar animate.

echo $this->TwitterBootstrap->progress(array("width" => 50, "striped" => true, "active" => true));

The valid values for the "style" options are "info", "success", "warning", and "danger".

TwitterBootstrapHelper::flash(string $key, array $options)

Twitter Bootstrap has some nice styles that would work great for the session flash message. By default, the method will return the value in the default key that SessionComponent::setFlash() sets (that is "flash").

echo $this->TwitterBootstrap->flash("success");

The flash message can be closable if the "closable" option is passed in the options array witha value of true. Here is info on the javascript closable alerts. The alert styles offer some different styles and they are "warning", "error", "success", and "info". To set the class to use these different styles, call setFlash() like so:

$this->Session->setFlash(__("Flash message"), "default", array(), "error");

Note that the default "flash" key that will be used when a different key is not passed in the 4th param of the setFlash() method will use the "warning" styles.

TwitterBootstrapHelper::flashes(array $options)

TwitterBootstrap->flashes() will loop through the valid alert types ("warning", "error", "success", "info") as well as the default flash type ("flash"), and optionally the "auth" flash.

echo $this->TwitterBootstrap->flashes(array("auth" => true));

Passing "auth" => true, will include the "auth" string in the loop of keys to try. You may also pass in an array of keys in the option "keys" for this method to loop through.

TwitterBootstrapHelper::block(string $message, array $links, array $options)

TwitterBootstrap->block() will create the more detailed alerts. It take a string (could contain html), and an array of links. The links should be created with TwitterBootstrap->button_link().

echo $this->TwitterBootstrap->block(
	$html_content,
	array(
		$this->TwitterBootstrap->button_link("Action Link", "/path"),
		$this->TwitterBootstrap->button_link("Another Action", "/another/path")
	),
	array(
		"style" => "success",
		"closable" => true
	)
);

The valid options for "style" are "warning", "error", "success", "info". If the "closable" option is passed with a value of true, then the close link is added. Here is info on the javascript closable alerts.

TwitterBootstrapHelper::page_header(string)

TwitterBootstrap->page_header(string) will print a page heading TB style.

echo $this->TwitterBootstrap->page_header("Page Header");

twitter-bootstrap-helper's People

Contributors

beporter avatar ericfreese avatar imsamurai avatar jasonadkison avatar joeytrapp avatar manuelfischer avatar mic-kul avatar tincho avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

twitter-bootstrap-helper's Issues

No pagination support

Would be nice to be able to use this helper to properly layout and use TB pagination.

What happened to iconed_button_link?

That method seems to have disappeared - was support for it dropped or is there a new way to do it?

Thank you.

Edit: I just realized I had added that method :S Please ignore this.

it does not work for edit methods!

seriously doesn't work for edit methods. does not output the selected value and the data markup does not have model in it. It just reflects $data['input_name'] instead of $data['model_name']['input_name'].

Is it the case or am i missing something???

display form errors

Hi,
I've some problems to display form errors, so i trying to debug the code.
I think there is an error at line 201:

if (strpos (".", $ options ['field'])! == false)

instead of

if (strpos ($ options ['field'], ".")! == false)

also at line 224

$options ['type'] = 'error';

instead of

$ options ['state'] = 'error';

correct?

PS: very useful helper! thanks

Input Select Option

Small bug with using the input method, if you use a dropdown and specified the selected option, it is not carried through.

You're missing

if (isset($options["selected"])) {
    $opt["selected"] = $options["selected"];
}

in _combine_input()

Contents

Thank you for great helper, that was what i was looking for these days..

Does this class contains all Bootstrap components/features, or do you have a plan to integrate them later?

Thanks.

Radio helper generates incorrect "for" attribute

First off, great plugin! Really saved me a lot of time on my latest project. Noticed a small bug when using the radio method.

The $this->TwitterBootstrap->radio method generates an incorrect "for" tag in the label.control-label tag.

The proper syntax for a bootstrap radio button label.control-label is different from the rest of the normal label.control-label tags:

for a radio button group it should look like this:
<label class="control-label">Radio buttons</label>
for a non radio button group it should look something like this:
<label class="control-label" for="prependedInput">Prepended text</label>

Notice the "for" attribute in the radio button group should be missing. As it stands, since there is no form control element for it to be bound to, it also fails HTML5 validation.

Extend CakePHP's Helpers?

Hi,

It kind of feels like this plugin is going in the wrong direction. Wouldn't it be better to extend the Helper's (say Form)? So one can use the standard CakePHP syntax: $this->Form->input();

Example how this would be instantiated:
https://github.com/CakePHP-Courses/Taking-Control/blob/master/app/Controller/AppController.php#L5

Basic structure of the helper (note the parent::input call):
https://github.com/CakePHP-Courses/Taking-Control/blob/master/app/View/Helper/BootstrapFormHelper.php#L12

option "class" has no effect when using $this->TwitterBootstrap->input

this hasnt the expected result:
$this->TwitterBootstrap->input('name', array('class' => 'myfieldclass') );

which would be something like

just added the line

if(!empty($options['class'])) $opt['class'] = $options['class'];

on line 268 (method _combine_input)

not sure of how to submit a pull request and no per-line comments allowed so excuse me for skipping the "formal protocol"

Random error

Hi

from time to time I'm getting this random error in my log (2-3 times a day with 500 users per day)

PHP Fatal error: Call to a member function flashes() on a non-object in /var/www/web/app/View/Layouts/default.ctp

I'm using the following code in default.ctp

try{
    echo $this->Session->flash();
    echo $this->TwitterBootstrap->flashes();
} catch (Exception $e) {}   

Also I have the helper defined in AppController.php
public $helpers = array('Time','Text','Session','Form','Html','Cache','TwitterBootstrap.TwitterBootstrap');

What do you think the problem is? can this be related to APC caching ?

Which is latest stable version?

I noticed the Master is older than the 2.1 branch.

So I'm confused as to which branch is stable and which I should be downloading?

There is a strict error in the Form helper / radio method in PHP 5.4.x

Error Message:

Strict (2048): Declaration of BootstrapFormHelper::radio() should be compatible with FormHelper::radio($fieldName, $options = Array, $attributes = Array) [ROOT/plugins/TwitterBootstrap/View/Helper/BootstrapFormHelper.php, line 456]

Solution:

/**
     * Outputs a list of radio form elements with the proper
     * markup for twitter bootstrap styles
     *
     * @param array $options
     * @param array $attr
     * @access public
     * @return string
     */
    public function radio($field, $options = array(), $attr = array()) {
        if (is_array($field)) {
            $options = $field;
        } else {
            $options["field"] = $field;
        }
        if (!isset($options["options"]) || !isset($options["field"])) {
            return "";
        }
        $opt = $options["options"];
        unset($options["options"]);
        $inputs = "";
        $attr['label'] = false;
        $attr['hiddenField'] =  (isset($options['hiddenField']) && $options['hiddenField']);
        foreach ($opt as $key => $val) {
            $input = parent::radio(
                $options["field"],
                array($key => $val),
                $attr
            );
            $id = array();
            preg_match_all("/id=\"[a-zA-Z0-9_-]*\"/", $input, $id);
            if (!empty($id[0])) {
                $id = end($id[0]);
                $id = substr($id, 4);
                $id = substr($id, 0, -1);
                $input = $this->Html->tag(
                    "label",
                    $input,
                    array("class" => "radio", "for" => $id)
                );
            }
            $inputs .= $input;
        }
        $options["input"] = $inputs;
        return $this->input($options);
    }

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.