You are not logged in.

1

Wednesday, February 8th 2012, 6:16pm

Set Variables for a template

I haven't got so much skill, but I tried to make a Game Serverstatus Page but i didn't found something to assign any variable to the template :o

isn't this right? : WCF::getTPL()->assign('test', 'My Test Text');

Sorry ;)

2

Wednesday, February 8th 2012, 6:18pm

PHP Source code

1
2
3
4
WCF::getTPL()->assign(array(
            'item1' => $item1,
            'item2' => $item2
        ));


if you want to pass more than one :)
Mit freundlichen Grüßen,
Christopher Walz

Meine kostenlosen Plugins

3

Wednesday, February 8th 2012, 6:20pm

ok thanks for your fast reply, and then i get them how? (in the template) with {$item1} and {$item2} ?

4

Wednesday, February 8th 2012, 6:23pm

Right :)
Add a @ before the dollar sign to get it as HTML -> {@$item1}
Mit freundlichen Grüßen,
Christopher Walz

Meine kostenlosen Plugins

5

Wednesday, February 8th 2012, 6:31pm

ah okay thanks ;) unfortunately i have to go off for today, will try it tomorrow ;)

6

Friday, February 10th 2012, 8:27pm

I tested but it doesn't work. ...don't know what im doing wrong.

if I understood it, then in the class i generate the wanted content and in the template i can make the output.

I tried this:

PHP Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?php
// wcf imports
require_once(WCF_DIR.'lib/page/AbstractPage.class.php');

/**
 * Minecraft ServerStatus
 *
 * @author      Sorry
 * @copyright   fightcraft.de
 * @package     no
 * @license     ask admin@fightcraft.de
 */
class ServerStatusPage extends AbstractPage {
    // system
    public $templateName 'serverstatus';
    
    function __construct() {
      parent::__construct();
      //$error = 'error';
      //$item1 = 'item1';
      //$item2 = 'item2';
    
      $item1 'test';
      $item2 'test';
    
      WCF::getTPL()->assign(array(
            'item1' => $item1,
            'item2' => $item2
      ));
    }
}


and this:

PHP Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<?php
// wcf imports
require_once(WCF_DIR.'lib/page/AbstractPage.class.php');

/**
 * Minecraft ServerStatus
 *
 * @author      Sorry
 * @copyright   fightcraft.de
 * @package     no
 * @license     ask admin@fightcraft.de
 */
class ServerStatusPage extends AbstractPage {
    // system
    public $templateName 'serverstatus';

      //$error = 'error';
      //$item1 = 'item1';
      //$item2 = 'item2';
    
      $item1 'test';
      $item2 'test';
    
      WCF::getTPL()->assign(array(
            'item1' => $item1,
            'item2' => $item2
      ));
}


both does not work...
but in the first code i get this error: PHP notice in file C:\xampp\htdocs\wbb\wcf\templates\compiled\48_0_1_serverstatus.php (47): Undefined index: item1, in the second i get this: Parse error: syntax error, unexpected T_VARIABLE, expecting T_FUNCTION in C:\xampp\htdocs\wbb\lib\page\ServerStatusPage.class.php on line 23
so the first one seem to be better....

This post has been edited 1 times, last edit by "Sorry" (Feb 10th 2012, 8:33pm)


7

Friday, February 10th 2012, 8:38pm

You don't have to use the constructor anymore, because you extend from the AbstractPage, which already has a constructor.
You class could look like this:

PHP Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?php
// wcf imports
require_once(WCF_DIR.'lib/page/AbstractPage.class.php');

/**
 * Minecraft ServerStatus
 *
 * @author      Sorry
 * @copyright   fightcraft.de
 * @package     no
 * @license     ask admin@fightcraft.de
 */
class ServerStatusPage extends AbstractPage {
    // system
    public $templateName 'serverstatus';

    
    /**
     * @see Page::readData()
     */
    public function readData() {
        $item1 'test';
        $item2 'test';
        parent::readData();
    }
    
     /**
     * @see Page::assignVariables()
     */    
    public function assignVariables() {
        WCF::getTPL()->assign(array(
            'item1' => $item1,
            'item2' => $item2
        ));
        parent::assignVariables();
    }
}

?>
Mit freundlichen Grüßen,
Christopher Walz

Meine kostenlosen Plugins

8

Friday, February 10th 2012, 8:40pm

okay thank you... and where can i look up which functions names (example: public function readData()) i have to use?

9

Friday, February 10th 2012, 8:41pm

AbstractPage or AbstractForm, depends on which class you extend from ;)
Mit freundlichen Grüßen,
Christopher Walz

Meine kostenlosen Plugins

10

Friday, February 10th 2012, 8:44pm

