Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 674 Vote(s) - 3.49 Average
  • 1
  • 2
  • 3
  • 4
  • 5
PHP Frameworks - Layout Dynamic Menu

#1
I am developing a website in PHP and I would like to use a mvc framework to do this as I want to gain experience with PHP frameworks.

I have looked at Zend, CakePHP, and CodeIgniter and I have not been able to find an intuitive way to accomplish this.

What I need to do is create a layout that will be for example:

<html>
<head>
<!--scripts go here-->
</head>
<body>
<div id='banner'></div>
<div id='menu'><?php $this->layout()->menu ?></div>
<div id='container'><?php $this->layout()->content ?></div>
<div id='ads'><?php $this->layout()->ads ?>
<div id='footer'>blah</div>
</body>
</html>

I am looking for a framework that could do this simply without a lot of hacks as this should be simple to accomplish from my perspective.

This would pull the menu, content, and ads from separate controllers and views and would of course be dynamic content. I do not want to put the menu code into every view...

Maybe this is simple and I am just going about it the wrong way?

Any help is appreciated.

Thank you,<br>
-Ben
Reply

#2
You're probably not looking for a framework so much as a templating engine. Try [Smarty][1].


[1]:

[To see links please register here]

Reply

#3
Smarty or [Tempalte lite][1]


[1]:

[To see links please register here]

Reply

#4
Each of the PHP libraries you have mentioned have templating and can accomplish what you need.

Pick a library and get to grips with it. It will pay off over time. Templating engines like Smarty are good, but a full framework offers a lot more.
Reply

#5
In Cake you could put this in an element that you just include via

echo $this->element('menu');

You can make this element as complicated or simple as you like. If you need it to automatically highlight in which section of the page you are, there are several methods. For example, set a variable in each Controller/Action that the element can pick up on.

// Controller
$this->set('currentNode', 'homepage');

// Menu Element
if ($currentNode == 'homepage') {
// add class 'selected' to menu item, or something like this
}

Depending on how complicated your application is, you could also keep it completely self-contained.

// Menu Element
if ($this->controller == 'home') {
// highlight this menu item, add extra sub-items, re-calibrate flux capacitor
}

The point being, there are certainly ways to do this, but it depends on the framework you use and what exactly you need to accomplish. It should be rather trivial to do in any framework, and your choice of framework/templating engine should not depend on this little feature.
Reply

#6
In Zend Framework (which is the only one I know of those you mentioned) there are several ways to accomplish this. I prefer the Helper way.
Your layout will look:

<html>
<head>
<!--scripts go here-->
</head>
<body>
<div id='banner'></div>
<div id='menu'><?php $this->menu() ?></div>
<div id='container'><?php $this->layout()->content ?></div>
<div id='ads'><?php $this->ads() ?>
<div id='footer'>blah</div>
</body>
</html>

And you will have two view Helpers:

class Zend_View_Helper_Menu{
public function menu(){
echo "<html.........>";
}
}

AND

class Zend_View_Helper_Ads{
public function ads(){
echo "<html.........>";
}
}




There is also a way which will result in exactly the syntax you gave. Not familiar with this way, [try this link][1]


[1]:

[To see links please register here]

Reply

#7
Symfony can do what you are looking for using a mix of concepts.

* Layout - This is the main structure used to decorate a page.
* Template - This is the main view attached to a URL by the controller.
* Fragments - Lightweight and uses data you pass to it.
* Component - Used if you need access to the model, the code is split between presentation and logic.
* Slot - used to replace a defined section of the layout.

In your case the layout would have the main content included using the template logic which is the core of the view layer and the other items would be either fragments or components depending on how much of the model they would need to access.

The [Symfony documentation][1] has a full explanation of this.

[1]:

[To see links please register here]

Reply

#8
In Zend Framework you can create individual actions and views for each section of your layout by setting the ViewRenderer helper's responseSegment property like so:

class IndexController extends Zend_Controller_Action {

public function menuAction() {

//menu code goes here

$this->_helper->viewRenderer->setResponseSegment('menu');
}

public function adsAction() {

//ads code goes here

$this->_helper->viewRenderer->setResponseSegment('ads');
}
}

etc...

You can then call them in the layout file exactly the way you specified:

<?= $this->layout()->menu; ?>
<?= $this->layout()->ads; ?>
Reply

#9
It's easy in DooPHP framework, just use
`<!-- include 'templatefilename' -->`
in your view templates. The view part of the framework is very flexible. There's a template engine demo on the site

[To see links please register here]

Reply

#10
Actually, what you want to achieve here can be done with very little deviation from what you have already, using Zend Framework.

For the menu, you can use `Zend_Navigation`, which allows you to define the tree of the navigation of your site, and create simple menus. I find that populating the `Zend_Navigation` container in a Front Controller plugin allows you to cache the object easily, so you have little performance worries from traversing your sites tree,

For the ads, you simply use the placeholder view helper, and you can once again use a Front Controller plugin to populate this. Using a plugin has the advantage that the logic of counting impressions and rotating ads is kept seperate from your actions, and easily performs its task across every action.

dustin.cassiday's method of using the action stack is risky, and can lead to massive headaches debugging your apps. and Itay Moav's method is now really redundant due to `Zend_Navigation`
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through