Are controller variables auto-passed to views?
Permalink
Hi There,
Is there a way to grab variables calculated or set in the controller to a view?
e.g. output Facebook's open graph tags using the variables directly from header_required.php like this
After largely learning 5.6 via these forums, I'm used to repeating logic found in header_required.php at the top of templates like this
Is there a way to get controllers to automatically pass variables to views like elements, page types, block templates etc?
And if so, how to you grab variables from the controller?
Hope that makes sense!
Cheers
Ben
Is there a way to grab variables calculated or set in the controller to a view?
e.g. output Facebook's open graph tags using the variables directly from header_required.php like this
<meta property="og:site_name" content="<?= $siteName; ?>" /> <meta property="og:title" content="<?= sprintf('%2$s – %1$s', $siteName, $pageTitle); ?>" /> <meta property="og:url" content="<?= $pageUrl; ?>" /> <meta property="og:type" content="article" /> <meta property="og:description" content="<?= $pageDescription; ?>" /> <? if ($pageImage) : ?> <meta property="og:image" content="<?= $pageImage; ?>" /> <? endif; ?>
After largely learning 5.6 via these forums, I'm used to repeating logic found in header_required.php at the top of templates like this
// Page description for SEO, microformats, social sharing if($c->getCollectionAttributeValue('meta_description')) { $pageDesc = $c->getCollectionAttributeValue('meta_description'); } else { $pageDesc = $c->getCollectionDescription(); } $pageDescription = htmlspecialchars($pageDesc, ENT_COMPAT, APP_CHARSET);
Is there a way to get controllers to automatically pass variables to views like elements, page types, block templates etc?
And if so, how to you grab variables from the controller?
Hope that makes sense!
Cheers
Ben
Unfortunately nothing is automatically sent through. You have to pass variables into an elements file with the variables array being the last variable in the call. You have to pass variables from the controller to the view with $this->set('variableName', $variableValue);
Thanks heaps for the reply,
So in practical terms, does that mean you override all core controllers (you're using) in order to set variables and pass them to views?
And do you have to do it for each variable individually or can you do something like...
And if you have to override core controllers, is there a best practice for handling c5 updates? i.e. does your override pull in the core controller first, then set the variables? Something like...
And do you have to do anything in the view to receive the variables?
Sorry for all the pseudo code! Just trying to wrap my head around how best to approach it.
Cheers
Ben
So in practical terms, does that mean you override all core controllers (you're using) in order to set variables and pass them to views?
And do you have to do it for each variable individually or can you do something like...
$this->set => array( 'variableName', $variableValue, 'variableName', $variableValue );
And if you have to override core controllers, is there a best practice for handling c5 updates? i.e. does your override pull in the core controller first, then set the variables? Something like...
<?php $this->inc('path/to/core/controller.php'); $this->set => array( 'variableName', $variableValue, 'variableName', $variableValue );
And do you have to do anything in the view to receive the variables?
Sorry for all the pseudo code! Just trying to wrap my head around how best to approach it.
Cheers
Ben
As far as I know you have to set them individually, you don't have to do anything to receive them. What core controllers are you thinking you would need to override?
Thanks for the reply,
I'd be looking to override header, footer, page types and blocks - I guess any controller that already calculates variables I'm using in a template.
Do you override controllers to set variables for views or do you just add the logic to the template file?
Cheers
Ben
I'd be looking to override header, footer, page types and blocks - I guess any controller that already calculates variables I'm using in a template.
Do you override controllers to set variables for views or do you just add the logic to the template file?
Cheers
Ben
I guess I don't quite understand what you're trying to do. But you should be able to just copy the original file from the concrete directory out to the application directory and make whatever adjustments you need to.
Thanks for that,
I'm trying to pull controller variables into templates (like elements, pages, block templates etc).
At the moment I repeat/recalculate many of the same variables found in controllers, at the top of templates which seems inefficient.
Sorry, I'm obviously confused. I guess I don't understand why we have to repeat logic that's already in a controller somewhere.
Hope that makes sense.
Cheers
I'm trying to pull controller variables into templates (like elements, pages, block templates etc).
At the moment I repeat/recalculate many of the same variables found in controllers, at the top of templates which seems inefficient.
Sorry, I'm obviously confused. I guess I don't understand why we have to repeat logic that's already in a controller somewhere.
Hope that makes sense.
Cheers
Ok, so say you have a page called test. In your controller you have something like
Then in your page type or your single page view you can just do
Then in your header or your element_file you can use that variable too. Does that help at all?
$c = Page->getCurrentPage(); $attribute_value = $c->getAttribute('attribute_handle'); $this->set('attribute_value', $attribute_value);
Then in your page type or your single page view you can just do
Then in your header or your element_file you can use that variable too. Does that help at all?
I don't really understand sorry, here's specific example.
So that I can output Facebook open graph tags, microformats and twitter stuff in header.php, I want header.php to have access to the following variables being set in header_required.php
Based on my limited understanding, there are two options...
1. Override header_required.php and set each variable for use in header.php
2. Duplicate logic from header_required.php and add it to header.php
But #1 creates a potentially painful update scenario when overridden core files change because of a C5 update and #2 duplicates logic that already exists.
So here's the question: Is there a simpler option that loads header_required.php's variables into a theme's header.php?
And if so, could the same technique be used in a block template?
If not, I'll continue duplicating logic but I wondered if there were less painful options.
Cheers
Ben
So that I can output Facebook open graph tags, microformats and twitter stuff in header.php, I want header.php to have access to the following variables being set in header_required.php
$site $pageTitle $defaultPageTitle $pageDescription $metaTags $linkTags $alternateHreflangTags
Based on my limited understanding, there are two options...
1. Override header_required.php and set each variable for use in header.php
2. Duplicate logic from header_required.php and add it to header.php
But #1 creates a potentially painful update scenario when overridden core files change because of a C5 update and #2 duplicates logic that already exists.
So here's the question: Is there a simpler option that loads header_required.php's variables into a theme's header.php?
And if so, could the same technique be used in a block template?
If not, I'll continue duplicating logic but I wondered if there were less painful options.
Cheers
Ben
I understand what you're saying an unfortunately no, you can't pass things out of an elements file. You'll have to duplicate that logic. I thought you were talking about logic that was done in a controller and being passed into an elements file.
Sorry, I thought header_required.php was a controller