cakephp 3 : How to add class in form button? - cakephp

I have tried this line to add class in cakephp form button, but class is not showing in html
<?= $this->Form->button(__('Login',['class'=>'login-btn'])); ?>
How can I add class in button ?

I think your example doesn't work, because the __() Call shouldn't include the array for the options of the button. Please try the following:
<?= $this->Form->button(__('Login'),['class'=>'login-btn']); ?>

Have a try on this below:
<?php echo $this->Form->button('Login',['class'=>'login-btn']); ?>
A good reference here: Creating input elements
Update
__() is for internalization. Using this will look in to your localization file and output it's corresponding translation. In your case, you include the options inside __() which I think it will cause an error but if it didn't, it will look for it's translated version and also this means ['class'=>'login-btn'] is not considered as an option anymore.

it has to be inside an array : try this
<?= $this->Form->button(__('Login'),array('class'=>'login-btn')); ?>

Related

Separate navigation menu from default.ctp in CakePHP

I am working on CakePHP 2.7. I have to show some static menu on every page. Since, the menu contains lot of sub menus, I want to keep them in a separate file navigation.ctp and show them on default.ctp
I tried extend and elements but none of them give expected result.
Note : This is not dynamic menu and I am not fetching them from database.
Place your navigation.ctp inside app/View/Elements/
Then, inside your default.ctp, include the element as follows:
<?= $this->element('navigation'); ?>
Note that if you need any variables within the element, you may need to pass them through inside an array as a second parameter, such as:
<?= $this->element('navigation', array(
"varible_name" => "variable_value"
)); ?>

Drupal 7 overriding node.tpl.php

I am trying to override the front page node, however after following the guide from the official site, it's not working.
Guide: https://www.drupal.org/node/1585528
I have taken node.tpl.php and renamed it to node--front.tpl.php and made changes to the layout, saved and cleared cache. The changes are not being displayed.
Now if I edit node.tpl.php directly it shows the changes, anyone know what I am doing wrong to override specific node templates?
EDIT:
I want to move the title below the image being displayed in the front page.
See below: Moving the title block under the content block, moves the title down as I want it to do, however how do I specify this for just the front page and not all nodes? (renaming node.tpl.php to node--front.tpl.php does not work as mentioned above)
In node.tpl.php: (title block)
<?php print render($title_prefix); ?>
<?php if (!$page): ?>
<h2<?php print $title_attributes; ?>>
<?php print $title; ?>
</h2>
<?php endif; ?>
<?php print render($title_suffix); ?>
(content block)
<div class="content clearfix"<?php print $content_attributes; ?>>
<?php
// We hide the comments and links now so that we can render them later.
hide($content['comments']);
hide($content['links']);
print render($content);
?>
To override the node template for particular node, you need to override the default node.tpl.php. For this copy the existing node.tpl.php file to node--{node_id}.tpl.php.
If you want to override the page template only for front page then you can create page--front.tpl.php file and copy the page.tpl.php file and then modify it as per your requirement.
Still you have issues, then use hook_preprocess_page() in template.php and use dpm() function to find the theme_suggestions that will give the sequence of execution of templates file.
you can use node-{nodeid}.tpl.php
It may help you.
Source: https://drupal.stackexchange.com/questions/39710/how-do-i-define-a-template-file-for-a-specific-node-id
Thanks
Samit K
samitkhulve.com

Fetching common elements in admin layout

I'm trying to fetch a common element from my admin.ctp layout
<?php echo $this->fetch('my_element'); ?>
This functions is working properly in my default layout but is returning an empty string if called from my admin layout.
I'm using admin routing prefixes.
Can you use this form:
<?php echo $this->element('my_element'); ?>

cakePHP v2.x - displaying content out of database field

Beginners question: Why is cakePHP not displaying the carriage return/new line and other characters when using;
<?php echo h($property['Property']['fullDesc']); ?>
I tried using
<?php echo $property['Property']['fullDesc']; ?>
but both showing output of text as one block of text instead of paragraphs.
Any help much appreciated.
The h function is just a wrapper for the php htmlspecialchars() function. It will not convert carriage returns into <br /> tags. You will need to do something like this:
echo nl2br(h($property['Property']['fullDesc']));
basic PHP...
<?php echo nl2br(h($property['Property']['fullDesc'])); ?>
nl2br() will form those newlines into <br>
Tip: you can make your bake templates include that automatically for all your textarea fields.
See http://www.dereuromark.de/2012/04/24/cake-bake-custom-templates-deluxe/

How should I handle forms in CakePHP?

I'm studying CakePHP. I read a CakePHP book, and web tutorials, but I still don't get some basic things:
I see people always create a form in View with $form->create. Can I use an HTML form like normal, or must do exactly like people do?
When a form is created in login.ctp with this code:
echo $form->create('User', array('method' => 'POST', 'action' => 'login'));
echo $form->input('email');
echo $form->input('password');
echo $form->input(array('type' => 'submit'));
echo $form->end('Login');
When I click the submit button, will the data be passed to the function login() in the Controller class?
Edited :
I tried this :
<?php
$this->Form->create("Test");
$this->Form->input("stuId",array('class'=>'inputField', 'placeholder'=>'SVxxxxxxxx'));
$this->Form->input("stuName",array('class'=>'inputField', 'name'=>'stuName'));
$this->Form->end();
?>
But it show nothing ? what is the problem :(
But it show nothing ? what is the problem :(
You have to use echo as in your first code snippet:
echo $this->Form->create("Test");
echo ...
You can use HTML for anything you want, but you'd be losing a big advantage of the CakePHP framework. The Cake HTML and form helpers help to future-proof your code. You also get the benefit of Cake's implementation of best practices in web coding. I fully recommend using those helpers.
The form data is passed to $this->request->data.
Yes, the parameters will be passed to login method.
I see $form being used in the form there, it appears you are using older version of cakephp (if $form has been instantiated with $this->Form then you are fine)
The FormHelper does lot of automagic for us and it also provides us means for added security.
I would reckon you to go with The Blog tutorial

Resources