Reputation: 12933
So I crated a class such as:
class CoreTheme_Form_Helpers_TabbedForm extends AisisCore_Form_Helpers_Content{
protected $_html = '';
public function init(){
parent::init();
if(isset($this->_options)){
$this->_html .= '<ul class="nav nav-tabs">';
foreach($this->_options as $options){
$this->_html .= '<li><a href="#'.str_replace(" ", "", $options['tab']).'" data-toggle="tab">
'.$options['tab'].'</a></li>';
}
$this->_html .= '</ul>';
$this->_html .= '<div class="tab-content">';
foreach($this->_options as $options){
$this->_html .= '<div class="tab-pane" id="'.str_replace(" ", "", $options['tab']).'">...</div>';
}
$this->_html .= '</div>';
}
}
public function __toString(){
return $this->_html;
}
}
What I am looking to do is add the class active to the div with the class tab-pane, but only once, and only to the first one.
so in:
foreach($this->_options as $options){
$this->_html .= '<div class="tab-pane" id="'.str_replace(" ", "", $options['tab']).'">...</div>';
}
the html should be something like:
<div class="tab-pane active" id="something">...</div>
<div class="tab-pane" id="something1">...</div>
<div class="tab-pane" id="something2">...</div>
ideas?
Upvotes: 0
Views: 813
Reputation: 1240
You could do a str_replace to replace the whole for:
str_replace('tab-pane', 'tab-pane active', $this->_html, 1);
... where the last parameter, being 1, is the amount of times you replace it. http://php.net/manual/en/function.str-replace.php
Upvotes: 0
Reputation: 14835
$active = " active";
foreach($this->_options as $options){
$this->_html .= '<div class="tab-pane'. $active .'" id="'.str_replace(" ", "", $options['tab']).'">...</div>';
$active = "";
}
I think it can works
Upvotes: 0
Reputation: 14691
If you need a counter, why use foreach
? Use a regular for
:
for ($i=0 ; $i < count($this->_options) ; $i++){
$options = $this->_options[$i];
if ($i==0) {
// do something else
}
Upvotes: 0
Reputation: 78046
This is a jQuery one-liner if you don't mind adding it from the client:
$('.tab-pane').first().addClass('active');
Upvotes: 1
Reputation: 25652
Just use a variable to store the extra class active
and reset it after the first set.
$extraClass = "active";
foreach($this->_options as $options){
$this->_html .= '<div class="tab-pane '.$extraClass. '" id="'.str_replace(" ", "", $options['tab']).'">...</div>';
$extraClass = "";
}
Upvotes: 1