Skip to content
hertsch edited this page Aug 6, 2013 · 5 revisions

The kitFramework provide you with a class Basic for the kitCommands.

Please try

~~ HelloBasic ~~

the response will be something like this:

PAGE_ID: 1
PAGE URL: http://localhost/wb283/pages/kitcommand.php

The controller in the bootstrap.include.php:

$app->match('/command/hellobasic', function() use ($app) {
    $HelloBasic = new HelloBasic($app);
    return $HelloBasic->exec();
});

The constructor of HelloBasic will be initialized with $app as parameter (Dependency Injection) and get full access to the kitFramework application.

Please have a look into the class Sample06:

namespace thirdParty\HelloWorld\Control;
use phpManufaktur\Basic\Control\kitCommand\Basic as kitCommandBasic;

The Basic class will be used as kitCommandBasic (only to make the things a little bit more clear, surely you can also use Basic) and is the parent for HelloBasic:

class HelloBasic extends kitCommandBasic
{    
    public function exec()
    {
        return $this->app['twig']->render($this->app['utils']->templateFile('@thirdParty/HelloWorld/Template', 'hello.basic.twig', $this->getPreferredTemplateStyle()),
            array(
                'cms' => $this->getCMSinfoArray()
        ));
    }    
}

The constructor does not appear here, simplified he is looking like that:

namespace phpManufaktur\Basic\Control\kitCommand;
use Silex\Application;

class Basic
{
    protected $app = null;

    public function __construct(Application $app, $parameter_id=-1)
    {
        $this->app = $app;
    }
 }

You see that $app is provided as $this->app and available for the parent and children of the class Basic.

Let's go back to the class HelloBasic.

return $this->app['twig']->render($this->app['utils']->templateFile('@thirdParty/HelloWorld/Template', 'hello.basic.twig', $this->getPreferredTemplateStyle()),
    array(
        'cms' => $this->getCMSinfoArray()
));

This nested return is hard to understand, so we extend it in some steps:

// get the CMS information like PAGE ID into $data 
$data = array('cms' => $this->getCMSinfoArray());

// get the template file
$template = $this->app['utils']->templateFile(
    '@thirdParty/HelloWorld/Template', 
    'hello.basic.twig', 
    $this->getPreferredTemplateStyle());

// render the template with the template engine Twig
$result = $this->app['twig']->render($template, $data);

// return the result
return $result;

The result will be the same as in the nested return.

// get the CMS information like PAGE ID into $data 
$data = array('cms' => $this->getCMSinfoArray());

Remember the last step Using parameters, where we have seen, that you can access with $cms = $app['request']->request->get('cms'); to additional information about the Content Management System. The Basic class serve you this information array with $this->getCMSinfoArray().

// get the template file
$template = $this->app['utils']->templateFile(
    '@thirdParty/HelloWorld/Template', 
    'hello.basic.twig',
    $this->getPreferredTemplateStyle());

Instead of accessing a template file directly - in this case the sample06.twig which is hosted at

/htdocs/../kit2/extension/thirdparty/thirdParty/HelloWorld/Template/default/hello.basic.twig

you should always use

  • Namespaces and
  • the kitFramework function templateFile()

Using Templates in the kitFramework provide you with the opportunity to use different templates and layouts for the same output.

It's more easy to demonstrate it than to explain it. Please try:

~~ HelloBasic template[two] ~~

this will prompt something like:

PAGE_ID: 1
PAGE URL: http://localhost/wb283/pages/kitcommand.php
USER: eddie
EMAIL: [email protected]

Oooops. What happend? Same kitCommand, same controller, same class HelloBasic but still another output?

The answer is: the kitCommand is now using the template two instead of the default template:

/htdocs/../kit2/extension/thirdparty/thirdParty/HelloWorld/Template/two/hello.basic.twig

and this template contains additional the USER and the EMAIL field (later more about templates).

The function $this->app['utils']->templateFile() is handling this feature:

  • the first parameter is the namespace of the template, in this case @thirdParty/HelloWorld/Template (the @ indicate the namespace)
  • the second parameter is the file name of the template hello.basic.twig
  • the third parameter tells the function which template should be used preferred.

The function $this->getPreferredTemplateStyle() from the Basic class return the preferred template. This can be default (Standard) or a template which is set in the framework.json or a template which is set as parameter in the kitCommand. There are many options.

You should have a look at the kitCommand help to get an overview of the general supported parameters:

~~ help ~~

Have a look at the css[] parameter and try:

~~ HelloBasic css[HelloWorld, hello.basic.css] ~~

This is a big playground, try it! Remember that you must use templates and namespaces to enable this feature.

Let's go back to the class HelloBasic. The next step we have to look at:

// render the template with the template engine Twig
$result = $this->app['twig']->render($template, $data);

With $this->app['twig']->render() you access the Twig template engine and render the template $template with the given $data.

At least you return the rendered template:

// return the result
return $result;

Remember the nested call:

return $this->app['twig']->render($this->app['utils']->templateFile('@thirdParty/HelloWorld/Template', 'hello.basic.twig', $this->getPreferredTemplateStyle()),
    array(
        'cms' => $this->getCMSinfoArray()
));

In the next step we have a brief look at the usage of templates.

Clone this wiki locally