User:Illori/sand: Difference between revisions From Online Manual

Jump to: navigation, search
(Undo revision 5115 by Illori (talk))
No edit summary
 
(8 intermediate revisions by the same user not shown)
Line 1: Line 1:
{{Cleanup html}}
So you want to create a mod for SMF but do not know where to really start?
So you want to create a mod for SMF but do not know where to really start?


Line 19: Line 18:
Since we like to keep things organized, lets put this action in the index list in alphabetical order, so locate the following code:
Since we like to keep things organized, lets put this action in the index list in alphabetical order, so locate the following code:


<div class="codeheader">Code: <a href="javascript:void(0);" onclick="return smfSelectText(this);" class="codeoperation">[Select]</a></div><code class="bbc_code"><span style="white-space: pre;">
{{Code|1=
</span><span style="white-space: pre;">
 
</span>'groups' => array('Groups.php', 'Groups'),
')<span style="white-space: pre;">
'groups' => array('Groups.php', 'Groups'),
</span><span style="white-space: pre;">
 
</span>'help' => array('Help.php', 'ShowHelp'),
')<span style="white-space: pre;"> </span><span style="white-space: pre;">
'help' => array('Help.php', 'ShowHelp'),
</span>'helpadmin' => array('Help.php', 'ShowAdminHelp'),</code>
'helpadmin' => array('Help.php', 'ShowAdminHelp'),}}


We will insert our new action between the "groups" and "help" action.
We will insert our new action between the "groups" and "help" action.


<div class="codeheader">Code: <a href="javascript:void(0);" onclick="return smfSelectText(this);" class="codeoperation">[Select]</a></div><code class="bbc_code"><span style="white-space: pre;">
{{Code|1=
</span><span style="white-space: pre;">
 
</span>'groups' => array('Groups.php', 'Groups'),
')<span style="white-space: pre;">
'groups' => array('Groups.php', 'Groups'),
</span><span style="white-space: pre;">
 
</span>'hello' => array('Hello.php', 'DisplayHello'),
')<span style="white-space: pre;">
'hello' => array('Hello.php', 'DisplayHello'),
</span><span style="white-space: pre;">
 
</span>'help' => array('Help.php', 'ShowHelp'),
')<span style="white-space: pre;"> </span><span style="white-space: pre;">
'help' => array('Help.php', 'ShowHelp'),
</span>'helpadmin' => array('Help.php', 'ShowAdminHelp'),</code>
'helpadmin' => array('Help.php', 'ShowAdminHelp'),}}


