I wanted to ask, if there is a way to assign meta description tag from view file ?
I know, that this code inside my .ctp file will work fine:
// Assign title tag
$this->assign('title','Page title);
I have tried :
$this->Html->assing('description','description']);
But without success.
Or, if this is not possible, how set meta description, but not from layout?
This works, but only in layout view file:
<?= $this->Html->meta(
'description',
'enter any meta description here'
);?>
Use the block option for the meta() function (http://api.cakephp.org/3.0/class-Cake.View.Helper.HtmlHelper.html#_meta)
<?php $this->Html->meta(
'description',
'enter any meta description here',
['block' => 'meta']
);?>
And then echo the meta block in your layout:
<?= $this->fetch('meta'); ?>
Related
I've a problem with WP API to insert a post with HTML Tags.
I'm using the method: wp_insert_post() and the content is like this:
$content = "Hi, this is an example <br/> of the content.";
The result I want when I publish the post is:
Hi, this is an example
of the content
But the result I have when I publish the post is:
Hi, this is an example <br/> of the content.
When I go to the WP Editor, The Text editor looks like:
Hi, this is an example <br/> of the content.
And The Visual Editor:
Hi, this is an example <br/> of the content.
And obviosly I want this result in the Text editor:
Hi, this is an example <br/> of the content.
And in the Visual Editor:
Hi, this is an example
of the content.
This happens with all html tags.
Thank you very much.
**** UPDATE WITH THE CODE ****
$content="Hi, this is an example <br/> of the content.";
$my_post = array(
'post_title' => $title,
'post_status' => 'publish',
'post_content' => $content,
'post_author' => 1,
'post_category' => array(8,39));
// Insert the post into the database
$post_id = wp_insert_post( $my_post );
I've found a solution:
html_entity_decode($content)
http://www.php.net/manual/en/function.html-entity-decode.php
I need to include some html code inside Html::button in yii2. According to the Class yii\helpers\BaseHtml i can pass html code to $content. Please, correct me if i am wrong. So i wrote:
<?= Html::button{'<div class=\'row\'></div>,[]); ?>
and it works. But what if the code i want to insert is big? How do i include big code with less pain?
Thank you very much.
You could use views and renderPartial
e.g. in your view
<?php
$html = $this->context->renderPartial('sub_view', [
'attribute' => 'test',
]);
echo Html::button($html,[]);
?>
and in your subview your static html or dynamic code
<h1>static html code<h1>
<div>
<?php
echo $attribute
?>
</div>
I tried to render something like this using html helper:
<img src="url/to/image.png" alt="alt-tag" />
I write this in my .ctp file:
<?php
echo $this->Html->link($this->Html->image("image.png", array("alt" => "alt-tag")),"#", array('class' => 'some-class'));
?>
But in browser, image was not displaying but simple plain text :
<img src="/url/to/image.png" alt="alt-tag" />
I did inspect element, where everything look as what I wanted to render. I tried to edit it live, so I selected edit as html option in inspect element. I surprised to see following code:
<img src="/url/to/image.png" alt="alt-tag" />
Please not that insted of < and > it was < and >.
I couldn't figure out what is the problem. (I am new to cakephp).
Use escape attribute to False
ex:
echo $this->Html->link($this->Html->image("image.png", array("alt" => "alt-tag")),"#", array('class' => 'some-class', 'escape' => false));
I'm trying to have CakePHP output a link that looks like this:
<a href="/foo/bar" class="some other classes">
<span class="icon new"></span>FooBar</a>
So I use the following code in my view
<?php
echo $this->Html->link(
$this->Html->tag('span', null, array('class' => 'icon new')) . "FooBar",
array('controller' => 'foo', 'action' => 'bar'),
array('class' => 'some other classes', 'escape' => false)
);
?>
However CakePHP outputs the following:
<a href="/foo/bar" class="some other classes">
<span class="icon new">FooBar</span></a>
Which breaks my design. How can I get CakePHP to append "FooBar" after the <span> tags?
EDIT: Its also worth mentioning that I know a <span> tags shouldn't be within an anchor tag usually, but in the case its a must.
You need to use an empty string in stead of null as the text for the span, then your code will work as expected.
Looking at the source code of the HtmlHelper, null is treated as a 'special' value, causing only the opening tag of the span to be created. You can see this in this line:
https://github.com/cakephp/cakephp/blob/2.3.2/lib/Cake/View/Helper/HtmlHelper.php#L906
Change your code to this and it should work;
echo $this->Html->link(
$this->Html->tag('span', '', array('class' => 'icon new')) . "FooBar",
array('controller' => 'foo', 'action' => 'bar'),
array('class' => 'some other classes', 'escape' => false)
);
Additional explanation of the closing </span>
A bit of explanation, for those who wonder:
The closing </span> in your example is actually not present in the output generated by CakePHP, but automatically 'added' by your browser. If you view the source of the HTML in your browser, you'll see that this is what is actually in your HTML:
<a href="/foo/bar" class="some other classes">
<span class="icon new">FooBar</a>
As you can see, no closing 'span'
Because the <span> is not closed, the browser will try to correct this error and automatically assumes that you 'forgot' to close it. Therefor it will add a closing </span> before the next tag it finds (in this case the closing </a>).
The 'inspector' in your browser will always show the HTML that the browser uses to render the output. This includes automatic corrections made by the browser and dynamically generated elements (e.g. Elements added via JavaScript).
To check the output of your PHP scripts, always view the source, not the inspector
In this situation, I've avoided the CakePHP helpers entirely because the markup becomes really messy and cannot take advantage of autocomplete or validation within your IDE.
I usually do something like this:
<span class="icon-new"></span>Foobar
This looks a little overkill for me. Just do this:
echo $this->Html->link(
"<span class="icon new"></span> FooBar",
array('controller' => 'foo', 'action' => 'bar'),
array('class' => 'some other classes', 'escape' => false)
);
I've been using CakePHP for 4 years and I don't see the benefit of using tag in this instance.
Would you be able to use regular PHP in this case?
I am thinking you could do it like this:
<?PHP
echo('<span class="' . 'icon new' . '"></span>' . 'FooBar' . '')
?>
I have this code;
<?php echo $this->Form->postLink(__('Delete'),
array('action' => 'delete', $homepage['Homepage']['ContentID']),
array('class'=>'btn icon-plus'),
__('Are you sure you want to delete # %s?', $homepage['Homepage']['ContentID'])); ?>
Where it displays the text 'Delete' I would like to put the trashcan icon from the twitter bootstrap css package. However when I do;
<?php echo $this->Form->postLink(__('<i class="icon-trash"></i>')....
It only displays the html rather than grabbing the icon.
Can anyone explain why its doing this and how I can resolve it?
Add 'escape' => false option to the third param.
<?php echo $this->Form->postLink(__('Delete'),
array('action' => 'delete', $homepage['Homepage']['ContentID']),
array('class'=>'btn icon-plus', 'escape' => false),
__('Are you sure you want to delete # %s?', $homepage['Homepage']['ContentID'])); ?>
Check to make sure the path to the "glyphicons-halflings.png" file is correct. By default, this image should be located in a directory named "img" that is at the same level as the folder containing the bootstrap.css file (named "css" in the default download).
css/bootstrap.css
img/glyphicons-halflings.png
Check your error console to see if there are any requested files not found.