cakephp-2.0 simplest ajax link with jshelper - php

I want to create the most basic ajax link there is in Cakephp 2.0.
In index.ctp i have
<?php
echo $this->Js->link('myLink', array('controller'=>'technologies', 'action'=>'view'), array('update'=>'#success'));
?>
<div id="success"></div>
in TechnologiesController.php i have
public function view(){
$this->set('msg', 'message');
$this->render('view', 'ajax');
}
and in view.ctp i have
<?php echo $msg;?>
Instead of setting the view in the success div, it navigates to the http://local.cake.com/technologies/view page to display the message.
Any help much appreciated!

By default scripts are cached, and you must explicitly print out the cache. To do this at the end of each page, include this line just before the ending tag:
echo $this->Js->writeBuffer(); // Write cached scripts
I am using this at the end of my default.ctp in the Layouts Folder

Be sure to set $components = array('RequestHandler') in your controller

So, in total the code would look like this - and it works for me (CakePHP 2.2.4):
index.ctp:
<?php
echo $this->Js->link('myLink', array('controller'=>'technologies', 'action'=>'view'), array('update'=>'#success'));
?>
<div id="success"></div>
echo $this->Js->writeBuffer();
Thank you a lot, this helped me understand how things work in 2.0 and above :)

try $this->autoRender = FALSE;

I think you might have to put this:
echo $this->Js->writeBuffer();
somewhere - for example right below the $this->Js->link call.
Hope that helps!

I just put this:
echo $this->Js->writeBuffer();
it work for me, hope i will for you

Related

Best way to remove header and footer for a particular page in YII

I came across a situation where I don't need to show the header (it has the navigator bar and logo)andfooter` for a particular page in yii. There are a number of tweaks to do this. For example, having a check for a particular action where the layout will not load header and footer..
Does yii provide any thing solid for this issue?
Do we have anything like $this->layout->unsetHeader();? or $this->layout->unsetFooter();?
I don't want to have a check in the layout file to sort this out.
Help would be appreciated.
You can set $this->layout to false;
$this->layout = false
$this->render('view', $data);
This can also be achieved using renderpartial
$this->renderPartial('view', $data);
The approach above will display a page segment and not other items of the page layout (for example inclusion of javascript and css, and HTML headers).
If you want your layout files to have logic in it, you can pass variables from the controller.
$this->layout = 'mytheme';
$this->show_css = true;
$this->render('view', $data);
and in your layout
<html>
<body>
<?php if ($this->show_css) { ?>
<div id="menu">
<ul><li>Menu Option 1</li><li>Menu Option 2</li></ul>
</div>
<?php } <?
<div id="body">
<?php echo $this->content; ?>
</div>
<div id="footer">
</div>
</body>
</html>
If you don't want to use a variable in the controller. use a global variable instead.
Yii::app()->params['show_menu'] = true;
I haven't find function like you looking for.
I think the best and siplest way is however use a proper layout, and call it in controller in this way
public function actionYourView()
{
$this->layout='yourLayout';
$this->render('yourView');
}
In this way you don't need check inside the layout for header and footer the logic is all in the controller. And assign the proper layout is easy.

Create back link user referer in CakePHP

I've tried doing the following in my 404 error page to create a back link the referring page so that I don't have to rely on javascript.
<?php $this->set('refer', $this->referer()); ?>
<p><?php echo 'Back to previous page'; ?></p>
However it doesn't work... Any ideas why? This code is done in /Errors/error404.ctp
Use the request object :)
$this->request->referer();
In CakePHP3 you can use:
<li><?= $this->Html->link(__('Back'), $this->request->referer()) ?></li>
It should be fine.
Try adding this to the beforeRender method of your app controller:
beforeRender() {
$this->set('refer',$this->referer);
}
It should be available to all views then.
What about using just $_SERVER['HTTP_REFERER'] instead? I know this is not the Cakey way to do it but it seems like it would work in your case.
$this->request->referer() works in .ctp file for Cakephp version 2.8.0
<?php echo $this->Html->link(__d('user', 'Login', true), $this->request->referer(), array('class' => 'login_text')); ?>

