Hax Templates
Hax is a very simple but powerful template engine
Hax is a simple, yet powerful template engine that comes with yuga. Unlike most of the PHP template engines, Hax does not restrict you from using plain PHP code in your views. Actually, all Hax views are compiled into plain PHP code and cached until they are edited, this means Hax adds basically no overhead to your application. Hax view files use the .hax.php
file extension and are typically stored in the resources/views
directory inside of the main directory of your main application.
There're two primary advantages of using Hax i.e; template inheritance and sections. To get started, let's take a look at a simple example. First, we will define a default page layout. Since most web applications maintain the same general layout across various pages, it's convenient to define this layout as a single Hax view:
As you can see, this file contains HTML mark-up. However, take note of the @section
and @yield
directives. The @section
directive, as the name says, defines a section of content, while the @yield
directive is used to display contents of a given section where the @yield
directive is found.
Now that we have defined a layout for our application, let's define a child page that inherits the layout.
When defining a child view, we typically use the Hax @extends
directive to specify which layout the child view should "inherit". Views that extend a Hax layout can insert content into the layout's sections using @section
directives. Remember, as seen in the example above, the contents of these sections will be displayed in the layout using @yield
:
You can display data passed to your Hax views by wrapping the variable in curly braces. For example, given the following route:
You can display the contents of the name
variable like below:
In addition to template inheritance and displaying data, Hax also provides convenient shortcuts for common PHP control structures, such as conditional statements and loops.
You may construct if
statements using the @if
, @elseif
, @else
, and @endif
directives. These directives function identically to their PHP counterparts:
In addition to conditional statements, Hax provides simple directives for working with PHP's loop structures. Each of these directives functions identically to their PHP counterparts:
Last updated
Was this helpful?