Simple Machines Community Forum

Customizing SMF => SMF Coding Discussion => Topic started by: nvcnvn on November 17, 2010, 08:06:18 AM

Title: How to add a context var!?
Post by: nvcnvn on November 17, 2010, 08:06:18 AM
Hi you! I'm try to design a Mod wich need to use some global var!

I trying to add these value into a context var so I can use them any where!

The question is how can I create a context var!?

Please help me! Thanks!
Title: Re: How to add a context var!?
Post by: All Colours Sam on November 17, 2010, 08:10:43 AM
$context['my_value'] =  $my_variable   that will work,  then you could use $context['my_value']  everywhere  :)
Title: Re: How to add a context var!?
Post by: nvcnvn on November 17, 2010, 09:14:24 PM
Quote from: Miss All Sunday on November 17, 2010, 08:10:43 AM
$context['my_value'] =  $my_variable   that will work,  then you could use $context['my_value']  everywhere  :)

Do I need to add this in a specific file!? Or any file!? (My own filw with a required SSI.php)!?
Title: Re: How to add a context var!?
Post by: All Colours Sam on November 18, 2010, 10:49:59 AM
as far as I know you can do this in any file inside SMF or out side by calling SSI.php
Title: Re: How to add a context var!?
Post by: IchBin™ on November 18, 2010, 02:23:26 PM
But it also depends on when you want that context variable to be available. For example, ou put it in Boardindex.php it will only be available on the board index page.
Title: Re: How to add a context var!?
Post by: nvcnvn on November 19, 2010, 11:10:09 PM
Thanks, that help! Beacuse I have a small proplrem like this:
in test.php there is some line read the content from a text file, put it in a var! and a function use this var as a global var!
if I run test.php in the test2.php (test2.php require SSI.php test.php), ok. But I try require test.php in BoardIndesm.template.php and it does not work!!

Test.php

<?
$var_name = file_get_content(home/nvcnvn/smf/'mytext.txt');
function printtext(){
global $var_name;
echo $var_name;
}
?>


test2.php

<?
require('SSI.php');
//When I do the same in BoardIndex.template.php It's not work!
require('test.php');
printtext();
?>
Title: Re: How to add a context var!?
Post by: IchBin™ on November 20, 2010, 12:38:24 AM
When you include the file in your boardindex file you should probably use the full path to the php files. I don't know off the top of my head what the correct path would be, but using the full path should work.

require('/home/path/to/test.php');
Title: Re: How to add a context var!?
Post by: nvcnvn on November 20, 2010, 06:29:29 AM
yes, i have tried like this! But....for an example, in BoardIndex.php (I put these line in the main function):

require('home/path/test2.php')
echo $var_name;
printtext();


if all the thing ok, I mus see two more line (because printext is simply echo $var_name out) but in my case, I just get 1!!
Title: Re: How to add a context var!?
Post by: IchBin™ on November 20, 2010, 01:51:21 PM
That is a relative path you have shown. The full path ALWAYS starts with a forward slash.
Title: Re: How to add a context var!?
Post by: nvcnvn on November 21, 2010, 03:39:48 AM
Quote from: IchBin™ on November 20, 2010, 01:51:21 PM
That is a relative path you have shown. The full path ALWAYS starts with a forward slash.

Yes, I know!!!
But if my path is wrong, how can I still get the #1 result!?
Title: Re: How to add a context var!?
Post by: IchBin™ on November 22, 2010, 01:02:23 PM
Can you post the code you have now? The code you posted above has an error:

This: $var_name = file_get_content(home/nvcnvn/smf/'mytext.txt');

Should be this: $var_name = file_get_content('/home/nvcnvn/smf/mytext.txt');
Title: Re: How to add a context var!?
Post by: nvcnvn on November 23, 2010, 07:40:33 AM
Can I send you my file and you can test it for me!

My coding a mod for SMF to, when I get over this error I hope I can submit it to SMF :D
Title: Re: How to add a context var!?
Post by: IchBin™ on November 23, 2010, 02:13:11 PM
Sorry, I've got too many things on my todo list to spend the time testing for you. I don't mind stopping and looking at some code if you want to post it. Otherwise, someone else will have to help.
Title: Re: How to add a context var!?
Post by: nvcnvn on November 24, 2010, 05:03:40 AM
OK, this is my file:
test.php

<?php
$textlic
= file_get_contents('/home/nvcnvn/license.txt'); //replace this text with your FULL path to liceense.txt

function textlicshow(){
global
$textlic;
echo
$textlic;
echo
'ZZZZZZZZZZZZZZZZZZZZZ';
}
textlicshow();
?>

run the file alone and make sure it print out the content of license.txt

After that, with the Index.template.php, I have add these code after <body> tag

require('/home/nvcnvn/test.php');


After do ing that, I though I would get the content of license.txt but all I get is ZZZZZZZZZZZZZZ
Title: Re: How to add a context var!?
Post by: All Colours Sam on November 24, 2010, 11:09:20 AM
why not include $textlic = file_get_contents('/home/nvcnvn/license.txt');  inside the function?
Title: Re: How to add a context var!?
Post by: nvcnvn on November 24, 2010, 08:26:03 PM
Quote from: Miss All Sunday on November 24, 2010, 11:09:20 AM
why not include $textlic = file_get_contents('/home/nvcnvn/license.txt');  inside the function?

Because this var is using for some other function! $textlic is just an example, in fact, I have do some thing else with these var before using it!

Any Ideal about this issue!?
Title: Re: How to add a context var!?
Post by: All Colours Sam on November 25, 2010, 11:17:14 AM
make $textlic  a $context['textlic']  and use it on your functions:

global $context;
$context['textlic'] = $textlic;

my_function() {

global $context;

return $context['textlic'];
}


$one = my_function();

echo $one;
Title: Re: How to add a context var!?
Post by: spottedhog on November 28, 2010, 08:54:49 AM
Here is one way to do what I think you are trying to do:

You could place all the text files into one folder so you can then call any of them in the same function using code like this:

function textlicshow($filename){
include("/home/yourname/public_html/ext_files_path/$filename");  // change to your own path to files
echo '<br />';
echo 'ZZZZZZZZZZZZZZZZZZZZZ';
}
$filename = "license.txt";
textlicshow($filename);
Title: Re: How to add a context var!?
Post by: nvcnvn on November 30, 2010, 07:10:39 AM
Thanks all of you! it ok now!