php excluding piece of html code?

I have a custom CMS here entitled phpVMS, and I want to exclude a piece of code, a banner for a single page. phpVMS is steered using templates, for instance, the main template that codes the general layout for all pages is entitled layout.tpl. So, like I said, this displays whatever is in the template, on all of the pages. I have however created a special control panel, and therefore require to exclude the banner, because it slightly destroys the theme of it. Is there any PHP code that excludes a piece of code on a single site? I need to remove a single div...
<div id="slideshow"></div>
...on a single page.
Basically, I could create a new template but this is a very long winded and unefficient way within this CMS, and the final result isn't that great - because I can't reinclude the mainbox div which is the box defining the content on the centre white bit of the theme - it's already in the layout.tpl.
I hope you can somehow help me, hope I've included enough information there.
Thanks.
I don't think you can do what you're asking in PHP, but you might be able to do this on the client-side, by either hiding the div (CSS display:none) or by removing it with JavaScript. You might be able to do something like:
<?php
include("layout.tpi");
if (condition)
{
// Javascript:
echo "<script>document.getElementById('slideshow').style.display = 'none';</script>";
// OR jQuery:
echo "<script>$('#slideshow').hide();</script>";
}
?>
If you use a variable to determine you don't want to include the div, you could do this:
<?php if ($include) { ?>
<div id="slideshow"></div>
<?php } ?>
OR
<?php
if (!$include)
echo "<!--";
?>
<div id="slideshow"></div>
<?php
if (!$include)
echo "-->";
?>
EDIT: Obviously, there is no good reason to use the second method. The second method will only comment out the HTML so it will still show up in the source.
I'm not sure if this is what you are looking for, but seems simple
<?
$template = true;
if($template) {
?>
<div id="slideshow"></div>
<?
}
?>
On the template, you could have some code that reads:
if($_SERVER['PHP_SELF'] == /*control panel file*/) {
//exclude
}else{
//include
}

drupal_set_message() not working in node.tpl.php template on Drupal 7

My problem is with
<?php drupal_set_message('Hello World'); ?>
in node.tpl.php
In node.tpl.php, I have also done
<?php print_r(get_defined_vars()); ?>
to find out if the theme template is set correctly. The answer is yes.
I also have page_top and page_bottom set in my [theme].info file
regions[content] = Content
regions[help] = Help
regions[page_top] = Page Top
regions[page_bottom] = Page Bottom
$messages is outputted in page.tpl.php
<div id="messages">
<?php print $messages; ?>
</div>
After checking through all these, drupal_set_message() is still not working in node.tpl.php
The $messages variable isn't listed in the node.tpl.php documentation for Drupal 7. Do you have the $page_top variable being output in your html.tpl.php? I think that may be where the messages get output in D7.
Drupal 7 might render the messages before the node templates, that's why you can't see those messages.
Yes, it feels wrong to me too, and here is a little discussion and confirmation about the topic. (Closed: work as designed)
Not 100% sure, but you might be able to create a preprocess_node function and pass $messages to the node templates.
function template_preprocess_node(&$vars){
$vars['messages'] = drupal_get_messages();
}

CodeIgniter and Javascript/Jquery Library

