MVC
MVC stands for model-view-controller pattern, or style, yuga supports this very well
When a developer chooses to use mvc as their style with yuga, Views are accepted in two formats i.e; php views, in which case there's totally nothing new to learn, or hax views in which case there's a bit of new syntax to learn on how to manipulate and control them.
Views contain the HTML served by your application and separate your controller / application logic from your presentation logic. Views are saved in the resources/views
directory. A simple view would look like this:
Since this view is stored at resources/views/test.hax.php
, it can be returned using the global view
helper like this:
As you can see, the first argument passed to the view
helper corresponds to the name of the view file in the resources/views
directory. The second argument is an array of data that should be made available to the view. In this case, we are passing the name
variable, which is displayed in the view using Hax syntax.
Views can also be nested within sub-directories of the resources/views
directory. Dot notation may be used to reference nested views. For example, if your view is stored at resources/views/users/profile.hax.php
, you can reference it like as below:
Creating The First Available View
With the first
method, you can create the first view that exists in a given array of views. This is useful if your application or package allows views to be customized or overwritten:
Passing Data To Views
As you saw in the previous examples, you may pass an array of data to views:
When passing information in this format, the data should be an array with key / value pairs. Inside your views, you can then access each value using its corresponding key, such as <?= $key; ?>
. An alternative syntax can also be used instead of passing the data array as a second parameter in the view
global function as seen below:
Missing View Name
The view
helper method accepts an optional string in which case when given, it works as the name of the view you want to render. When not given, yuga will try to look for a view from within the resources/views
directory with the name Controller/Method.php
E.g
In the above example, Yuga will look for resources/views/Test/method.php
file, and once found, it will render it
Last updated