So what does that mean? Lets break it down... the first part, "hello" is the action. To access this action, we would visit [http://yoursite/index.php?action=hello http://yoursite/index.php?action=hello]. Actions should always be lowercase for consistencies sake.
So what does that mean? Lets break it down... the first part, "hello" is the action. To access this action, we would visit [http://yoursite/index.php?action=hello http://yoursite/index.php?action=hello]. Actions should always be lowercase for consistencies sake.
')
 
')The second part, array('Hello.php', 'DisplayHello'), contains two important pieces of information.&nbsp; The 'Hello.php' is the file in the Sources directory that we want to load, and 'DisplayHello' is the name of the function we want to call after opening that file.&nbsp; You can name these whatever you want really, it doesn't make a difference.
The second part, array('Hello.php', 'DisplayHello'), contains two important pieces of information. The 'Hello.php' is the file in the Sources directory that we want to load, and 'DisplayHello' is the name of the function we want to call after opening that file. You can name these whatever you want really, it doesn't make a difference.
')
 
')Let's create Hello.php.&nbsp; Open your text editor and create a new file and save it as Hello.php in your /Sources directory.
Let's create Hello.php. Open your text editor and create a new file and save it as Hello.php in your /Sources directory.
')
 
')Create an empty function named DisplayHello in that file, so your Hello.php should look like this:
Create an empty function named DisplayHello in that file, so your Hello.php should look like this:
')
 
')<div class="codeheader">Code: <a href="javascript:void(0);" onclick="return smfSelectText(this);" class="codeoperation">[Select]</a></div><code class="bbc_code"><span style="color: #000000"><span style="color: #0000CC"><?php
{{Code|1=
')
<?php
')</span><span style="color: #006600">function&nbsp;</span><span style="color: #0000CC">DisplayHello</span><span style="color: #006600">()
 
'){
function DisplayHello()
')
{
')}
 
')
}
')</span><span style="color: #0000CC">?></span></span></code>
 
')We need to create our template file.&nbsp; For consistency, we'll call it Hello.template.php.&nbsp; Create this new empty file in your /Themes/default directory.
?>}}
')
We need to create our template file. For consistency, we'll call it Hello.template.php. Create this new empty file in your /Themes/default directory.
')Let's create a function in that file named template_hello_world, so your Hello.template.php looks like this:
 
')
Let's create a function in that file named template_hello_world, so your Hello.template.php looks like this:
')<div class="codeheader">Code: <a href="javascript:void(0);" onclick="return smfSelectText(this);" class="codeoperation">[Select]</a></div><code class="bbc_code"><span style="color: #000000"><span style="color: #0000CC"><?php
 
')
{{Code|1=
')</span><span style="color: #006600">function&nbsp;</span><span style="color: #0000CC">template_hello_world</span><span style="color: #006600">()
<?php
'){
 
')
function template_hello_world()
')}
{
')
 
')</span><span style="color: #0000CC">?></span></span></code>
}
')Okay, let's do some real coding now.&nbsp; Let's go back to Hello.php.&nbsp; We need to load our template file we've created so that SMF will be able to find it later, so add the following line to DisplayHello():
 
')<div class="codeheader">Code: <a href="javascript:void(0);" onclick="return smfSelectText(this);" class="codeoperation">[Select]</a></div><code class="bbc_code">loadTemplate('Hello');</code>
?>}}
')We get 'Hello' from <strong>Hello</strong>.template.php.&nbsp;
Okay, let's do some real coding now. Let's go back to Hello.php. We need to load our template file we've created so that SMF will be able to find it later, so add the following line to DisplayHello():
')
{{Code|1=
')Now we need to tell SMF which template function to use (in our case, template_hello_world).&nbsp; To do this we'll need add it to SMF's $context variable.&nbsp; $context is an array that SMF uses to store data that is being passed around to different functions or between the source and template files.
loadTemplate('Hello');}}
')First thing that we'll need to do is tell our DisplayHello() function that context is a global variable, so add this to the top of your DisplayHello() function:
We get 'Hello' from '''Hello'''.template.php.
')
 
')<div class="codeheader">Code: <a href="javascript:void(0);" onclick="return smfSelectText(this);" class="codeoperation">[Select]</a></div><code class="bbc_code">global $context;</code>
Now we need to tell SMF which template function to use (in our case, template_hello_world). To do this we'll need add it to SMF's $context variable. $context is an array that SMF uses to store data that is being passed around to different functions or between the source and template files.
')Now we can set the 'sub_template' key of the $context array to our template function.&nbsp; After the loadTemplate() function call, insert the following:
First thing that we'll need to do is tell our DisplayHello() function that context is a global variable, so add this to the top of your DisplayHello() function:
')
 
')<div class="codeheader">Code: <a href="javascript:void(0);" onclick="return smfSelectText(this);" class="codeoperation">[Select]</a></div><code class="bbc_code">$context&#91;'sub_template'] = 'hello_world';</code>
{{Code|1=
')Notice that we don't include the template_ part of the function name.&nbsp; SMF adds this automatically, so there's no need for you to do it.
global $context;}}
')
Now we can set the 'sub_template' key of the $context array to our template function. After the loadTemplate() function call, insert the following:
')So by now, your Hello.php file should look like this:
 
')
{{Code|1=
')<div class="codeheader">Code: <a href="javascript:void(0);" onclick="return smfSelectText(this);" class="codeoperation">[Select]</a></div><code class="bbc_code"><span style="color: #000000"><span style="color: #0000CC"><?php
$context&#91;'sub_template'] = 'hello_world';}}
')
Notice that we don't include the template_ part of the function name. SMF adds this automatically, so there's no need for you to do it.
')</span><span style="color: #006600">function&nbsp;</span><span style="color: #0000CC">DisplayHello</span><span style="color: #006600">()
 
'){
So by now, your Hello.php file should look like this:
')</span><span style="color: #0000CC"><span style="white-space: pre;"> </span>global&nbsp;</span><span style="color: #0000CC">$context</span><span style="color: #006600">;
 
')
{{Code|1=
')</span><span style="color: #0000CC"><span style="white-space: pre;"> </span></span><span style="color: #0000CC">loadTemplate</span><span style="color: #006600">(</span><span style="color: #CC0000">'Hello'</span><span style="color: #006600">);
<?php
')
 
')</span><span style="color: #0000CC"><span style="white-space: pre;"> </span></span><span style="color: #0000CC">$context</span><span style="color: #006600">[</span><span style="color: #CC0000">'sub_template'</span><span style="color: #006600">]&nbsp;=&nbsp;</span><span style="color: #CC0000">'hello_world'</span><span style="color: #006600">;
function DisplayHello()
')}
{
')
global $context;
')</span><span style="color: #0000CC">?></span></span></code>
 
')Right now if you test your mod you'll just see a blank page with an SMF header and footer (if you got a PHP error, check your code).&nbsp; It's always appropriate to define your text strings in the language files, NEVER in the templates.&nbsp; So let's open up /Themes/default/languages/Modifications.english.php add the following line to it:
loadTemplate('Hello');
')
 
')<div class="codeheader">Code: <a href="javascript:void(0);" onclick="return smfSelectText(this);" class="codeoperation">[Select]</a></div><code class="bbc_code">$txt&#91;'hello_world'] = 'Hello World!';</code>
$context['sub_template'] = 'hello_world';
')Save and close that file.&nbsp; If you are using SMF 2.0 and have caching enabled, you will need to clear your file cache for the language strings to take effect.&nbsp; You can do this in Admin > Maintenance > Forum Maintenance > Routine, and it's the last option.
}
')
 
')Let's go back to your template file now, Hello.template.php.&nbsp; Since we'll be using the global $txt variable, we need to let the function know it's a global variable.&nbsp; Add the following to the top of your template function:
?>}}
')
Right now if you test your mod you'll just see a blank page with an SMF header and footer (if you got a PHP error, check your code). It's always appropriate to define your text strings in the language files, NEVER in the templates. So let's open up /Themes/default/languages/Modifications.english.php add the following line to it:
')<div class="codeheader">Code: <a href="javascript:void(0);" onclick="return smfSelectText(this);" class="codeoperation">[Select]</a></div><code class="bbc_code">global $txt;</code>
 
')Now we just need to add one more thing and we should able to see 'Hello World!' displayed to the world.&nbsp; Add an echo statement for the new text string we created:
{{Code|1=
')
$txt&#91;'hello_world'] = 'Hello World!';}}
')<div class="codeheader">Code: <a href="javascript:void(0);" onclick="return smfSelectText(this);" class="codeoperation">[Select]</a></div><code class="bbc_code">echo $txt&#91;'hello_world'];</code>
Save and close that file. If you are using SMF 2.0 and have caching enabled, you will need to clear your file cache for the language strings to take effect. You can do this in Admin > Maintenance > Forum Maintenance > Routine, and it's the last option.
')So now your template file should look like this:
 
')
Let's go back to your template file now, Hello.template.php. Since we'll be using the global $txt variable, we need to let the function know it's a global variable. Add the following to the top of your template function:
')<div class="codeheader">Code: <a href="javascript:void(0);" onclick="return smfSelectText(this);" class="codeoperation">[Select]</a></div><code class="bbc_code"><span style="color: #000000"><span style="color: #0000CC"><?php
 
')
{{Code|1=
')</span><span style="color: #006600">function&nbsp;</span><span style="color: #0000CC">template_hello_world</span><span style="color: #006600">()
global $txt;}}
'){
Now we just need to add one more thing and we should able to see 'Hello World!' displayed to the world. Add an echo statement for the new text string we created:
')</span><span style="color: #0000CC"><span style="white-space: pre;"> </span>global&nbsp;</span><span style="color: #0000CC">$txt</span><span style="color: #006600">;
 
')
{{Code|1=
')</span><span style="color: #0000CC"><span style="white-space: pre;"> </span>echo&nbsp;</span><span style="color: #0000CC">$txt</span><span style="color: #006600">[</span><span style="color: #CC0000">'hello_world'</span><span style="color: #006600">];
echo $txt&#91;'hello_world'];}}
')}
So now your template file should look like this:
')
 
')</span><span style="color: #0000CC">?></span></span></code>
{{Code|1=
')Ok, now visit your new action and you should see 'Hello World!' displayed on the screen
<?php
')
 
')<a href="http://forum/index.php?action=hello" class="bbc_link new_win" target="_blank">http://forum/index.php?action=hello</a></div>
function template_hello_world()
{
global $txt;
 
echo $txt['hello_world'];
}
 
?>}}
Ok, now visit your new action and you should see 'Hello World!' displayed on the screen
 
<a href="http://forum/index.php?action=hello" class="bbc_link new_win" target="_blank">http://forum/index.php?action=hello</a></div>

Latest revision as of 21:25, 23 March 2011

So you want to create a mod for SMF but do not know where to really start?

Do not worry, we have all been in that same spot. Hopefully this will give you a basic idea of how to create your first mod that will display the classic "Hello World!" on the screen, wrapped by your SMF header and footer.


Note: These tutorials show you how SMF works, not how to code PHP. If you do not know PHP, please read and complete all of the PHP Tutorials that we have here. You should also use a decent text editor (NOT Dreamweaver or Frontpage) to edit your files.


While this mod will be relatively simple, it will use actions, text strings, source files, and templates.

Let's begin!

We need to open index.php in your main SMF directory and add a new action. We will call this action "hello (all lowercase) and it will load it's own Source file.

Since we like to keep things organized, lets put this action in the index list in alphabetical order, so locate the following code:

'groups' => array('Groups.php', 'Groups'),

	
	'help' => array('Help.php', 'ShowHelp'),
	
	'helpadmin' => array('Help.php', 'ShowAdminHelp'),

We will insert our new action between the "groups" and "help" action.

'groups' => array('Groups.php', 'Groups'),

	
	'hello' => array('Hello.php', 'DisplayHello'),

	
	'help' => array('Help.php', 'ShowHelp'),
	
	'helpadmin' => array('Help.php', 'ShowAdminHelp'),

So what does that mean? Lets break it down... the first part, "hello" is the action. To access this action, we would visit http://yoursite/index.php?action=hello. Actions should always be lowercase for consistencies sake.

The second part, array('Hello.php', 'DisplayHello'), contains two important pieces of information. The 'Hello.php' is the file in the Sources directory that we want to load, and 'DisplayHello' is the name of the function we want to call after opening that file. You can name these whatever you want really, it doesn't make a difference.

Let's create Hello.php. Open your text editor and create a new file and save it as Hello.php in your /Sources directory.

Create an empty function named DisplayHello in that file, so your Hello.php should look like this:

<?php

function DisplayHello()
{

}

?>

We need to create our template file. For consistency, we'll call it Hello.template.php. Create this new empty file in your /Themes/default directory.

Let's create a function in that file named template_hello_world, so your Hello.template.php looks like this:

<?php

function template_hello_world()
{

}

?>

Okay, let's do some real coding now. Let's go back to Hello.php. We need to load our template file we've created so that SMF will be able to find it later, so add the following line to DisplayHello():

loadTemplate('Hello');

We get 'Hello' from Hello.template.php.

Now we need to tell SMF which template function to use (in our case, template_hello_world). To do this we'll need add it to SMF's $context variable. $context is an array that SMF uses to store data that is being passed around to different functions or between the source and template files. First thing that we'll need to do is tell our DisplayHello() function that context is a global variable, so add this to the top of your DisplayHello() function:

global $context;

Now we can set the 'sub_template' key of the $context array to our template function. After the loadTemplate() function call, insert the following:

$context['sub_template'] = 'hello_world';

Notice that we don't include the template_ part of the function name. SMF adds this automatically, so there's no need for you to do it.

So by now, your Hello.php file should look like this:

<?php

function DisplayHello()
{
	global $context;

	loadTemplate('Hello');

	$context['sub_template'] = 'hello_world';
}

?>

Right now if you test your mod you'll just see a blank page with an SMF header and footer (if you got a PHP error, check your code). It's always appropriate to define your text strings in the language files, NEVER in the templates. So let's open up /Themes/default/languages/Modifications.english.php add the following line to it:

$txt['hello_world'] = 'Hello World!';

Save and close that file. If you are using SMF 2.0 and have caching enabled, you will need to clear your file cache for the language strings to take effect. You can do this in Admin > Maintenance > Forum Maintenance > Routine, and it's the last option.

Let's go back to your template file now, Hello.template.php. Since we'll be using the global $txt variable, we need to let the function know it's a global variable. Add the following to the top of your template function:

global $txt;

Now we just need to add one more thing and we should able to see 'Hello World!' displayed to the world. Add an echo statement for the new text string we created:

echo $txt['hello_world'];

So now your template file should look like this:

<?php

function template_hello_world()
{
	global $txt;

	echo $txt['hello_world'];
}

?>

Ok, now visit your new action and you should see 'Hello World!' displayed on the screen

<a href="http://forum/index.php?action=hello" class="bbc_link new_win" target="_blank">http://forum/index.php?action=hello</a>



Advertisement: