Categories > TinyButStrong general >

MVC & RESTful & TBS

The forum is closed. Please use Stack Overflow for submitting new questions. Use tags: tinybutstrong , opentbs
By: Alan Lake
Date: 2009-02-09
Time: 20:10

MVC & RESTful & TBS

My MVC program has a controller (PHP code) and a view (HTML code).  The controller uses a RESTful package.  Before TBS, my controller looked something like this:
class ParamsController implements RestController { 
    function execute(RestServer $rest) {
        $title = 'My Title';
        $html = file_get_contents('my_template.html');
        // code to merge the value of $title into $html
        return new StringView($html);  // Stringview is a class in the RESTful package
    }
}
Because some of my variables are more complex than the simple assignment to $title, I want to use TBS in a manner similar to this:
class ParamsController implements RestController { 
    function execute(RestServer $rest) {
        $title = 'My Title';
        $TBS =& new clsTinyButStrong;
        $TBS->LoadTemplate('mytemplate.html');
        $TBS->Show(TBS_NOTHING);
        return new StringView($TBS->Source);  // Stringview is a class in the RESTful package
    }
}
This doesn't work because TBS is looking for global variables.  I changed one statement to
        $TBS->LoadTemplate('mytemplate.html','=ParamsController.execute');
but that doesn't work, presumably because my TBS calls are within the ParamsController class.  I also tried to move the variable into another class:
class ParamsController implements RestController { 
    function execute(RestServer $rest) {
        $TBS =& new clsTinyButStrong;
        $TBS->LoadTemplate('mytemplate.html','=GetParams.init');
        $TBS->Show(TBS_NOTHING);
        return new StringView($TBS->Source);  // Stringview is a class in the RESTful package
    }
}

class GetParams
{
    function init()
    {
        $title = 'My Title';
    }
}
This didn't work either, as TBS is still looking for global variables. 

I tried to create global variables like this:
class ParamsController implements RestController { 
    function execute(RestServer $rest) {
        $GLOBALS['title'] = 'My Title';
        $TBS =& new clsTinyButStrong;
        $TBS->LoadTemplate('mytemplate.html');
        $TBS->Show(TBS_NOTHING);
        return new StringView($TBS->Source);  // Stringview is a class in the RESTful package
    }
}
I really don't like global variables. They seem to violate the spirit of OO.  Are there any other suggestions?

By: Skrol29
Date: 2009-02-09
Time: 22:52

Re: MVC & RESTful & TBS

Hi Alan,

You should put the variables to merge into an array, and merge them using MergeField().

class ParamsController implements RestController {
    function execute(RestServer $rest) {
        $title = 'My Title';
        $myvars = array('title'=>$title);
        // or $myvars = get_defined_vars() in order to get all variables including locals and globals
        $TBS =& new clsTinyButStrong;
        $TBS->LoadTemplate('mytemplate.html');
        $TBS->MergeField('mv',$myvars);
        $TBS->Show(TBS_NOTHING);
        return new StringView($TBS->Source);  // Stringview is a class in the RESTful package
    }
}