there shouldn't be any user input. so I think AbstractPage should be right.

i tried what you wrote but then i get this error:
error message: PHP notice in file C:\xampp\htdocs\wbb\lib\page\ServerStatusPage.class.php (32): Undefined variable: item1

11

Friday, February 10th 2012, 8:46pm

PHP Source code

1
2
3
4
5
6
    public function readData() {
  parent::readData();
        $item1 'test';
        $item2 'test';
      
    }

try it likes this.
Mit freundlichen Grüßen,
Christopher Walz

Meine kostenlosen Plugins

12

Friday, February 10th 2012, 8:49pm

again PHP notice in file C:\xampp\htdocs\wbb\lib\page\ServerStatusPage.class.php (32): Undefined variable: item1 :(

0xLeon

Intermediate

  • "0xLeon" is male

Posts: 386

Location: Stuttgart

  • Send private message

13

Friday, February 10th 2012, 8:52pm

Won't work. The problem is: Variables defined within ServerStatusPage::readData() aren't available within ServerStatusPage::assignVariables(). Either you have to use class variables or define the variables within ServerStatusPage::assignVariables() itself.
By the way: The problem with the version with your code within the constructor was: Your methoed calls the parent method before its own stuff is done. So the parent constructor method calls alle the class methods and tries to display the template. I recommed some learing of OOP, because this stuff is pretty common within WCF.
Meine Pakete
Invitation System – Plugin StoreGithub
Update Files Delete PIP – Plugin StoreGithub
Friend Messenger – Github
Kein Support via PN oder E-Mail.

14

Friday, February 10th 2012, 8:54pm

I realised that with OOP too but it's seems to be much more complicated...

Alexander Ebert

WoltLab Developer

  • "Alexander Ebert" is male

Posts: 3,804

Location: Berlin

  • Send private message

15

Friday, February 10th 2012, 9:01pm

I realised that with OOP too but it's seems to be much more complicated...

It is not. If you're familiar with PHP you might know about variable scope, which means that you cannot access a variable defined outside a function unless you import it with "global" or pass it as argument. Try the example below, it works and shows you how to carry variables across different functions.

PHP Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?php
// wcf imports
require_once(WCF_DIR.'lib/page/AbstractPage.class.php');

/**
 * Minecraft ServerStatus
 *
 * @author      Sorry
 * @copyright   fightcraft.de
 * @package     no
 * @license     ask admin@fightcraft.de
 */
class ServerStatusPage extends AbstractPage {
    // system
    public $templateName 'serverstatus';
    
    public $item1 '';
    public $item2 '';
    
    /**
     * @see Page::readData()
     */
    public function readData() {
        parent::readData();
        
        $this->item1 'test1';
        $this->item2 'test2';
        
    }
    
     /**
     * @see Page::assignVariables()
     */    
    public function assignVariables() {
        parent::assignVariables();
        
        WCF::getTPL()->assign(array(
            'item1' => $this->item1,
            'item2' => $this->item2
        ));
    }
}
?>
Alexander Ebert
Developer WoltLab® GmbH


16

Friday, February 10th 2012, 9:24pm


It is not. If you're familiar with PHP you might know about variable scope, which means that you cannot access a variable defined outside a function unless you import it with "global" or pass it as argument. Try the example below, it works and shows you how to carry variables across different functions.

i knew that but i was a little bit confused...

thank you for your example. it works, i think a can work with it ;)

17

Friday, February 10th 2012, 9:42pm

it works very well now ;) now i've only to improve the design.

and i've to try foreach in the template within an array ... i'll hope it works ;)

18

Friday, February 10th 2012, 10:09pm

if i want to do something like this:

PHP Source code

1
{if isArray($infovalue)}{foreach from=infovalue item=value}{$value}<br/>{/foreach}{/if}

where do I have to place the funtion isArray? Or isn't that possible?

edit: or something like:

PHP Source code

1
{if $infovalue|is_array{*}don't know if that exists{*}}{foreach from=infovalue item=value}{$value}<br/>{/foreach}{/if}


now i found another solution, i rewrote a small part of the php script and it works like i want now :D

This post has been edited 3 times, last edit by "Sorry" (Feb 10th 2012, 10:50pm)


19

Saturday, February 11th 2012, 12:21am

Why do you want to check if the given variable is a array?
You should know this by initialising it in your class:

PHP Source code

1
2
3
    public $array = array();
    public $string '';
    public $integer 0;


And please avoid double posts...WBB has a easy-to-use edit function.
Mit freundlichen Grüßen,
Christopher Walz

Meine kostenlosen Plugins

This post has been edited 1 times, last edit by "Christopher Walz" (Feb 11th 2012, 12:28am)


Similar threads