As title said, I'm trying to figure out how to use javascript and jquery libraries on CI.
Following instruction in the docs, I load the library in my controller:
$this->load->library('javascript');
Then, I define the location of the jQuery file (jquery.min.js) in the config.php:
$config['javascript_location'] = 'http://localhost/ci/assets/js/jquery/');
After that, I open the view file and put in these two lines:
<?php echo $library_src;?>
<?php echo $script_head;?>
First error comes up here: Undefined variable $library_src and $script_head (don't understand where I have to set them)
Anyway, I've commented these lines and continue with jquery lib, by loading it in my controller with:
$this->load->library('jquery');
Next error: Unable to load the requested class: jquery. (it seems that it can't find the lib, what i messed up?)
Checking on system folder it looks all files are in place:
system/libraries/Javascript.php
system/libraries/javascript/Jquery.php
Thanks in advance for your help!
Put the code in the config.php like this:
$config['javascript_location'] = 'js/jquery/jquery.js';
$config['javascript_ajax_img'] = 'images/ajax-loader.gif';
In your controller file (e.g. controllers/sample.php) type this codes:
function __construct()
{
parent::__construct();
$this->load->library('javascript');
}
function index()
{
$data['library_src'] = $this->jquery->script();
$data['script_head'] = $this->jquery->_compile();
$this->load->view('sampleview', $data);
}
In your view file (e.g. views/sampleview.php) type this codes:
<?php echo $library_src;?>
<?php echo $script_head;?>
This works for me. I hope it works for you too. XD
It is important to note that this Driver is marked as experimental so I wouldn't rely on it.
Also, personally I think it's asking for confusion and headaches to try and directly mix server-side portions of your applications with client side portions.
To use javascript in your views, I would just start out by loading them like this...
<script type="text/javascript" src="<?= base_url() ?>path/to/jquery.js"></script>
Because this driver is experimental the documentation isn't quite there yet. But I was able to find a solution.
First, the documentation has an error in it. Unless you change the core Javascript library (not suggested) the reference variable is not $script_head but in fact$script_foot.
Second, once you've finished making your calls, it seems you need to run
$this->javascript->external();
and
$this->javascript->compile();
These functions set the $library_src and $script_foot variables.
To put it all together, in your controller you would have:
class Some_Controller extends CI_Controller {
public function index()
{
$this->javascript->click('#button', "alert('Hello!');");
$this->javascript->external();
$this->javascript->compile();
$this->load->view('index');
}
}
In your view you would have
<html>
<head>
<?php echo $library_src; ?>
<?php echo $script_foot; ?>
Although CI first looks for system folder, you can also try putting your libs in the these folders:
application/libraries/Javascript.php
application/libraries/javascript/Jquery.php
Not sure why you have to load your js file via controller and then echoing it in your view. You can just load ur js file using HTML tags directly in your view. Passing data from controller and echoing it in view is mostly used when your variable's value is dynamic/ needs to be loaded from databases etc.
As we know on the user guide, first, it was an experimental but working. Thr first step is open your config file under application/config/config.php .
Put the following line :
// path to JS directory you want to use, I recommended to put the .js file under 'root app/js/yourfile.js'
$config['javascript_location'] = 'js/yourfile.js';
Second step is open your controller, within constructor method, put the following code :
// Load javascript class
$this->load->library('javascript');
$this->load->library('javascript/jquery'); // if u want to use jquery
Then, still in controller file within index method :
$data['library_src'] = $this->jquery->script(); // Because I refer to use jquery I don't test to use $this->javascript->script().
Then open your view file and put the following code within tag head :
<?php echo $library_src; ?>
That way is working for me, let's try it.
Use:
$this->load->library('javascript/jquery');
instead of:
$this->load->library('jquery');
This will load your jQuery library.
I had the same problem, and found :
<?php $this->load->library('javascript'); ?>
<?php $this->load->library('javascript/jquery'); ?>
on https://ellislab.com/forums/viewthread/181742/#860506
Because jquery is in javascript folder.
Or
$autoload['libraries'] = array('database', 'session', 'javascript', 'javascript/jquery');
In the autoload.php file.
That solved the problem of loading the librairy.
You can try this, it's work to me.
in config.php
$config['javascript_location'] = 'http://localhost/ci/js/jquery.min.js';
in Controller
public function __construct() {
parent::__construct();
$this->load->library('javascript');
$this->load->library('javascript/jquery');
}
public function index(){
$d['library_src'] = $this->jquery->script();
$d['logalert']=$this->jquery->_show('#logalert',1000,"$('#logalert').html('hello world');");
$this->load->view('main',$d);
}
in view (head)
<?php echo $library_src; ?>
in view content (body)
<div class="alert alert-dismissable alert-info" id="logalert">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
</div>
in javascript view
<script type="javascript">
$(document).ready(function(){
<?php echo $logalert; ?>
};
</script>
happy coding :)

Categories