「boostrap」タグアーカイブ

cake1.3 でboostrap

参考:http://d.hatena.ne.jp/slywalker/20111006/1317884853

http://d.hatena.ne.jp/slywalker/20111006/1317884853

app/plugins/に TwitterBootstrap を置く
app/views/elementsに alert.ctp を置く
app/webroot/に Bootstrapの「css,img,js」 を置く

app_controllrt.php に記入
[text] public $helpers = array(
‘Session’,
‘Html’ => array(‘className’ => ‘TwitterBootstrap.BootstrapHtml’),
‘Form’ => array(‘className’ => ‘TwitterBootstrap.BootstrapForm’),
‘Paginator’ => array(‘className’ => ‘TwitterBootstrap.BootstrapPaginator’),
);
public $layout = ‘TwitterBootstrap.default’;[/text]

app/views/pagination.ctp
[text]<?php
if (!isset($modules)) {
$modulus = 11;
}
if (!isset($model)) {
$models = ClassRegistry::keys();
$model = Inflector::camelize(current($models));
}
?>
<div class="pagination">
<ul>
<?php echo $this->Paginator->first(‘<<‘, array(‘tag’ => ‘li’)); ?>
<?php echo $this->Paginator->prev(‘<‘, array(
‘tag’ => ‘li’,
‘class’ => ‘prev’,
), $this->Paginator->link(‘<‘, array()), array(
‘tag’ => ‘li’,
‘escape’ => false,
‘class’ => ‘prev disabled’,
)); ?>
<?php
$page = $this->params[‘paging’][$model][‘page’];
$pageCount = $this->params[‘paging’][$model][‘pageCount’];
if ($modulus > $pageCount) {
$modulus = $pageCount;
}
$start = $page – intval($modulus / 2);
if ($start < 1) {
$start = 1;
}
$end = $start + $modulus;
if ($end > $pageCount) {
$end = $pageCount + 1;
$start = $end – $modulus;
}
for ($i = $start; $i < $end; $i++) {
$url = array(‘page’ => $i);
$class = null;
if ($i == $page) {
$url = array();
$class = ‘active’;
}
echo $this->Html->tag(‘li’, $this->Paginator->link($i, $url), array(
‘class’ => $class,
));
}
?>
<?php echo $this->Paginator->next(‘>’, array(
‘tag’ => ‘li’,
‘class’ => ‘next’,
), $this->Paginator->link(‘>’, array()), array(
‘tag’ => ‘li’,
‘escape’ => false,
‘class’ => ‘next disabled’,
)); ?>
<?php echo str_replace(‘<>’, ”, $this->Html->tag(‘li’, $this->Paginator->last(‘>>’, array(
‘tag’ => null,
)), array(
‘class’ => ‘next’,
))); ?>
</ul>
</div>[/text]

paginationを表示させたいところで
[text]<?php echo $this->element(‘paginate’); ?>[/text]

views/helpers/bootstrap_form.php
[text]<?php
class BootstrapFormHelper extends AppHelper {

public $helpers = array(‘Html’, ‘Form’);

public function input($name, $options = array()) {
$default = array(
‘type’ => null,
‘label’ => null,
‘before’ => null, // to convert .input-prepend
‘after’ => null, // to convert .help-inline
‘div’ => array(
‘class’ => ‘input’,
),
);
$options = Set::merge($default, $options);

$out = array();

$label = $options[‘label’];
if ($label !== false) {
$out[] = $this->Form->label($name, $label);
}

$options[‘label’] = false;
$divOptions = $options[‘div’];
$options[‘div’] = false;
$options[‘legend’] = false;
$options[‘error’] = array(
‘wrap’ => ‘span’,
‘class’ => ‘help-block’,
);
if ($options[‘after’]) {
$options[‘after’] = $this->Html->tag(‘span’, $options[‘after’], array(
‘class’ => ‘help-inline’,
));
}
if ($options[‘before’]) {
$options = $this->_prepend($options[‘before’], $options);
}

$form = $this->Form->input($name, $options);
if (!empty($options[‘multiple’]) && $options[‘multiple’] === ‘checkbox’) {
$form = $options[‘after’] .$this->_multipleCheckbox($form, $options);
}
elseif ($options[‘type’] === ‘radio’) {
$form = $this->_radio($form, $options);
}
$out[] = $this->Html->div($divOptions[‘class’], $form, $divOptions);

$errorClass = ”;
if ($this->Form->error($name)) {
$errorClass = ‘ error’;
}
return $this->Html->div(‘clearfix’ . $errorClass, implode("\n", $out));
}

protected function _prepend($before, $options) {
$before = $this->Html->tag(‘span’, $before, array(‘class’ => ‘add-on’));
$options[‘before’] = ‘<div class="input-prepend">’ . $before;
$options[‘after’] .= ‘</div>’;
return $options;
}

protected function _radio($out, $options) {
$default = array(
‘ul’ => array(‘class’ => ‘inputs-list’),
‘li’ => array(),
);
$options = Set::merge($default, $options);

if (!preg_match_all(‘/(<input type="radio"[^>]+>)([^<]*)/m’, $out, $matches)) {
return $out;
}

if (!preg_match(‘/<input type="hidden"[^>]+>/m’, $out, $match)) {
return $out;
}
$hidden = $match[0];

$error = ”;
if (preg_match(‘/<span class="help-block"[^>]*>[^<]*<\/span>/m’, $out, $match)) {
$error = $match[0];
}

$lines = array();
foreach ($matches[0] as $key => $value) {
$line = $matches[1][$key] . ‘&nbsp;’ . $this->Html->tag(‘span’, $matches[2][$key]);
$line = $this->Html->tag(‘label’, $line);
$lines[] = $this->Html->tag(‘li’, $line, $options[‘li’]);
}

$out = $hidden;
$out .= $this->Html->div(‘clearfix’, $this->Html->tag(‘ul’, implode("\n", $lines), $options[‘ul’]));
$out .= $error;
return $out;
}

protected function _multipleCheckbox($out, $options) {
$default = array(
‘ul’ => array(‘class’ => ‘inputs-list’),
‘li’ => array(),
);
$options = Set::merge($default, $options);

if (!preg_match_all(‘/<div[^>]+>(<input type="checkbox"[^>]+>)(<label[^>]+>)([^<]*)(<\/label>)<\/div>/m’, $out, $matches)) {
return $out;
}

if (!preg_match(‘/<input type="hidden"[^>]+>/m’, $out, $match)) {
return $out;
}
$hidden = $match[0];

$error = ”;
if (preg_match(‘/<span class="help-block"[^>]*>[^<]*<\/span>/m’, $out, $match)) {
$error = $match[0];
}

$lines = array();
foreach ($matches[0] as $key => $value) {
$line = $matches[2][$key] . $matches[1][$key] . ‘&nbsp;’;
$line .= $this->Html->tag(‘span’, $matches[3][$key]) . $matches[4][$key];
$lines[] = $this->Html->tag(‘li’, $line, $options[‘li’]);
}

$out = $hidden;
$out .= $this->Html->div(‘clearfix’, $this->Html->tag(‘ul’, implode("\n", $lines), $options[‘ul’]));
$out .= $error;
return $out;
}
}[/text]

app/views/layouts/TwitterBootstrap.default.ctpを置く
[text]<?php //echo $this->fetch(‘content’); ?>//書き換え
<?php echo $content_for_layout; ?>//これ[/text]
[text]<?php //echo $this->fetch(‘script’); ?>//書き換え
<?php echo $this->element(‘sql_dump’); ?>//これ[/text]