I can't load blocks in my default.ctp - cakephp

I want to use the blocks statements in my application. I have a theme called: Boostrap. If I use only the statement $this->element() my elements load without problems, but if I use $this->start() and $this->end() I can't load my elements.
I try create a new file called index.ctp for a TestsController but the result is the same. I put $this->fetch('header') and $this->fetch('footer') in this file and nothing.
That's only an example of my real structure:
Themed\Boostrap\Layouts\default.ctp:
<html>
<head>
<title></title>
</head>
<body>
<?php $this->start('header'); ?>
<?php echo $this->element('header'); ?>
<?php $this->end(); ?>
<?php echo $this->Session->flash(); ?>
<?php echo $this->fetch('content'); ?>
<?php $this->start('footer'); ?>
<?php echo $this->element('footer'); ?>
<?php $this->end(); ?>
</body>
</html>
Themed\Boostrap\Elements\header.ctp:
<header>
<h1>Hello World!!</h1>
</header>
Themed\Boostrap\Elements\footer.ctp:
<!-- app/views/themed/boostrap/elements/footer.ctp -->
<footer>
Copyright (c) Bla bla bla 2014.
</footer>
Details of my environment:
S.O: Windows 7
CakePHP Versión: 2.4.4
PHP Versión: 5.3.28

You're layout should define the block output, not the start and end methods:
<?php echo $this->fetch('header'); ?>
Then, in your view, you outline the block that will be outputted:
<?php $this->start('header'); ?>
<?php echo $this->element('header'); ?>
<?php $this->end(); ?>
Given this use case, blocks don't seem like the right solution. Your element seems like it would be the only thing to display, in which you would output the element on the layout. However, a more appropriate use case would be different output at a per-page level.
In your layout:
<?php echo $this->element('header'); ?>
<?php echo $this->fetch('header_block'); ?>
Page one:
<?php $this->start('header_block'); ?>
//Something unique to page 1
<?php $this->end(); ?>
Page two:
<?php $this->start('header_block'); ?>
//Something unique to page 2
<?php $this->end(); ?>

Related

View inside a view using CakePHP 3

Is it possible to display a view inside another view?
I have the following code:
<?php if ($result->type === 'brochure') : ?>
<div>
// massive template block
</div>
<?php elseif ($result->type === 'library') : ?>
<div>
// massive template block different from above
</div>
<?php else : ?>
<div>
// massive template block different from both above
</div>
<?php endif; ?>
I want to replace those with a content block so to speak. I had a look at view blocks but I'm either using it wrong or it doesn't do what I want it to do.
Is this possible in CakePHP 3?
you can use elements for that.
first you should create elements in src/Template/Element directory with .ctp format like this
// in brochure.ctp file in src/Template/Element
<div>
// your massive template block
</div>
then you can call elements like this :
<?php if ($result->type === 'brochure') : ?>
<?= $this->element("brochure") ?>
<?php elseif ($result->type === 'library') : ?>
<?= $this->element("library") ?>
<?php else : ?>
<?= $this->element("default") ?>
<?php endif; ?>

CakePHP 3 error message not showing up on login

I want to show a flash error message by default when the user or pass is incorrect.
This is my code in my UsersController:
class UsersController extends AppController
{
public function login()
{
if($this->request->is('post'))
{
$user = $this->Auth->identify();
if($user)
{
$this->Auth->setUser($user);
return $this->redirect($this->Auth->redirectUrl());
}
else{
$this->Flash->error(__('user/password incorrect'));
}
}
}
...
}
After put some field incorrectly, the flash message doesn't appear. BUT, after that, if I put the user and password correctly, I can see the flash error message in the following view (when the controller redirect me).
Also, flash cards work perfectly in any part of the project. It's just in the login when doesn't show up.
This is my login.ctp
<?php
use Cake\Cache\Cache;
use Cake\Core\Configure;
use Cake\Datasource\ConnectionManager;
use Cake\Error\Debugger;
use Cake\Network\Exception\NotFoundException;
$this->layout = false;
$cakeDescription = 'Welcome';
?>
<!DOCTYPE html>
<html>
<head>
<?= $this->Html->charset() ?>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>
<?= $cakeDescription ?>
</title>
<?= $this->Html->meta('icon') ?>
<?= $this->Html->css('base.css') ?>
<?= $this->Html->css('cake.css') ?>
</head>
<body class="home">
<br>
<br>
<div class="index large-4 medium-4 large-offset-4 medium-offset-4 columns">
<div class="panel">
<h2 class="text-center">Login</h2>
<?= $this->Form->create(); ?>
<?= $this->Form->input('email', array('label' => 'write your email')); ?>
<?= $this->Form->input('password', array('label' => 'write your password')); ?>
<?= $this->Form->button('Login', ['type' => 'submit']); ?>
<?= $this->Form->end(); ?>
</div>
</div>
</body>
</html>
Thanks
You have to call render() method of Flash Helper to display Flash success/error message.
You either put this line in layout file:
<?= $this->Flash->render() ?>
Or
Either you can call in particular ctp file.
you are using no layout in your login view
but the code that outputs the flash message is usually in the default layout where you can find something like
<?= $this->Flash->render('auth') ?>
So you can do two things:
put that line of code in your login.ctp
Create a login layout that contains that code and use that layout in your view

