Structuring CakePHP Views - php

Long time reader, first time poster :)
I'm just embarking on my first Cake app so hopefully you guys can help me on my way.
First question is about extending/including views. I realise the way the layouts/view work is to prevent code having to be repeated, but I can't get my head around how to set up what I want to do without some repetition.
My page layout consists, apart from header and footer, a left nav bar which I want Controllers to add themselves to if appropriate, and a top nav bar which will be populated by appropriate pages within the current controller.
I tried creating a view block from within the controller but it didn't work, I'm a bit stumped.
Here's what I have:
My default layout includes the sidebar, currently just hardcoded, and the content:
Layout default.ctp
<!DOCTYPE html>
<html>
<head>.....</head>
<body>
...
<div id='leftnav'>
This is where I want my left nav
I want controllers to be able to add themselves
here.
</div>
<?php echo $this->fetch('content'); ?>
</body>
</html>
Then my /Customer/index view:
View index.ctp
<?php $this->extend('common'); ?>
<h1>Customers</h1>
.... do stuff with customers .....
Which extends my /Customer/common view to bring in the top nav bar, each view has to include this extend line, it would be nice not to have to if there's a different way of doing this.
At the moment, the links are just fixed but I'd like the controller to be able to create these options.
View common.ctp
<?php
echo $this->Html->Link('index', "index")." ";
echo $this->Html->Link('find', 'find')." ";
echo $this->Html->Link('add', 'add')." ";
echo $this->Html->Link('details', 'details');
echo $this->Session->flash();
echo $this->fetch('content');
?>
Appreciate your help cheers! :D

I think you should just be able to put those links in your layout file. But you may have to re-write them as "$this->Html->link("Index",array("controller => $controller","action" => "index");" etc.
To get the current controller within the layout file you can say "$controller = $this->params['controller']".

right, after searching around I think I found a good way of doing at least one of these things.
For the top nav, where the links will be populated by the controller, I'll pass an array from the controller to view. Then, instead of having an ->extend in every view I'll create an element to turn the array into a nav bar, and ->fetch this in the layout.
This leads me onto my next question....
How much code is ok in a CakePHP Layout?

Hi you can use Cake PHP HTML Helper
https://book.cakephp.org/3/en/views/helpers/html.html#creating-links
echo $this->Html->link('Users List', ['controller' => 'Users','action' => 'index']);
Output will be look like this:
Users List

Related

Add html div tag based on file name

Sorry if this is a nooby question, but I have the following problem:
I have some sort of a Documentation as a multiple webpages. I created a basic navigation bar on the left of the screen using a tutorial... To be able to mark the active link every page has its own unique navigation adding it's own link to a specific div class.
The problem is that if I wanted to add another page to my collection I would have to add the link in every html document.
I know that there is a possibility to include other pages Php code to have one navigation document, but I have no idea on how to achieve that the current page is getting highlighted.
I would appreciate any help.
If you want to have your navigation on a separate file it is completely possible and is actually preferred.
Use any of the following:
require
include
require_once
include_once
So let's say your navigation HTML is located at nav.php you would do something like:
<html>
<head><title>My Awesome Website</title></head>
<body>
<div id="nav-wrapper">
<?php
include_once("nav.php");
?>
</div>
<div id="contents">
<h1>Hello World!</h1>
</div>
</body>
</html>
If you update your navigation in the future you will only need to update one file (nav.php).
So if you want to make a new function please read this:
http://php.net/manual/en/functions.user-defined.php
There is full info how to create function.
Pass to function what page is active you can just simply in this way:
function menu($active)
{
echo $active;
}
menu($active);
It's hard to say more, because you didn't show us anything, a construction of HTML or PHP. Please read linked above tutorial. I think it will be enough in this stage of PHP learn.
Because of Grzegor J I found this Stackoverflow question which pretty much solves my problem.
Thanks to everyone for trying.

Make a self-aware menubar

So, In the website I'm currently designing (HTML5, PHP, JS/JQuery and Bootstrap), I've got a basic menubar at the top of the page. Just your normal
<ul>
<li>Home</li>
<li class="active">About</li>
<li>Players</li>
<li>Rules</li>
</ul>
Now, there's a lot more to this, such as a login button, etc, but basically it's adding a LOT of clutter to the top of my pages, and I was wondering if there would be any way to put it in a header.php file.
My issue is how I can use it in multiple webpages and still have the class="active" part. The only thing I thought of was making a function where it takes the page name as a string and go through each line and does if (the page is the same as the link) { echo the element with the class="active" } else { echo the element without the class }
Thanks!
You probably want to extract your header to header.php as you said, and then use the PHP include method.
<?php
include 'header.php';
?>
As far as selecting the 'active' class, you could pass and set an '$active' variable on each page. And then, since the included file inherits the scope from the page where it's included, you can get the variable and preform your logic in the header.php page.
http://php.net/manual/en/function.include.php
You can include another php file located anywhere on your server, and have the menu html in there, in a function as you said.
As for keeping the class="active", since you already have it working somewhere, presumably in your main php file, you can just move it to the new header php file that will be included and re-factor if necessary.
I don't know what your code looks like at all, if you post part of it that does the menu, me or someone else will probably be able to help more.

