How to modify $content_for_layout in CakePHP 1.3 - cakephp

I am using CakePHP 1.3. In app/views/layouts/default.ctp I am using this:
<?php echo $content_for_layout;?>
According to CakePHP Application Development, by Ahsanul Bari and Anupom Syam,
"This line is mainly responsible for placing controller-rendered view
contents inside the layout. We must include this to tell Cake where to
place the action-specific controller-rendered views."
I was trying to figure out how to modify the content of $content_for_layout. But I guess that is not the kind of variable that I am supposed to modify to modify the layout. What I am trying to accomplish is to either modify $content_for_layout or create a new layout to customize some elements for an A/B testing experiment. Could you help me to better understand how to manipulate $content_for_layout? Thank you.
UPDATE 1:
I am configuring an A/B testing experiment. I have this in app/views/layouts/default.ctp:
<body>
..........
<div id="split-test-homepage-content-original">
..........
<?php echo $content_for_layout;?>
..........
</div>
<div id="split-test-homepage-content-redesign">
..........
<?php echo $content_for_layout;?>
..........
</div/>
</body>
With CSS I am hiding/showing either split-test-homepage-content-original or split-test-homepage-content-redesign depending on the version that the split test will show. But I am having problems because $content_for_layout is being rendered twice. Even though visually people only see one version but behind the scenes the page has $content_for_layout loading twice on the same page. This is causing all kinds of problems to me such as duplicate forms, JavaScript elements not working correctly as a result of duplicate variables, etc. What I want to do is to modify what $content_for_layout does for <div id="split-test-homepage-content-redesign">. One option I am considering is to use JavaScript so that in the jQuery(document).ready(function($) { section I can do something in the experiment so that if a variable contains a specific boolean value, I show one version or the other. Something like this:
if(javascriptvariable==true){ then use the following HTML for the view:
<div id="split-test-homepage-content-original">
..........
<?php echo $content_for_layout;?>
..........
</div>
else, the Javascript will take care of not displaying the above, but this:
<div id="split-test-homepage-content-redesign">
..........
<?php echo $content_for_layout;?>
..........
</div/>
Does the JavaScript approach make sense to you or should I do something about creating new layouts or modifying what $content_for_layout does? Thank you.

Duplicating content was definitely asking for trouble. I used a much better approach to manipulate CSS classes and ids only without duplicating content.

Related

What's the advantage(s) of setting a model value in the controller as opposed to in a template in AngularJS?

I'm learning AngularJS and am currently at this stage of it's tutorial. Full code available here on Github.
There's a component which has a controller with the following function:
self.setImage = function setImage(imageUrl) {
self.mainImageUrl = imageUrl;
};
which is really a method used in the Click event-handler for a img element:
<img ng-src="{{$ctrl.mainImageUrl}}" class="phone" />
...
<ul class="phone-thumbs">
<li ng-repeat="img in $ctrl.phone.images">
<img ng-src="{{img}}" ng-click="$ctrl.setImage(img)" />
</li>
</ul>
...
So what are the advantages to handling the event as above as opposed to:
<img ng-src="{{img}}" ng-click="$ctrl.mainImageUrl = img" />
?
Does it have to do with keeping logic-handling out of the View? Any other considerations?
Yes basically, it's just for separation of concerns.
This is somewhat of an edge case situation as the code you need to execute is just one line. In this case it really is just a matter of opinion, some people prefer in the HTML some people don't.
Obviously if you write the Javascript code in the .js file you get intellisense and syntax highlighting and stuff like that, so easier to spot errors. + Your code can be linted. :-)
Also I'd recommend learning Angular 2 instead of 1. Angular 1 is redundant now.
In general the HTML in Angular is considered the "View" or the "Template". So there are at least two principles at work here:
In an MVC approach you want your Views to be "dumb". Basically, this
is a crude way of saying don't put logic into your Views.
From a web development stand point, the template is the declarative
part. So, your HTML should be declarative and not functional.
Finally, another reason is that keeping functional code in the controller increases the readability of your code. Other developers know to look for logic in the controller and don't need to hunt in the View when there is a bug in the logic.
All that being said, I do break those rules from time to time for convenience.

Best way of use of ajax in cakephp?

I am using ajax in my cakephp application by using JS helper.so I do not required to write jquery code.
Js helper automatically add the code in my file.Following is the line by which JS helper write the code.
echo $this->Js->writeBuffer(array('cache'=>true));
When I set the true value of cache attribute,every time a new js file created in js folder and a new script is added in my code in following manner.
<script type="text/javascript" src="filename.js"></script>
But when I change the cache value to false,all the js code added in my file line by line.
Now my question is, which way is best way by which,page execution fast.
And my second question is that when I set true value of the cache, js file add only once, right now js helper add the js file again and again,when page is reload or refresh.
For the record this question could have been worded far better.
First question, execution speed: Saving a new file for every bit of JS probably isn't the way to go. Hopefully the code below should explain a better way and render your second question moot.
// In /app/View/Layouts/default.ctp
...
<head>
...
<?php echo $this->fetch('script'); ?>
...
</head>
<body>
...
<?php echo $this->Js->writeBuffer();
...
</body>
Once that's in place, you can add a script file to your views or layouts with
<?php echo $this->Html->script('scriptname', array('inline'=>false)); ?>
(note no .js extension)
Or add custom Js:
<?php
$customJs = "alert('Hi!');";
echo $this->Html->scriptBlock($customJs, array('inline'=>false));
?>
Hope that helps.

element is rendered without layout

I have books/index controller and view.
The view looks like this:
abcdef
123445
<?php echo $this->element('categorytree'); ?>
xyz
999
I have elements/categorytree element:
<?php
function my_function() { echo '123'; }
echo 'element is here!';
?>
And when I go to books/index in my browser, the element is rendered without layout so source output is:
abcdef
123445
element is here!
xyz
999
And when I remove function declaration in element the problem disappears - the whole layout is rendered - so the problem is when I declare any php function inside element, then if this element is "fetched" by any view only this element is rendered without a layout.
This problem appeared suddenly, I don;t know why...
Before I used functions declared in element and didnt have any problem...
------------------ solved
Wow this is strange - the problem was caused by the <!-- --> tags after the $this->element()...
Although you've found the solution to your problem, I'd just like to point out that defining functions in your view is a horrible practise. A better and cleaner way to do it is to create a custom helper, and simply use that.
A view is simply not the place to write functions or classes.

Drupal render() forcing other divs closed?

I'm new to Drupal theming and am attempting to create a custom theme template for a specific content type I have.
I am attempting the following:
<div<?php print $content_attributes; ?>>
<?php print render($content['field_property_media']); ?>
<div class="field-label-above property-information">
<div class="field-label">Property Information</div>
<?php print render($content['address']); ?>
</div>
</div>
However, when the content actually render the address portion gets booted out of its parent div and looks like this:
<div class="field-label-above property-information">
<div class="field-label">Property Information</div>
</div>
<div class="field field-name-field-property-address field-type-addressfield field-label-inline clearfix"><div class="field-label">Address: </div><div class="field-items"><div class="field-item even"><div class="street-block"><div class="thoroughfare">55 Duchess Ave</div></div><div class="addressfield-container-inline locality-block"><span class="locality">London</span> <span class="state">Ontario</span> <span class="postal-code">N6C 1N3</span></div><span class="country">Canada</span></div></div></div>
I can't figure out why this is happening or what the solution is to keep the address within the div I manually created.
Thoughts?
The only possible way this could happen is if you've got a custom or contributed module intercepting your template file and changing it, or if you have some javascript enabled that's moving the <div> out of it's container. Drupal does absolutely no re-ordering of content when it processes a template file so it can't be anything in Drupal core that's causing the problem.
If the <div> is just displaying outside the container (i.e. it's inside when you inspect the source code) then you're probably just facing a floating issue; just add the clearfix class to the containing element.
EDIT
Just a thought, have you cleared your cache since you added the template file? If not, do that, it won't be picked up until the caches are cleared.
Also if this is a custom node template (i.e. node--page.tpl.php), make sure you've also copied node.tpl.php to your theme's folder. Then clear your cache again.

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