SSI_NavBar (BETA) left $board nav buttons w/ Custom Pages (BETA) - SMF 2.0

Started by ElectricSquid, September 26, 2009, 01:11:43 AM

Previous topic - Next topic

Arantor

Quote from: ∑£ℓ¢†®¡¢ §ợų¡đ on October 05, 2009, 09:01:11 PM
So index.php is where the action= is defined.
But what do you mean when you say "provides a callback in smf_main()"?

The bulk of what index.php does is in the smf_main() function. That works out what action={blah} should become, then simply returns that function name, back to the main part of the script so it can be called.

ElectricSquid

#21
WARNING - this is still being developed - do not do this - there are issues still
But if you want to learn, you came to the right place.
==================================================


Ok, I did some coding today trying to code up the new homepage 2 different ways.

Version 1 - Easy -
This first way of setting up a custom page to have the index.template.php features show up (like user info, menu tabs, etc.) involves using the functions from index.template.php in a custom file.
Oh, and yes, this did work beside the load time at the bottom of the page which created an undefined index error for 'show_load_time'

In a custom php file named sample_page.php
ADD


<?php

require_once('C:\xxxx\xxxx\SSI.php');

template_init();
template_html_above();
template_body_above();

// ======================
// START custom page HERE
// ======================

echo '

Hi there!!

'
;

// ====================
// END custom page HERE
// ====================

template_body_below();
template_html_below();



?>




Now go to this file directly via url
example: http://yoursite.com/sample_page.php

===================================================
===================================================





Version 2 - A royal pain in the @$$ -
WARNING, this code does not work, yet
The next reply will explain more on that. When I get it fixed, I will update this code and remove this warning.

Since version 1 created that error, I decided to dive into the non-SSI way of creating new pages.
This is god-awful complicated and I do not recomend it to anyone unless they are on a test server and have some php experience under their belt (unlike myself :P)

First, the upper level index.php was edited to have 'samplepage' defined like so...

FIND


// Here's the monstrous $_REQUEST['action'] array - $_REQUEST['action'] => array($file, $function).
$actionArray = array(




ADD


'samplepage' => array('SamplePage.php', 'sample_page_main'),




Next...
In Sources, create a file called SamplePage.php

ADD


<?php
/**********************************************************************************
* Sample_Page.php                                                                         *
***********************************************************************************
* SMF: Simple Machines Forum                                                      *
* Open-Source Project Inspired by Zef Hemel ([email protected])                    *
* =============================================================================== *
* Software Version:           SMF 2.0 RC1                                         *
* Software by:                Simple Machines (http://www.simplemachines.org)     *
* Copyright 2006-2009 by:     Simple Machines LLC (http://www.simplemachines.org) *
*           2001-2006 by:     Lewis Media (http://www.lewismedia.com)             *
* Support, News, Updates at:  http://www.simplemachines.org                       *
***********************************************************************************
* This program is free software; you may redistribute it and/or modify it under   *
* the terms of the provided license as published by Simple Machines LLC.          *
*                                                                                 *
* This program is distributed in the hope that it is and will be useful, but      *
* WITHOUT ANY WARRANTIES; without even any implied warranty of MERCHANTABILITY    *
* or FITNESS FOR A PARTICULAR PURPOSE.                                            *
*                                                                                 *
* See the "license.txt" file for details of the Simple Machines license.          *
* The latest version can always be found at http://www.simplemachines.org.        *
**********************************************************************************/

if (!defined('SMF'))
die('Hacking attempt...');

/* This file is mainly concerned, or that is to say only concerned, with building new pages.
It contains only the following functions:

sample_page_main()
- prepares the sample_page data for the Sample_Page template.
- uses the SamplePage template (main sub template.) and language file.
- is accessed via ?action=samplepage

     // ================
    Look for // Sample page MAIN for the code to copy to create new pages 
     // ================


*/

// ================
// Sample page MAIN
// ================

// heres the sample code for custom pages
// copy this to a new function to build new pages
// be sure to add the new action= to index.php
// be sure to add the new_page.template.php to the theme folder

function sample_page_main()
{
global $context$scripturl$txt$smcFunc;

// Load the 'SamplePage' template.
loadTemplate('SamplePage''sample_page_one');
loadLanguage('Pages');
}


?>





Next...
In whatever theme you are using, create a file called SamplePage.template.php

ADD


<?php

function sample_page_one()
{

global 
$txt;

// ======================
// START custom page HERE
// ======================

echo '

Hi there!!<br />
'
;

// ====================
// END custom page HERE
// ====================

}

?>





Next...
In whatever theme you are using, create a php file in \languages called Pages.english.php
(not used yet due to issues, but will be in the near future)
ADD


<?php
// Version: 2.0 RC1; SamplePage

// Important! Before editing these language files please read the text at the top of index.english.php.

global $scripturl$context;

$txt['sample_text_1'] = '<em>This is sample text one</em>';
$txt['sample_text_2'] = '<em>This is sample text two</em>';
$txt['sample_text_3'] = '<em>This is sample text two</em>';

?>




==================================================
==================================================


ElectricSquid

#22
OK, I figured I'd post my issue in a new reply so it wasn't lost at the bottom of that monstrous one above.
In version 2, there is some kind of issue.
It seems like I am really close to getting it to work, but for some reason when I go to the page, it is erroring out telling me:

An Error Has Occurred!
Unable to load the 'main' template.

I have tried a bunch of different thing, and none of them worked.
Any help would be appreciated.

Arantor

So, you're getting that from your own custom code?

There's no template_main() function in your template file. It's expecting to run that.

ElectricSquid

OK, that was the issue. A minor edit and it worked. Thanks :D

I swapped the text "hello" out of the custom function sample_page_one() over to function template_main() and the sample page was created properly without error, and with the index.template.php header, footer, and my custom SSI_navbar.

My 2 questions:
==========

With my custom code...
index.php says use function sample_page_main() in Sources\SaplePage.php for action=samplepage
function sample_page_main() says load SamplePage.template.php and use function sample_page_one()

1) So why is it that instead of using function sample_page_one(), it uses function template_main() ??
2) and how is it that I make this code use function sample_page_one() in SamplePage.template.php ??


