How to generate 'a href="javascript:void(0)"' like link in cakephp? - cakephp

How to generate 'a href="javascript:void(0)"' like link in CakePHP?
I make an application, the content will insert into the editor textarea when user click a list of image. I add a class to these images and write some code in the javascript file. Everything is going well.
But the link of the image is a URL address, but not 'href="javascript:void(0)' like URL. Anyone could tell me how to make it in CakePHP?
Thanks in advance!

<?php
echo $this->Html->link(
'/path/to/image/',
'javascript:void(0)'
);
?>
You can either set a path to the image or use the Html helper to generate the image tag code. The second parameter will set the href.

Don't believe there is any dynamic way, however when you are creating your form element you can set it in the options array 'href' => 'javascript:void(0)'

Related

CakePHP Overide getCrumbList

How can I go about overiding CakePHP's code without creating it manually myself? I'm attempting to customise the getCrumbList function.
The lastClass option is applying a class to the 'li' tag sucesfully, but I'd also like to remove the link/ ahref altogether for the last tag.
Function generating crumbs
echo $this->Html->getCrumbList(array('class' => 'breadcrumb', 'lastClass' => 'active'), 'Home');
Output of getCrumbList
<ul class="breadcrumb"><li class="first">Home</li><li>Scheduler</li><li class="active">Downloaded Playlists</li></ul>
Simple solution - You dont include the URL in addCrumb. Doh!

How to add country list in registration form in cakephp?

I have simple registration form. I just want to add dropdown country list in registration form in cakephp. Please give me simple and detail description of what to do in all related files (like changes in module, controller and .ctp files). I have country list in my database table 'countries'.
In register.ctp i did this:
echo $form->input('country_id');
I am very new in cakephp, please help me.
Thanks!
Use Find to get a List of all of your countries...
$this->set('countries', $this->Country->find('list',array('Country.id','Country.name')));
Then in your view use the form helper and pass your countries to it via the options parameter.
echo $this->Form->input('country_id', array('options'=>$countries));
Use following syntax Just replace User model with appropriate model that you are using.
$this->set('countries',$this->User->Country->find('list'));
In controller (make sure the column names are correct):
$this->loadModel('Country');
$this->set('countries', $this->Country->find('list',array('Country.id','Country.name')));
In view:
echo $form->input('country_id', array('options' => $countries));

cakephp placeholder if image not exist

I have a database with images associated to each entry.
Anyhow, some entries don't have an image.
Googling I found some js and php way to apply in the view, but I'm pretty sure there is a (much nicer) MVC-way to do this, just I don't know how..
Is there a nice Cake-way to display placeholder image where none exist?
Supposed the data you set from the controller named 'post'. And you have 'your_default_image' in your /app/webroot/img/
<?php
if(empty($post['Post']['img'])) {
echo $this->Html->image($post['Post']['img']);
} else {
echo $this->Html->image('your_default_image');
}
And you can wrap those logic in your helper.
You can use placehold.it anywhere you need for placeholding images.
Just use http://placehold.it/WxH like http://placehold.it/250x250 and you're set.

HTML Link Helper full_calendar plugin breaks links

I'm using elements for different menus in my site using CakePHP to create the links, eg:
echo $this->Html->link(
'Home',
array(
'controller'=>'users',
'action'=>'home'
)
);
My links all break when using the full_calender plugin 2.0 branch from Github when I click the link to the full calender page located at
http://localhost/mysite/full_calendar
All the links in my element are broken and become:
http://localhost/mysite/full_calendar/home/
instead of
http://localhost/mysite/home
I've installed the plugin in app/plugin.
I'm using the controller name and view in my links in the element so whats going wrong?
I solved it by adding plugin link to the links not using the plugin;
echo $this->Html->link('home',array('plugin'=>'','controller'=>'users','action'=>'home'));
I suggest using:
echo $this->Html->link('home',array('plugin'=>false,'controller'=>'users','action'=>'home'));
using
'plugin'=>false
works perfectly and it looks cleaner than 'plugin'=>''.

CakePHP Elements - how to put javascript?

I've just started using cakephp elements and they're awesome (I used to use include).
I have an element gallery and an element called comments (for a certain page) and for them I have some javascript code attached to both of them. Do you have any suggestions on how I could include that javascript code in the element? If I simply add it like that it will load javascript before loading the html after the element and I don't think it's very wise.
You could put the Javascript code directly into the element file, or put the Javascript code in into your webroot folder, <cake directory>/app/webroot/js/ and include the file in your layout by using the HTML helper:
echo $html->script("myCode");
If you're worried about the Javascript code executing before the page has completely loaded, then use window.onload or $(document).ready() if you're using JQuery.
If I understand you correctly, you want to have JS specific to a page loading in the header when you call a certain element, but that the JS could be different for each element. You also want the JS to be referenced at the beginning of your HTML document.
This is actually quite easy to do. Just make sure that you have <?php echo $scripts_for_layout; ?> in you <head> tag in the layout you are using.
Then, within the element, just do:
<?php $this->Html->script("js_file", array("inline"=>false)); ?>
js_file is the name of your JavaScript file in app/webroot/js/. In this case, the file would be called js_file.js but you must leave off the .js when referencing it as above.
It doesn't matter where abouts in the element file you put this because the "inline"=>false part ensures it won't actually appear at that stage in the code. Instead, it will appear in the <head> wherever you put <?php echo $scripts_for_layout; ?> in your layout.
In cakephp 3 instead of array('inline' => false) you should use array('block' => true) if anyone is looking for that answer like I was.

Resources