I've created a custom view template (see link below), which I'm using to bake my views in cakephp.
http://book.cakephp.org/view/789/Modify-default-HTML-produced-by-baked-templates
Everything works fine, but if I try to incorporate an element in my view template, such as
<?php echo $this->element('menu'); ?>
I get an error while baking the view. If I comment out this line, baking works fine.
This is not a huge problem as I can easily comment out the line and edit the views after baking them - I'm just curious why this happens.
EDIT
The error I get in bake is something like "Notice: Undefined property: ViewTask::$element in C:\xampp\xampplite\htdocs\company\app\vendors\shells\templates\views\index.ctp on line 87 ('menu')" - I guess it must be a problem with bake trying to interpret $this->element('menu') rather than just echoing it.
You're exactly right:
"I guess it must be a problem with
bake trying to interpret
$this->element('menu') rather than
just echoing it."
So echo it, e.g.
<?php echo "<?php echo \$this->element('menu'); ?>"; ?>
Related
In the index action of my UsersController I want to pass strings and arrays to view, so that my View is render correctly. I don't know what am I doing wrong but my view file is not getting one of my array to view.
$users = $this->User->getUsers();
//Setting all variable that are present in layout
$this->set('title_for_layout','Users - Admin');
$page_headings = array("text" => "Users", "clss" => "icon-head");
$this->set('page_heading', $page_headings);
//Ends here
//This is actually going to action view file
$this->set(compact('users'));
My View file Code is like this
<div><?php echo $page_heading[0]['text']; ?></div>
<?php foreach($users as $user){ ?>
I am able to get $users variable here but noting getting $page_heading.
I am getting this error Undefined variable: page_heading.
I have tried everything like these:
$this->set($page_headings);
$this->set(compact('page_headings');
and yes I have change my variable name in view file also to page_headings after doing above code. I am not able to get it working. Can Please anybody help.
Thanks in advance.
This was happened because I was editing my live server file directly from filezilla's edit option and when I was saving it after editing file, it was somehow becoming the one line code and because of the comment tag all code that I had edited became comments.
I rectified this problem when i took fresh copy from server.
So what I learned today: After say working about half an hour on file, take updated file from server if you editing from filezilla because you don't know how it gonna save the file.
Thanks ndm for your help.
I read this and this.
But i couldn't find the answer.
I have this file, where my action's views exist:
View/MyController/index.ctp
I also have this file
View/Commons/blocks.ctp
Blocks.ctp file includes these:
$this->start('sidebar1');
echo 'Some content';
$this->end();
$this->start('sidebar2');
echo 'Some content more';
$this->end();
So in the "index.ctp" file i want to fetch sidebar1 or sidebar2.
How can i do this?
I wrote this to index.ctp, but didn't work.
<?php echo $this->fetch('sidebar1'); ?>
Also this one didn't work
<?php echo $this->fetch('../View/Commons/blocks.ctp/sidebar1'); ?>
Thank you
Put at top of your index.ctp:
$this->extend('Common/blocks.ctp');
With blocks and view inheritence, you can create "sub layouts" which are basically analogous to the standard Cake layout file. So you'd have the main layout.ctp, and the controller-action view ctp would be based on a parent view file (e.g. /Commons/xxxx.ctp) which is "populated" via blocks.
Blocks are like elements but less "formal" unless you use the "view inheritance" features. Their markup & data get created in your scripts, in possibly multiple locations so they can be more cumbersome to debug/maintain (i.e. imagine appending markup to a block from multiple classes). They're also harder to reuse if you don't use inheritance.
Elements are more like stand-alone view files that can be used within any controller+action view or layout: all the markup is in one place and you just pass in the data.
Bottom line: if you're new to Cake, you can get by fine with just elements. View inheritance can help make views/layouts more elegant but at the price of some complexity.
I am using cake php now. I have two file example1.ctp and example2.ctp. I want to include example2.ctp in example1.ctp just like how we add php page using "include". I am new to this please suggest me how to do it.
According to me , You have to create Elements..
CakePHP can help you repeat parts of your website that need to be reused. These reusable parts are called Elements.
<?php echo $this->element('ur elements ctp file name'); ?>
I have a fat view (add) with a lot of inputs (it's a form).
For a role , there is a user_id input that is necessary, but for another role it isn't necessary. Here it comes my question, I know that elements are used for this kind of things, but I would be duplicating a lot of code if I make a view with this input and another one without it. Is there any way to just replace a line of code depending on the role? How would it be?
Thank you very much in advance, have a nice day!
What's wrong with a good old if?
<?php if ($theRoleOfTheUser == 'someSpecificRole') : ?>
<div><?php echo $this->Form->input(…); ?></div>
<?php endif; ?>
I am using cakePHP v1.26.
In the default.ctp file,
I got a single of this code in it:
$session->flash();
I came a corss a web site in which the author suggested using this instead:
if($session->check('Message.flash')){
$session->flash();
}
I do not understand what this line of code is doing:
if($session->check('Message.flash')){...}
what is "Message.flash" in this case?
Is "Message.flash" a custom variable or
a built-in varibale which has been predefined in cakePHP?
Message.flash is the session variable name. It will be defined by cakephp, when you use $this->Session->setFlash('Your message'); from your controller.
if($session->check('Message.flash')){...} checks, if session Message.flash, which contains the flash message, exists.
Note also that contrary to the current manual description, $session->flash() does not echo the result, it just returns it, so you will need to have
echo $session->flash();
in your view.
For latest cakephp version
if(!($this->Session->check('Message.flash')));
// your code
In view section for show messages.
$this->Session->flash();