Context - What is $context: Difference between revisions From Online Manual

Jump to: navigation, search
m (2 revisions: Importing from local.)
No edit summary
 
(7 intermediate revisions by 5 users not shown)
Line 1: Line 1:
Context is basically, well, the context of this page view. In other words, it's what makes one view different from the next or last. Maybe there are more or less boards... maybe someone posted a new topic... these are all things the template context can tell you. The same context information isn't available everywhere. For example, the list of boards you can move a [[Topic]] to isn't there when you are editing your [[Profile]] because that's illogical.  
Context is basically, well, the context of this page view. In other words, it's what makes one view different from the next or last. Maybe there are more or less boards... maybe someone posted a new topic... these are all things the template context can tell you. The same context information isn't available everywhere. For example, the list of boards you can move a [[Topic|topic]] to isn't there when you are editing your [[Profile|profile]] because that's illogical.


'''Next:''' [[http://docs.simplemachines.org/index.php?topic=220|" class="bbc_link">What is the difference between settings and options?]]
== Using $context ==
$context is used when you want to store data to be displayed in a template, or when you need to carry data from one function to another (though [http://php.net/manual/en/functions.arguments.php parameters] should be used where possible instead).
 
Before using $context, you need to add it as a global to your function.
 
== Examples ==
<code>function addOne()
{
// Before we can use $context, we need to global it.
global $context;
// Check if the key already exists in $context. If it does not, create it.
if (empty($context['number']))
$context['number'] = 0;
// Then add one to the key in $context.
++$context['number'];
}</code>
 
[[Category:FAQ]]
[[Category:Developing SMF]]
[[Category:Customizing SMF]]
[[Category:SMF Themes]]

Latest revision as of 15:56, 22 May 2013

Context is basically, well, the context of this page view. In other words, it's what makes one view different from the next or last. Maybe there are more or less boards... maybe someone posted a new topic... these are all things the template context can tell you. The same context information isn't available everywhere. For example, the list of boards you can move a topic to isn't there when you are editing your profile because that's illogical.

Using $context

$context is used when you want to store data to be displayed in a template, or when you need to carry data from one function to another (though parameters should be used where possible instead).

Before using $context, you need to add it as a global to your function.

Examples

function addOne() { // Before we can use $context, we need to global it. global $context;

// Check if the key already exists in $context. If it does not, create it. if (empty($context['number'])) $context['number'] = 0;

// Then add one to the key in $context. ++$context['number']; }



Advertisement: