How to add microdata (schema.org) to Yii2 Breadcrumps?
I have the following code in the application layout:
<?= Breadcrumbs::widget([
'links' => isset($this->params['breadcrumbs']) ?? []
]); ?>
I want to add microdata attributes for breadcrumbs (https://schema.org/BreadcrumbList)
Add attribute values to options, itemTemplate and activeItemTemplate:
<?= Breadcrumbs::widget([
'links' => $this->params['breadcrumbs'] ?? [],
'options' => ['class' => 'breadcrumb', 'itemscope' => true, 'itemtype' => 'http://schema.org/BreadcrumbList'],
'itemTemplate' => '<li itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem">{link}</li>' . PHP_EOL,
'activeItemTemplate' => '<li class="active" itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem">{link}</li>' . PHP_EOL,
]); ?>
UPDATE:
In fact, for Google it does not work as expected: itemprop="position" and itemprop="item" required inside <li></li>. See https://developers.google.com/search/docs/data-types/breadcrumb
Related
i want to echo like this
<li>
<img src="img/h4-slide.png" alt="Slide">
<div class="caption-group">
<h2 class="caption title">some_title
</h2>
<h4 class="caption subtitle">Dual SIM</h4>
<a class="caption button-radius" href="some_route"><span class="icon"></span>check</a>
</div>
</li>
here my code for render image carousel :
$slides = [];
foreach (Slide::find()->orderBy('sortOrder')->all() as $slide) {
/** #var $slide common\models\Slide */
$slides[] = [
'content' => Html::img(Yii::$app->urlManager->baseUrl . '/uploads/slide/' . $slide->id . '.jpg'),
'caption' => Html::tag('content-group', $slide->title)."<br>".$slide->body,
];
}
And my carousel :
<div class="slider-area">
<div class="block-slider block-slider4">
<?= Carousel::widget([
'id' => 'bxlider-home',
'items' => $slides,
'options' => [
'class' => 'slide',
'data-interval' => 3000,
],
'controls' => [
'<span class="bx-next fa fa-angle-left"></span>',
'<span class="bx-prev fa fa-angle-right"></span>',
],
]) ?>
</div>
</div>
how to Slide->title, slide->body, and some links can be in class caption-group ?
I think it would be better to create new partial file.
Create a new file called _slider.php
Call $this->render('_slider') inside configuration of the slider. Please check below code.
$slides = [];
foreach (Slide::find()->orderBy('sortOrder')->all() as $slide) {
/** #var $slide common\models\Slide */
$slides[] = [
'content' => $this->render("_slider"),
'caption' => Html::tag('content-group', $slide->title)."<br>".$slide->body,
];
}
You can write html inside _slider.php easily now. Don't need to use Html::beginTag() etc.
Generating Tags
The code generating a tag looks like the following:
<?= Html::tag('p', Html::encode($user->name), ['class' => 'username']) ?>
<p class="username">samdark</p>
$options = ['class' => ['btn', 'btn-default']];
echo Html::tag('div', 'Save', $options);
// renders '<div class="btn btn-default">Save</div>'
Hyperlinks:
<?= Html::a('Profile', ['user/view', 'id' => $id], ['class' => 'profile-link']) ?>
Images:
<?= Html::img('#web/images/logo.png', ['alt' => 'My logo']) ?>
generates
<img src="http://example.com/images/logo.png" alt="My logo" />
Lists:
<?= Html::ul($posts, ['item' => function($item, $index) {
return Html::tag(
'li',
$this->render('post', ['item' => $item]),
['class' => 'post']
);
}]) ?>
Even more?
visit
I would like to have content inside my link with cakephp. I know I can use this syntax to have an image inside my link
$this->Html->image("recipes/6.jpg", [
"alt" => "Brownies",
'url' => ['controller' => 'Recipes', 'action' => 'view', 6]
]);
For an HTML output like this :
<a href="/recipes/view/6">
<img src="/img/recipes/6.jpg" alt="Brownies" />
</a>
But the above code idn't quite what I'm looking for. I would like to have the following HTML output
<a href="/recipes/view/6">
<div>
<img src="/img/recipes/6.jpg" alt="Brownies" />
</div>
</a>
I came with this code witch could work but I will have to figure out the full path link.
<a href="<?= '/recipes/view/6' ?>">
<div>
<?= $this->Html->image("recipes/6.jpg", ["alt" => "Brownies"]); ?>
</div>
</a>
Is there a more robust way of doing what I want with cakephp?
I am not sure if this is a more robust way, but you could possibly do something like this:
<?php
echo $this->Html->link(
$this->Html->div(null, $this->Html->image('/img/recipe/6.jpg')),
array('controller' => 'recipes','action' => 'view', 6),
array('escape' => false)
);
?>
And the output would be this:
<a href="/recipes/view/6">
<div>
<img alt="" src="/img/recipes/6.jpg">
</div>
</a>
You can use Link or URL mechanism.
Using Link
echo $this->Html->link(
$this->Html->div(null,
$this->Html->image('recipes/6.jpg', ['alt' => 'Brownies'])),
array('controller' => 'Recipes', 'action' => 'view', 6),
array('escape' => false)
);
Using URL
Adding the URL inside the HTML
<a href="<?= $this->Html->url( array('controller' => 'Recipes', 'action' => 'view', 6),
array('escape' => false)); ?>" class="light_blue">
<div>
<?= $this->Html->image("recipes/6.jpg", ["alt" => "Brownies"]); ?>
</div>
</a>
I want to click on a link inside Grid View, which should open a new window in a new tab.
I don't want CHtml::Link answers, since it is YII 1.1, I am using YII 2.0.
THE BELOW CODE IS INSIDE GRID VIEW.
['attribute'=>'EMPLOYEEID',
'label'=>'EMPLOYEEID',
'value'=> Html::a('E_ID', '?r=tb-run-engine/index', ['title' => 'Go']),
],
I didnt get any value for EMPLOYEEID instead am getting [notset] as value in Grid view.and am not getting hyperlink also.Am new to yii 2.0 can any one help me to figure out this problem??
To open link in new tab/window you have to set attribute target="_blank" for this link:
some text
So in Yii2 with Html helper in view file you can write:
<?= Html::a("some text","some_url",['target'=>'_blank']) ?>
And in yii2 grid you can show raw column:
[
'attribute'=>'name', //your model attribute
'format'=>'raw',
'value'=>function ($model, $index, $widget){
return Html::a(
$model->name, //link text
['page/update','id'=>$model->id], //link url to some route
[ // link options
'title'=>'Go!',
'target'=>'_blank'
]
);
}
],
add this to your Html:a options ['target' => '_blank', 'data-pjax' => 0] or turn off pjax in grid
here is my grid view:
<?= GridView::widget([
'dataProvider' => TbRunEngineSearch::$dataprovider_static,
'filterModel' => $searchModel,
'id'=>'searchgrid',
'columns' => [
['class' => 'yii\grid\SerialColumn'],
[
'attribute'=>'run_id',
'label'=>'field level details', //your model attribute
'format'=>'raw',
'value'=>function ($model, $index, $widget){
return Html::a(
$model->run_id, //link text
['page/update','id'=>$model->run_id], //link url to some route
[ // link options
'title'=>'Go!',
'target'=>'_blank'
]
);
}
],
['attribute'=>'run_id',
'value'=>'product_name',
'label'=>'Product Name'],
['attribute'=>'run_id',
'value'=>'module_name',
'label'=>'Module Name'],
['attribute'=>'run_id',
'value'=>'operation_name',
'label'=>'Operation Name'],
['attribute'=>'initiated_at',
'value'=>'initiated_at',
'label'=>'Start Time'],
['attribute'=>'finished_at',
'value'=>'finished_at',
'label'=>'End time'],
['attribute'=>'run_id',
'value'=>'pass_percent',
'label'=>'Pass %'],
['attribute'=>'run_id',
'value'=>'fail_percent',
'label'=>'Fail %'],
['attribute'=>'run_id',
'value'=>'operations_num',
'label'=>'Operations #'],
['attribute'=>'build_num_primary',
'value'=>'build_num_primary',
'label'=>'Build # Pri/Sec'],
'run_status',
'source',
['attribute'=>'env_primary',
'value'=>'env_primary',
'label'=>'ENV # Pri/Sec '],
['attribute'=>'instance_primary',
'value'=>'instance_primary',
'label'=>'INSTANCE # Pri/Sec '],
],
]);
?>
I am loging out user through following code. This is my view code behind logout button:
<li>
<a href="<?= Url::to(['site/logout'])?>">
<i class="fa fa-sign-out"></i> Log out
</a>
</li>
My controller code is:
public function actionLogout()
{
Yii::$app->user->logout();
$model = new LoginForm();
$this->layout = 'index';
return $this->render('login', ['model' => $model]);
}
In the logout it shows me:
Method Not Allowed. This url can only handle the following request
methods: POST.
What is it?
Seems like you have VerbFilter attached to logout action in your SiteController:
/**
* #inheritdoc
*/
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'logout' => ['post'],
],
],
];
}
That means this action can requested only with POST method, and you are requesting with GET, that's why exception #405 is thrown.
Either remove this from VerbFilter or add data-method attribute to request with POST:
...
Update: Another reason of this problem can be missing dependency for yii\web\YiiAsset. Make sure it's included in AppAsset:
public $depends = [
'yii\web\YiiAsset',
...
];
YiiAsset provides data-method attribute feature which gives possibility to link act as a form with action post by writing less code. Without asset obviously link will act as regular link and standard GET request will be sent.
You can also use a custom template
'items' => [
[
'label' => 'Logout',
'url' => ['/user/security/logout'],
'template' => '{label}',
],
]
u can change the view code and echo instead of
<li>
<a href="<?= Url::to(['site/logout'])?>">
<i class="fa fa-sign-out"></i> Log out
</a>
</li>
this one:
<?= Html::a('<i class="fa fa-sign-out"></i>',
['/site/logout'],
['class'=>'btn btn-default btn-flat']), //optional* -if you need to add style
['data' => ['method' => 'post',]])
?>
You must only replace 'logout' => ['post'], with 'logout' => ['get']. In this way your error will be solved.
This way works only with Yii Framework version 2.
See more at: http://tutorials.scrisoft.com/solve-this-error-method-not-allowed-this-url-can-only-handle-the-following-request-methods-post/#sthash.fQmwYPJH.dpuf
If you are using Nav::widget to generate menus, the logout item should have linkOptions specified:
[
'label' => '<i class="fa fa-sign-out"></i>Logout',
'url' => ['/logout'],
'linkOptions' => ['data-method' => 'post'],
],
Following works too assuming you might extra class and data-method attribute.
<?=
Html::a(
'Logout (' . Yii::$app->user->identity->username . ')',
['/site/logout'],
['class' => 'ui inverted button', 'data-method' => 'post']
);
?>
this code is working for AdminLTE template.
['label' => 'Sign out (' . Yii::$app->user->identity->username . ')','url' => ['/site/logout'],'template' => '{label}',],
I'm using html5-blank for my theme.
all queries work fine when pulling from standard wordpress post type... it breaks when trying to pull from 'html5-blank' post type.
Here's the query:
<?php $my_query = new WP_Query('category_name=spotlight&showposts=2');
while ($my_query->have_posts()) : $my_query->the_post();
$do_not_duplicate = $post->ID; ?>
<!-- pull thumbnail image -->
<article class="article_wrap">
<div class="widget_thumb">
<a href="<?php echo get_permalink(); ?>">
<?php echo get_the_post_thumbnail(); ?>
</div>
<h1 class="widget_title"><?php the_title(); ?></h1>
<p class="widget_spot_description"><?php the_excerpt(); ?></p>
</article>
<?php endwhile; ?>
here's functions.php:
function create_post_type_html5()
{
register_taxonomy_for_object_type('category', 'html5-blank'); // Register Taxonomies for Category
register_taxonomy_for_object_type('post_tag', 'html5-blank');
register_post_type('html5-blank', // Register Custom Post Type
array(
'labels' => array(
'name' => __('HTML5 Blank Custom Post', 'html5blank'), // Rename these to suit
'singular_name' => __('HTML5 Blank Custom Post', 'html5blank'),
'add_new' => __('Add New', 'html5blank'),
'add_new_item' => __('Add New HTML5 Blank Custom Post', 'html5blank'),
'edit' => __('Edit', 'html5blank'),
'edit_item' => __('Edit HTML5 Blank Custom Post', 'html5blank'),
'new_item' => __('New HTML5 Blank Custom Post', 'html5blank'),
'view' => __('View HTML5 Blank Custom Post', 'html5blank'),
'view_item' => __('View HTML5 Blank Custom Post', 'html5blank'),
'search_items' => __('Search HTML5 Blank Custom Post', 'html5blank'),
'not_found' => __('No HTML5 Blank Custom Posts found', 'html5blank'),
'not_found_in_trash' => __('No HTML5 Blank Custom Posts found in Trash', 'html5blank')
),
'public' => true,
'hierarchical' => true, // Allows your posts to behave like Hierarchy Pages
'has_archive' => true,
'supports' => array(
'title',
'editor',
'excerpt',
'thumbnail'
),
'can_export' => true, // Allows export in Tools > Export
'taxonomies' => array(
'post_tag',
'category'
) // Add Category and Post Tags support
));
}
I'm checking the category box in the custom post type but the query does not populate the post. It only shows the posts from the standard post type.
Noooooooo!
You should specify the post type in the wp query:
$my_query = new WP_Query('category_name=spotlight&posts_per_page=2&post_type=html5-blank');
Edit: replaced deprecated showposts by posts_per_page