How to set custom variables?

Discussion forum for MOD Writers regarding MOD Development.
Locked
-ToM-
Registered User
Posts: 11
Joined: Fri Feb 08, 2008 4:12 pm

How to set custom variables?

Post by -ToM- » Fri Feb 08, 2008 4:17 pm

Hello, I am working on a basic modification that adds a small box to the top of a page.

I have it all sorted, and it works fine - but the problem is - I have to either set the message to show it, in ftp or use the forum description.

I wanted to create a variable like {SITE_DESCRIPTION}, but call it {LITTLE_BOX}. How could I do this?

Thanks, Tom.

User avatar
thecoalman
Community Team Member
Community Team Member
Posts: 3348
Joined: Wed Dec 22, 2004 3:52 am
Location: Pennsylvania, U.S.A.
Contact:

Re: How to set custom variables?

Post by thecoalman » Sat Feb 09, 2008 6:12 am

You can't just add it to the template? Anyhow....

The template variables are assigend in arrays, the largest one used in every page can be found in includes/functions.php Scroll down near the bottom and you'll find:

Code: Select all

    // The following assigns all _common_ variables that may be used at any point in a template.
    $template->assign_vars(array(
        'SITENAME'                        => $config['sitename'],
    
So if you inserted a value like this:

Code: Select all

        'MY_VARIABLE'                        => 'my value',    
It would be parsed in any template, "my value" would appear any where in the template you use {MY_VARIABLE}

To further explain lets say we want to set a switch just for index.php so {MY_VARIABLE only appears when index.php is viewed, open index.php with your editor and down near the bottom you'll find:

Code: Select all

    
// Assign index specific vars
$template->assign_vars(array(
    'TOTAL_POSTS'    => sprintf($user->lang[$l_total_post_s], $total_posts),
    'TOTAL_TOPICS'    => sprintf($user->lang[$l_total_topic_s], $total_topics),
    
We add a custom variable:

Code: Select all

    
// Assign index specific vars
$template->assign_vars(array(
     'IS_IN_INDEX'        => true,
    'TOTAL_POSTS'    => sprintf($user->lang[$l_total_post_s], $total_posts),
    'TOTAL_TOPICS'    => sprintf($user->lang[$l_total_topic_s], $total_topics),
    
In overall header we add:

Code: Select all

<!-- IF IS_IN_INDEX -->
{MY_VARIABLE}
<!-- ENDIF -->
Now when someone loads a page "my value" will only appear in index.php.

Note that I've probably broken every coding guideline here and there is probably a phpbb specific rules how it should be done but that's the gist of it.
“Results! Why, man, I have gotten a lot of results! I have found several thousand things that won’t work.”

Attributed - Thomas Edison

Locked

Return to “[3.0.x] MOD Writers Discussion”