.. SlaxWeb Framework General Topics - Views & Templates file, created by Tomaz Lovrec .. highlight:: php .. _gen topics viewtpl: Views & Templates ================= Templates are the visual side of your application, they define the look & feel of your application, while Views are the ones controlling the Templates. They obtain data, merge it with the data it received through other channels, and render the Template with the help of the templating engine. To use Views & Templates, you need to extend the functionality of the Framework. SlaxWeb provides an installable component called **view** for this exact purpose. It comes with a sub-component that exploits the `Twig Templating Engine `_ to help with the Template rendering, however, it is not required, as the main **view** component uses plain PHP Templates. To Use the Twig Templating Engine, just install the **view-twig** sub-component when asked during installation of the **view** component, and refer to the Twig documentation for use. To install the **view** component use the :ref:`gen topics slaxer` command line tool:: ./slaxer component:install view And follow the on-screen instructions. The View -------- As outlined above, a View is the one controlling the Templates, it is sort of a presenter for the Templates, as it obtains and prepares the data for the Template and renders and outputs the rendered Template. Therefore, a View is a PHP class, that has to extend from the **\\SlaxWeb\\View\\Base** class. The Base View already provides all the necessary functionality for rendering of the Template. The constructor of the Base View requires the configuration object, template loader class, and the response object. Base View also sets the base directory of the templates, which is controlled by the **baseDir** configuration option found in the **view.php** configuration file. The base directory is the directory where all the template files are located, be in it directly or in subdirectories. Loading the View ```````````````` The View component provides a View Loader service, available with the **loadView.service** in the :ref:`gen topics application`. It takes 2 parameters, the first is the name of the View that you want loaded, and the second if the loader should attempt to inject the loading Views template into a :ref:`gen topics viewtpl layout` template automatically, which it attempts to do by default. Example:: addSubView("login", $subView)->render(); The Sub View is then rendered before the main view, and it is injected into the main views parameters with the **subview_** prepending the name of the Sub View. In our example the Sub View would be available in the **subview_login** template variable. Multiple Sub Views added to the main view are concatenated into the same template variable in the order that they were added. .. _gen topics viewtpl subtemplates: Sub-Templates ''''''''''''' Sub Templates are the same as the Sub Views, except they do not require their own View class, since they reuse the already available base View class **\\SlaxWeb\\View\\Base**. This makes loading simpler and faster since you do not need to invoke the View loader service to load a view, and instantiate a class. Example:: addSubView("login", "Login")->render(); Code is also a bit shorter, and there is no need to create an empty View class just to load a simple Sub Template. They are loaded into the same View variable, so the above example will also be available as **subview_login** in your main template. .. _gen topics viewtpl layout: Layouts ``````` Layouts, while technically still the same as regular Views, are wrappers for your templates, that never change, and just present different kind of content. A layout would normally contain the HTML head definitions, and so on, and dedicate a certain area as to where the rendered view will be injected. When the main view is rendered it is injected into the layout template file with the **mainView** template variable. To use a layout on a template its View class object needs to be added to the view with the **setLayout** method. In the following example we will load the homepage view, and use the *DefaultLayout* as the layout:: setLayout($layout)->render(); As you can see, we use the View Loader service to load the Layout View class, just as if loading a regular view. The Layout View in fact is just a regular view, what makes it a layout is the way we use it. The main view will then render the main view, store it in a variable, and inject it into the layout view before rendering the layout template. Default layout '''''''''''''' In the **view.php** configuration file a default layout may be set. If a default layout is set, and the second parameter to the **loadView.service** or the **loadTemplate.service** is omitted or set to *true*, the loader will automatically load and set the default layout for that view. Layout View ''''''''''' Just as by normal templates, the View class can be omitted for the Layout Template. If the loader does not find the Layout View Class it will use the **\\SlaxWeb\\View\\Base** class as the View class. If special processing is required for the layout template then a class with the same name must be found in the **classNamespace**(**view.php**) defined namespace.