Arantor

1. Because the template engine expects to find template_main().
2. For a simple page being displayed you will see template_main() as the primary way of doing it throughout SMF, and it is recommended to stick to that. Modifying it does make things a lot more complicated.

ElectricSquid

LOL, as you can see, I'm not scared off from complicated. In fact, complicated makes this fun for me, and I'm learning a lot as I go.

For most of my needs, template_main does the job, but I would like to learn more about how to call upon other functions within the template. Could you point me in the right direction?

Arantor

It really depends on the type of page.

Best thing to do is sit and examine how some of the other templates work; they explain it far better than I can; for main pages, they call template_main() but some such as Admin.php don't. They call their own sub templates. Look how they do it.

ElectricSquid


SoLoGHoST

You don't have to use template_main().  Honestly, I never needed to use it.  I usually do sub templates instead, since I deal with actions that have several different sub actions and need to display differently for each subaction.

In the PHP Function you can define in the last line of the function (though probably doesn't matter where it's defined):$context['sub_template'] = 'anythingyouwant';

And then in the template file that gets loaded with loadTemplate(), you can now have a function called, function template_anythingyouwant()
{
// your html related code, etc. in here...
}


I don't use the template_main() function at all, since this will always load the template_main function.  If you want to have different pages load from 1 action, depending on the subaction that gets requested, use $context['sub_template'] = '';  Just remember it's name, and the function will always need to have template_ in front of the name you gave it.

Cheers :)

On a more Personal Note:  I feel that using template_main() is a perfectly good example of a waste of a .template.php file and an SMF action.  Probably just me though.

Arantor

Quote from: SoLoGHoST on October 14, 2009, 04:43:15 PM
On a more Personal Note:  I feel that using template_main() is a perfectly good example of a waste of a .template.php file and an SMF action.  Probably just me though.

Depends what you're doing. Extending an existing area, subaction+subtemplate is recommended. Anything new, should really be its own action and invariably it's own template too.

The whole idea is a logical separation of data from presentation.

For this, maybe, for other uses, no.

ElectricSquid


ElectricSquid

Thanks for explaining this to me.
Here's one of the things I'll be using this for, and part of what I've been preparing for with almost everything in this topic ...

http://www.simplemachines.org/community/index.php?topic=342535.0

Advertisement: