How can I setup a dynamic gallery on a Layout page? - php

Here's the layout at the moment:
<header>
<navigation>
<section id="<?= $this->id; ?>">
<?= $content; ?>
</section>
<footer>
The page layout however (here called main.php), has the following hierarchy:
[header]
[dynamic gallery of images depending on db record id field] !!!
[navigation]
[content]
[footer]
Navigation is equal on all pages, so they should be on the layout, correct?
The dynamic gallery wrapper is equal on all pages, so it should be on the layout, correct?
But the images themselves, they should be different depending on the database records.
I don't even know where to start here.
Can I have a little push please?
UPDATE
Should I create a method inside components/controller.php or mycontroller or whatever we may have, that retrieves the value from the model, and make that value available as a public parameter to be used on the layout?
Does this makes sense to you?

As you correctly know, You must put your non-changing views into your main layout. But about views which will change during the request, You must create a different view for each one. Then, depend on your request you can renderPartial() your views.
Say, Your image view.
First, We create a view with name _image.php in our layout directory (or any directory).
Second we fetch the data from database. for example we have images data like below:
$images=array('test1.jpg','test2.jpg',...);
Third, In each request you must send your data to your renderPartial method:
$this->renderPartial('//layout/_image.php',array('images'=>$images));
and in your _image.php you can manipulate your data, which is always different in different requests.
There are another ways to do this:
Using the Yii's DECORATOR feature which is really cool. You can google it
Using Yii's CLIPS feature
UPDATE
In this case, using Yii's Decorator worked for questioner.

Related

Where to place common code in Phalcon-based site that is called each page load

I have code that I want to run on every page load, such as looking up menu items, looking up the users details etc. These will be displayed on partial views that make up the main view.
Where do I place this code so that it can fill my partial views with each page load? I know I can just add the code to the top of the partial view itself, but this doesn't really follow the MVC pattern.
Is there a function that is always called that I can hook into in my base controller?
You can create a base viewmodel for the repeated code and make other viewmodels inherit from it.
...such as looking up menu items, looking up the users details etc
You're a bit unclear about the type of information you want to load: in case the info is a view-component then indeed you should create a base-view and inherit from it or include it (composition) in any other view.
But, in case it is "user-information" - the data should live in a model-component that again, may live as "base-model" object that is included in other model components.

Include A View Of Another Controller Within Views/site/index

I can't figure out how to solve this problem i'm facing. Alright, so basically i have a default view/site/index.php view file which is generating the home page. Now i have built two separated Models and their associated Controllers and views. I want to include those view of the controllers in the default home page view i.e. views/site/index.php. Does it make any sense ?
So my home page is divided into several sections. The header and footer parts are the same throughout the whole site, which is why i've written them in the /themes/brushed/layouts folder under separate files. Those are being loaded fine.
The two controller i was talking about are named as FrontPageCategoriesController.php and TopProductsController.php. They are generating their separate view files i.e. index.php in their respective view folders.
How will i include those view files (index.php) into the main site view file (views/site/index.php). I just can't include those files using include_once() because the data is being generated yet by its controller. So whats the way to achieve this ?
Can anyone please help me out with this. Thanks in advance.
First you should move data generation from controller to model. Controller should only wire your input parameters to model and pass it to view. The reason is that you want to reuse models, not controllers. Remember that controller is bound to page request, while model is not.
Then just use your model to fetch the data and renderPartial to render the data. For example:
$categories = FrontPageCategories::model()->findAll();
$this->renderPartial('/frontPageCategories/index', array(
'categories' => $categories,
));
In fact it is possible to fetch categories from model directly in view, if they are not dependant on some parameters in the current page.
Actually, you should use renderPartial for your header and footer too.

Codeigniter multiple views in one view

I am working on a web application. This might be a silly question, but I want to know whether I am taking the good approach to this or not.
I want to have multiple views on one single view/page.
The Codeigniter documentation says that "A view is simply a web page, or a page fragment, like a header, footer, sidebar, ...".
I want to have a header, some quick search view, some other view and a footer for a example. Should I implement a controller to every view (header, quick search, footer, ...), or is it better to implement every view functions in a single controller? For instance, if I know that the header, footer, quick search views are going to always be there (even if their content might change) should I put functions for all those views in one controller?
Please, help.
one approach is to have a template view that has the elements you require.
see this pseudo-code example...
so, template_view.php has:
$this->load->view('header',$header);
$this->load->view('quicksearch',$quickssearch);
$this->load->view('body',$body);
$this->load->view('footer',$footer);
your single controller then calls the template with the parameters for each view.
$data = new stdClass();
$data->header = ....
$data->quickssearch = ....
$data->body = .....
$data->footer = .....
$this->load->view('template_view',$data);

Menu and all pages content from database

I'm writing simple website with some cms functions. It would be good to have all menu items and pages contents held in database.
I have table with id, name, parent_id and a content field. In future I would maybe move content to a content table to have multiple contents to menu item with fk. But it is not the case here.
The question is:
Do I need the URL field in menu table?
What else do i need to get it to work? Should every page have its own controller? I,m a beginner with zend framework, so please give me some directives. Thanks in advance.
Lets start with Question 2: every page does not need its own controller. If your pages are static you can even load every page using a single action. For more dynamic processing you could use a separate action for each page.
In any case, make sure you structure your code into controllers and actions in a way that makes sense. For example, inside your CMS a user might edit, create or delete a post. You could then create a PostController inside which you write an editAction, createAction and deleteAction.
You could store the URL in the table, but you do not necessarily have to.
Single action approach (mostly for static content)
Make sure the page id or name is stored in a GET param. You could then use the following code:
public function genericpageAction()
{
$thePageID = $this->_request->getParam('id');
// fetch the page content from the db based on $thePageID
// and pass it to the view
}
Of course, here, you could also match against the URL stored in the table if you chose that approach.
Multiple action approach (for more dynamic processing, most likely what you want with a CMS)
You could define a route for each page and load its content in the respective action. For example, for the page to edit a post:
class MyCMS_PostController extends Zend_Controller_Action
{
public function editAction()
{
// fetch the home page content
// do any further processing if necessary
}
}

Rendering layout parts in Zend Framework

My app is supposed to work like this. First, the default Action of the Default controller (according to URL) does it's job. For the purpose of this question lets name it MyController and indexAction.
Then it forwards ($this->_forward) to my LayoutController menuAction which renders navigation part (menu from database) and forwards to footerAction, which then renders footer from database (things which are in db but must be visible in every single page).
Finally /views/scripts/my/index.phtml is rendered in the layout with $this->layout()->content.
Question is, how shall I make other parts of layout rendered in their appropriate places? Do I need additional files menu.phtml and footer.phtml which somehow would be rendered by LayoutController menuAction and footerAction and somehow inserted in the appropriate places inside layout.phtml? Or can I have just one layout.phtml script with all the html inside, and menuAction / footerAction just provides the text from DB to be inserted?
I'm interested in good programming practice side as well as technical "how to" if you can.
I think you should consider to use just one layout.phtml as your template. So your default action will render not the contents of the layout but the content of the page itself. For example, in a blog application, you'll have the sidebar and the footer, also you will have the post content. Your ->viewPostAction() will render the post, and a plugin with ->postDispatch() method will render the layout contents in every page.
You can take as basis this question: how to call multiple controller action from within any action in ZF?

Categories