Jump to content
Destramic

Header problems

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

 

Share this post


Link to post
Share on other sites

Depends on your framework.

An easy solution is to allow for an array as a second argument to render(), perhaps $args, which the file could read from.

Share this post


Link to post
Share on other sites

That is possible with my view when rendering but still I would need to pass vars on each page which seems unlogical and long winded

Preloading vars in my bootstrap may be the best way to do this then in my case.

Thank you

Share this post


Link to post
Share on other sites

What kinds of "vars" are you talking about? When do you know their values? When do they change?

Share this post


Link to post
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

Share this post


Link to post
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.

Share this post


Link to post
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

 

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

a controller for the header sounds good, but leaves me with the question how would i run two controllers or load the header controller into other controllers.

what would be the best pattern/way please requinix?

 

thank you for your patience

Share this post


Link to post
Share on other sites

1) Let the view do a sub-request to the header controller to load the menu.

2) Create a view helper that loads the menu.

Both will require the current route to determine which hierarchy to load/show.

Share this post


Link to post
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 :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

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.