CakePHP - View Template - default values for fetched content?

This is the example in the book, of a simple View Template
// app/View/Common/view.ctp
<h1><?php echo $this->fetch('title'); ?></h1>
<?php echo $this->fetch('content'); ?>
<div class="actions">
<h3>Related actions</h3>
<ul>
<?php echo $this->fetch('sidebar'); ?>
</ul> </div>
And this is how it might be extended
// app/View/Posts/view.ctp
<?php
$this->extend('/Common/view');
$this->assign('title', $post);
My question is:
How can I set a default value/content for (let's say) the title, in order to overwrite if needed ?
Thank you!
Fetch the var. If its not empty, echo it, otherwise echo the default.
$title = $this->fetch('title');
echo !empty($title) ? $title : 'Default';

Is it possible to get a different header.php on language switch with qtranslate (Wordpress)?

the problem is that on home page I have a lot of code which is added through header.php and it is not amendable through admin panel. What I want to do is to switch header files on language switch. Whenever a user press on the language icon, lets say on the English language icon the header should also switch from <?php get_header('mylanguage') ?> to <?php get_header('english') ?> or etc. Is it possible to do like this?
Regards,
In your page template instead of calling header through:
get_header();
use this to enable header switching when changing languages;
<?php if (qtrans_getLanguage() == 'de'): ?>
<?php include(TEMPLATEPATH.'/header-home-de.php' ); ?>
<?php else : ?>
<?php include(TEMPLATEPATH.'/header-home-en.php' ); ?>
<?php endif; ?>
In this example there are 2 headers depending on selected language.
Surely, it is possible in qTranslate. Even I've done qTranslate Swicher in bootstrap navigation.
<?php } if (qtranxf_getLanguage() == 'en') { ?>
English
<ul class="dropdown-menu">
<?php echo qtranxf_generateLanguageSelectCode('code'); ?>
</ul>
<?php } elseif (qtranxf_getLanguage() == 'fr') { ?>
Français
<ul class="dropdown-menu">
<?php echo qtranxf_generateLanguageSelectCode('code'); ?>
</ul>
<?php } ?>

Help with the auto complete method in cakephp

I'm trying to have an auto complete box in my application,where the values are retrieved from a table.
I want to share a form with set of users in the Users table. i have a link called 'Share' and when I click the link, the autocomplete box is generated.
Now when I type in the text box any letter, I want it to be auto suggested.
This is my view file, /views/main/home.ctp
<?php echo $javascript->link('prototype');?>
<?php echo $javascript->link('scriptaculous');?>
<?php echo $javascript->link('effects');?>
<?php echo $javascript->link('controls');?>
$(document).ready(function(){
$(".Share<?=$r['Form']['id'];?>").click(function(){
$("#share<?=$r['Form']['id'];?>").toggle("show");
});
});
<li id="Share<?php echo $r['Form']['id']?>">
Share
<div id="share<?php echo $r['Form']['id']?>">
<?php echo $form->create('User', array('url' => '/forms/share'));?>
<?php echo $ajax->autoComplete('User.name', '/forms/autoComplete');?>
<?php echo $form->end('Share');?>
</div>
</li>
This is my auto_complete.ctp file:/views/forms/auto_complete.ctp
<?php echo $javascript->link('scriptaculous.js');?>
<?php echo $javascript->link('prototype.js');?>
<ul>
<?php foreach($users as $user): ?>
<li><?php echo $user['User']['name']; ?></li>
<?php endforeach; ?>
</ul>
And this this the function in the forms_controller: /forms/autoComplete
function autoComplete()
{
$this->set('users',$this->User->find('all',array('conditions'=>array('User.name LIKE' => $this->data['User']['name'].'%'))));
$this->layout = "ajax";
}
I get the results in the auto_complete.ctp file, i.e, if I type in the letter 's' in the autoComplete text box, I get the corresponding users in that file, but I do not get those values in the text box. Someone help me please.
Well I found the answer myself.
Autocomplete feature of cakephp does not work if you use JQuery.
I removed the line
<?php echo $javascript->link('jquery');?>
and it is working...
EDIT:
If you also want to work with jquery and need the jquery.js file, you would need to enable no-conflict mode in jQuery,as given in the site
http://docs.jquery.com/Using_jQuery_with_Other_Libraries

Resources