Jump to content

Header problems


Destramic

Recommended Posts

Hey guys I include my header to my pages by adding this to the top of the page

$this->render ('myheader.html');

The problem is I want to pass results from my db to the navigation menu, which would require me to assign vars in each controller view that includes the header.

What is the best way to do this please?

Another way I can think off is assigning the results to my view in my bootstrap ready for the controllers...but I'm sure there is a more logical way.

Thank you

 

Link to comment
Share on other sites

I'm using my own framework that I built...I think it's pattern issues that I have.

OK so in a typical page I'll load the header.html and footer.html with my content in between...now my header has a menu and I want to add result counts from my database. My header is also rendered on every page.

Ie.  Tasks (5)

Just don't see a clear way of how this should be done.

How is this typically done in the real world?

Thank you

Link to comment
Share on other sites

Separate logic from presentation.

Do all your work before you output the header. That's when you get your result count. Then you render the header, passing values into it such as the page title (which you aren't already doing?) which the page can format however it wants.

$this->render('myheader.html', ['Title' => "Tasks ({$count})"]);

Your framework passes that second argument to myheader.html, which will probably have to turn into a PHP script, and it grabs the Title value.

Link to comment
Share on other sites

sorry requinix i think i may have no explained myself correcly, although you may understand what ive been trying to say. but i'll explain fully

heres my layout

 

tets.png

 

on the left navigation you will see a count of 5 for the number of tasks.

 

how my pages are put together

<?php echo $this->render('header.html'); ?>
login page
<?php echo $this->render('footer.html'); ?>

i could do what you said like so:

<?php echo $this->render('header.html' array('task_count' => $user_tasks_count)); ?>
login page
<?php echo $this->render('footer.html'); ?>

but this would require me to do this in every controller not just login

    public function login($task_id)
    {
        $user = $this->get_user();

	$this->render(array(
           'user_tasks_count' => $this->tasks->get_tasks_count($user->user_id());
        ));
    }

 

if this is how it should be done then so be it, just dont seem too practical...sorry to keep on.

 

ps. feel free to tell me to shut up and move on :)

 

thank you

 

Link to comment
Share on other sites

If you want it on every page then you should do it in the header...

If the problem is bridging the divide between the header (some sort of view) and the task count (which apparently requires a controller) then that's a framework question. What sorts of things can you do with it? What can you not do with it?

The way I would address this is by asking what the best way to do this should be, then do the work required to make that actually possible.

Link to comment
Share on other sites

i like the idea of loading a controller view inside of the view it's self. so here it is...

 

view method 

   public function load($controller, $variables = array())
    {
        $dispatcher = new Dispatcher($this->get_request(), $this->get_response());

        if (!$dispatcher->is_dispatchable($controller))
        {
            throw new Exception(sprintf('View: Unable to dispatch %s.', $controller));
        }
        
        if (!empty($variables))
        {
            self::$variables = array_merge(self::$variables, $this->escape_values($variables));
        }
        
        return $dispatcher->dispatch()->get_view()->get_contents();
    }

 

then in my pages

echo $this->load('template:common:header', array(
	'heading'             => 'Task',
    'heading_description' => 'This is a quick view of your current task'
));

 

thank you both for your help :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.