Codeigniter - Templating with separate templates

I wish to have a codeigniter application with 3 templates.
a template for displaying a login view, or an error view.
a template with header body navigation
a template with header body sidebar footer
now I can build the codeigniter application, but I can't find a simple template system to accomplish this task. There are many recommendations for libraries available, but they lack implementation details.
Suggestions and guidance would be appreciated.
You can user CodeIgniter Template.
http://williamsconcepts.com/ci/codeigniter/libraries/template/index.html
Download here the library and also you have a full documentation.
With this library, you can use more than one template, and you can manage it easy and separate in groups.
It's quite simple once you get your head around it.
You need to create a view file for each of the things you've listed above, so you'll want something like this (folder/file wise):
views/page-login.php
views/page-error.php
views/header.php
views/footer.php
Then from your controller you will load one of the page views.
Within the page view you can then load the elements you require, so header and footer with the page specific code between them.
So for example your login page could be:
<?php $this->load->view('header'); ?>
<h1>Login</h1>
<p>Please login to my website.</p>
<?php $this->load->view('footer'); ?>
This is fairly simple.
A quick example:
In your controller you can put
//a test variable
$data["foo"] = 'bar';
$data["page"] = 'a_page'; // this will make sure it loads the views/pages/a_page.php in your template
$this->load->view('templates/login',$data);
And in your views/template/login.php you can put:
<!-- your login template html -->
<html>
...
<!-- include the view you want inside your login template -->
<?php $this->load->view('pages/'.$page);?><!-- As you can see it loads /views/pages/a_page.php -->
<?php echo $foo;?> //This will echo Bar
</html>
or:
<?php $this->load->view('template/login_header');?>
<?php $this->load->view('pages/'.$page);?>
<?php $this->load->view('template/login_footer');?>
views/pages/a_page.php will also know $foo.
This loads another view (views/pages/a_page.php) in your template.
This way you can create all the templates you want, and include your view in those templates.
TIP: Handling headers & footers this way get's unmanageable pretty quickly. And it's still better to use template libraries. Try Phil sturgeon's library

Drupal: How to display block/region in views page?

I have a views page that contains a listing of one of my content type. I used views-view-list--<name of my view>.tpl to theme the page. However, the region/blocks that I defined are not displaying. In other pages it works fine, but on the views page it does not. I'm trying to display a user login block in my defined region.
Please tell me how to access my user login block or my region to display on my views.
Your help is greatly appreciated. I'm using drupal 6 by the way.
Best regards,
Think i ran into something similar yesterday. I was trying to print a region, for example
<?php print $footer ?>
but inside a tpl file that came from views - and for whatever reason, it doesn't output the region from one of the views tpl files.
I used this code:
<?php print theme('blocks', footer); // change "footer" to the name of your region ?>
As I am writing this, a safer and maybe recommended way in D7 would be:
<?php
$region = block_get_blocks_by_region('footer') //first define the block;
print render($region) // then print the block;
?>
I tried #Garry but I got some errors back from Drupal. Please see here
Hope this helps someone down the line.
Any page in drupal have page template based on url or content type . So you have to create right page template for your page where views page/block to be displayed. I'm assuming that you are using page template based on url
This worked for me, except the syntax above needs semi-colons after (); for example print render($region); otherwise thank you for this answer
This is how the snippet will work for Drupal 7 with Bootstrap - HTML tags, considering that the 'billboard' region host an ad:
<aside class="col-xs-0 col-sm-12 role="banner">
<?php
$region = block_get_blocks_by_region('billboard');
print render($region);
?>
</aside>

Pagination Not Displaying

I am a front-end designer/code trying to style up pagination for a web app. It is called in one section of the application and called it with the following:
<?php echo $pagination;?>
And I used CSS + the Pagination JS file to style it up.
In another section of the application, which I have just been asked to style, it is called with this:
<?php echo $pages;?>
And looks entirely unstyled. When I replace $pages with $pagination the page control no longer appears. When I run a search for $pages through our repository I see no matches for $pages (not even in the pagination file).
Anyone have any idea why pagination breaks the control?
Thanks!
EDIT: I did a really bad job of explaining this. The problem is that I can find a reference to the variable $paginate in the repository (in paginate.js) and I was able to assign a CSS class to it through that JavaScript file, but I can't find any references to $pages, which I just can't wrap my head around. If I change the variable to something else that I think is defined (such as $paginate) it no longer displays the pagination.
EDIT: Figured it out, it was in the controller in CodeIgniter.
Well, I'm not quite sure if I my answer will help, but as far as I undertood your question
When $pages is called, in the source don't you see the HTML tags that those pages are? Why don't you try not assign smth in javascript but make styles in your CSS according to those html tags that are generated?
for ex.
<div class="smth">
<div class="your_$page_div">
<ul>
<li>text</li>
etc...
In your CSS do smth like div.smth div.your_$page_div ul li {some stile}
so you won't have to replace anything and assign smth in core file.
I hope I correctly understood what you were asking :P Cheers!
I just saw you figured out the problem

Categories