Hope you are all doing well. I have been trying to implement CMenu in Yii template. I am using,
$this->widget('zii.widgets.CMenu', array(
'items'=>$this->menu,
'htmlOptions'=>array('class'=>'collapse','id'=>'component-nav'),
));
And I want to dislpay my HTML output as
<li class=""><i class="icon-angle-right"></i> List Registration </li>
But I can not insert this section: <i class="icon-angle-right"></i> beside the label i.e. "List Registration". Is there any way to insert this <i class="icon-angle-right"></i> html part beside every label of item.
Please help me someone.
Thanks in advance
A quick dirty way:
$this->menu = array_map(function($item){
$item["label"] = "<i class='icon-angle-right'></i>" . $item["label"];
$item["encodeLabel"] = false;
return $item;
}, $this->menu);
$this->widget('zii.widgets.CMenu', array(
'items'=>$this->menu,
'htmlOptions'=>array('class'=>'collapse','id'=>'component-nav'),
));
Information from http://www.yiiframework.com/wiki/525/customizing-the-cmenu-widget/
Disable HTML encoding 'encodeLabel' => false,
Submenu change class 'submenuHtmlOptions' => array('class' => 'dropdown-menu',)
Use label property
'label' => '<i class="icon-user"></i><span class="username">Admin</span> <i class="icon-angle-down"></i>', Very useful with bootstrap
Use YII url property for correct adress i.e. 'url' => array('site/logout'),
I don't think Yii provides a way to add a html element inside a according to the class reference (yiiframework.com/doc/api/1.1/CMenu). If you want to do it the css way:
#component-nav li a {
background: url('path/to/image.png') no-repeat;
padding-left: 30px; // depends of the width of your image
}
Related
how to place yii2 form error in title of anchor tag
This is my code
$form = ActiveForm::begin([
'id' => 'login-form',
'options' => ['class' => 'form-horizontal'],
'fieldConfig' => [
'template' => '
{label}
<div class="error-block">
error
</div>
{input}
',
'errorOptions' => ['tag' => null]
],
]);
I want to add error in title of anchor tag in YII2
error
You need to display the error text inside the anchor tags title attribute, and using the template option wont help you achieve it automatically.
A few things in your approach.
You are specifying the "tag"=>null under the errorOptions which wont create the default html <p class="help-block help-block-error"></p>.
Even if you specify the tag as div or span even then it will break the HTML as the attribute title has double quotes error and the tag created will also have double quotes <div class="help-block help-block-error"></div>.
Then if you change the error to <a href="#" title=\'{error}\'>error</a> to fix the broken HTML the javascript wont be able to detect the element.
So then the event afterValidateAttribute will come to your rescue which is triggered after validating the whole form and each attribute.
The signature of the event handler should be:
function (event, attribute, messages)
where
event: an Event object.
attribute: the attribute being validated. Please refer to attributeDefaults for the structure of this parameter.
messages: an array to which you can add additional validation error messages for the specified attribute.
Now change the template under the fieldConfig to the following and remove the {error} from the template and the errorOptions, as it is not needed and just keep you custom error element inside the template.
'fieldConfig' => [
'template' => '
{label}
<div class="error-block">
error
</div>
{input}
',
],
Now add the below javascript at the top of your view where you are rendering the form.
$js = <<< JS
$("#login-form").on(
'afterValidateAttribute',
function (event,attribute,messages) {
let input=attribute.input;
$(input).siblings('.error-block').find('a').attr('title',messages.join(','));
}
);
JS;
$this->registerJs($js, \yii\web\View::POS_READY);
Now if you focus out of the fields or hit the submit button you will see the error messages being populated in the title attribute of the a tag.
I'm working with Laravel right now and can't figure out how I can apply different background colors on different pages when I am using partials and extends in blade.php.
add a variable for a class:
<body class="{{ $body_class }}">
Then return that variable from your controller when you want:
return view('whatever', [
'body_class' => 'bg-blue'
]);
And of course, define your classes in your stylesheet:
.bg-blue { background-color: blue }
The above works great... but recommend this when you add the variable to the tag:
<body class= "{{ $body_class ?? '' }}" >
That way if your body_class is not assigned by the controller, the page will still render.
I like to use bootstrap 3 with yii. I am stuck with how to edit CListview and CGridview and add in the neccesarry classes.
My code
$this->widget('zii.widgets.CListView',array(
'dataProvider'=>$dataProvider,
// refers to the partial view named '_thumb'
'itemView'=>'_thumb',
'template' => "{summary}{sorter}{pager}{items}{pager}",
'itemsCssClass' => 'nav row',
'pagerCssClass'=>'pagination', // doesn't work is set on <div> instead of <ul>
// 'sorterCssClass'=>'row-fluid', // how to so this...
)); ?>
I tried putting the 'pagination' class. but the class is splaced on the div tag instead of the ul tag that is required. With CGridview it is the same
I used the yii bootstrap extension but it doesn't work with bootstrap3 and I like to know how to do it the easy way.
I want to build a menu from an array in Laravel. What I'm currently doing is putting the array in a view
$menu = ['home', 'users' => ['create users' , 'update user', 'activity log']];
and then looping through it to generate the menu:
<section>
<!-- Left Nav Section -->
<ul class="left">
<li class="divider"></li>
#foreach($menu as $key => $nav)
<li class="has-dropdown">
{{ $key }}
<ul class="dropdown">
#foreach($nav as $subnav)
<li>
{{ $subnav }}
</li>
#endforeach
</ul>
</li>
#endforeach
</ul>
</section>
Is there any other way that I can achieve the same result without putting the data in the view?
I also tried creating a constructor function in the controller:
public function __construct() {
$menu = ['home', 'users' => ['create users' , 'update user', 'activity log']];
return $menu;
}
But I guess that is not how it works. I appreciate any ideas on how I can go about this. Thanks in advance
View composers to the rescue!
They are executed every time before a view is rendered, so you can use this to pass standard data to them.
If you're using controller layouts you can bind data to the layout from within the constructor. Just make sure you call the parent constructor first so that the layout is instantiated properly.
public function __construct()
{
parent::__construct();
$this->layout->menu = ['home', 'users' => ['create users' , 'update user', 'activity log']];
}
That will bind a $menu variable to the layout, and will also be available to any nested views that are used with Blades #include.
Have a look at view composers:
http://www.laravel.com/docs/views#view-composers
i have this menu:
$menu['talent'] = array(
array('id' => 'welcome',
'label' => 'My Profile',
'uri' => '/profile/,
'class' => 'navlink'
)
),
array('id' => 'welcome',
'label' => 'My Profile',
'class' => 'navlink',
'uri' => '/profile/
),
);
this will end up looking like this:
<ul>
<li>My Profile</li>
<li>My Profile</li>
<ul>
how can i change the ul into a div and also the li into something else?
any idas?
Thanks
Well, to accomplish this I would use one of these two methods:
Using a View Helper and overload menu()
In order to overload Zend_View_Helper_Navigation_Menu, you could write you own helper, register it and make it extends Zend_View_Helper_Navigation_Menu. In your helper, you can overload the method you want such as _renderMenu() and make it renders <p> instead of <li> for example. In your View Helper, you would have to return the parent::menu() or something like this.
Finally, you have to register this new View Helper using:
$view->addHelperPath(APPLICATION_ROOT . '/library/My/View/Helper/Navigation', 'My_View_Helper_');
Using a partial script
This one seems to me a little bit longer, but it worth a try.
Basically, you need to set a partial script to your navigation using setPartial() method and you menu will be rendered through this partial.
<?= $this->navigation()->menu()->setMaxDepth(2)->setPartial('partials/_nav.phtml')->render() . PHP_EOL; ?>
Afterwards, in your _nav.phtml you can render your menu as you wish. From this partial, you can access the menu variables using $this->container. Then, you can iterate through each page of your container, and display what you want.
I know all of this seem pretty complicate for such a simple task, but unfortunately this is the only solution. Does anyone have a better idea?