Views



Views are the web pages that users encounter, consisting mainly of HTML codes. Views are created in the Projects/Any/Views/ directory. In order for these pages to be viewed by users, they must be loaded by the requested controllers.

ZN Framework uses the Wizard named template wizard created by itself for the views. We strongly recommend that you use the template wizard, which is not mandatory, but shortens the code development process and simplifies the writing stages. For this reason, the following examples of views have been given in consideration of this situation.

 

 

# Bölüm Başlıkları


# View Loading Methods
# Create a View Page
# Import View Page
# Send Data to Imported View

# Send Data to All Views
# Sending the View as Data

# Automatically Import Views [5.0.0]

# Automatic Loading in Other Methods
# Send Data
# Import Other Views [5.7.4]

# View Class [5.2.6]

# Import View
# Send Data
# Send Content

 

 

# View Loading Methods


Views can be loaded by controllers in 2 ways.

1 - Loading with the Import Library
2 - Automatic Loading with Controller/Method Matching [5.0.0]

 

 

# Create a View Page


The views need to be created before they can be loaded by the controllers. Now, as an example, create 2 files named homepage.wizard.php and content.wizard.php in Views/ directory and edit their content as follows.

File: Projects/Frontend/Views/homepage.wizard.php
<html>
    <head>
        <title>{{ $title }}</title>
    </head>

    <body>
        {{ $content }}
    </body>
</html>
File: Projects/Frontend/Views/content.wizard.php
this is Content Page!

 

 

# Import View Page


In order to run the created views over the URL, they must be called by the relevant controllers. The Import::view() method is used to call views within the Views/ directory. Apart from these methods, it is also possible to load views automatically.

Let's show you how to include the view contained in the Views/ directory by the controller in the Controllers/ directory.

File: Projects/Frontend/Controllers/Homepage.php
<?php namespace Project\Controllers;

use Import;

class Homepage extends Controller
{
    public function main()
    {
        Import::view('content');
    }
}
Run: localhost/homepage

You can see that the Views/content.wizard.php file has been loaded by the relevant controller.

this is Content Page!
Warning: If the page you include is a file with a .php extension, there is no need to use the .php extension.

 

 

# Send Data to Imported View


Data is sent over the controllers into the created views. Because the main objects that will shape the appearance are the data prepared and sent by the controllers. Below is a sample code showing how to do this.

File: Projects/Frontend/Controllers/Homepage.php
<?php namespace Project\Controllers;

use Import;

class Homepage extends Controller
{
    public function main(...$parameters)
    {
    	$data['title']   = 'Welcome to Web Page'; 
        $data['content'] = Import::usable()->view('content');
        
    	Import::view('homepage', $data);
    }
}

2 pieces of data were sent to Views/homepage.wizard.php view above. While the first data is a simple string expression, the second data is the content of Views/content.wizard.php view. The usable() method has been used to transfer this content to the variable. There are 2 variables named $title and $content in the content of the Views/homepage.wizard.php view you created. These variables represent the keys of the array.

Run: localhost/homepage
this is Content Page!
Send Data to All Views

Submissions made with the View:: class also send data to all view pages loaded at the same time. Thus, there is no need to send again for nested views.

File: Projects/Frontend/Controllers/Homepage.php
<?php namespace Project\Controllers;

use Import;

class Homepage extends Controller
{
    public function main(...$parameters)
    {
    	View::example('Example Data');
   
        $page = Import::usable()->view('content');
        
    	Import::view('home', ['page' => $page]);
    }
}

With the use of View::example() in the above usage, we have sent $example data to both content and home views.

Sending the Views as Data

In the example above, it can be seen that a view has already been sent to another view as data. However, under this title, you will see how a page is sent as data with the View:: class.

Rule: View::dataName('page:pageName')

File: Projects/Frontend/Controllers/Homepage.php
<?php namespace Project\Controllers;

use Import;

class Homepage extends Controller
{
    public function main(...$parameters)
    {
        # Sending Views/content.wizard.php as data.
    	View::subpage('page:content');
        
        # Views/home.wizard.php
    	Import::view('home');
    }
}

In the above example, Views/content.wizard.php view is sent as data to Views/home.wizard.php view by means of the page: statement.

 

 

# Automatically Import Views [5.0.0]


By default, the views that the controllers will load are defined in the method related to the Import::view() method. However, it can be annoying to include views with Import:: every time. Let's introduce you to the automatic loading feature that will save you from this trouble. For automatic uploading, there must be a certain link between the controller name and the view name.

Controller: Projects/Frontend/Controllers/Example.php
View: Projects/Frontend/Views/Example/main.wizard.php
Run: localhost/example
Automatic Loading in Other Methods

Eğer alt fonksiyonlara otomatik olarak sayfa yükletmek isterseniz aşağıdaki gibi kullanıyorsunuz.

View: Projects/Frontend/Views/ControllerFileName/methodName.php

So the controller name should be the directory name in Views/. The methods in the controller must be the filename within that directory.

Send Data

You can use the View:: class to send data. This library need not be specified with use.

File: Projects/Frontend/Controllers/Example.php
<?php namespace Project\Controllers;

class Example extends Controller
{
    public function main(string $params = NULL)
    {
        View::pageName('Example Page');
    }

    public function try()
    {
         View::pageName('Example Try Page');
    }
}
File: Projects/Frontend/Views/Example/main.wizard.php
Example Page
File: Projects/Frontend/Views/Example/try.wizard.php
Example Try Page
Import Other Views

Aynı kontrolcü dizine sahip görünümlerin bir biri içerisine dahil edilmesi gereken durumlarda standart olarak görünümün tam yolu yazılır.

File: Projects/Frontend/Controllers/Example.php
<?php namespace Project\Controllers;

use Import;

class Example extends Controller
{
    public function main(string $params = NULL)
    {
        View::productPage('view:Example/product');
    }
}

In the above usage, there is an example showing that Example/product view is sent to Example/main view as $productPage data.

[5.7.4]

With this update, views with the same controller directory can also be included using the (/) symbol instead of the directory name.

File: Projects/Frontend/Controllers/Example.php
<?php namespace Project\Controllers;

use Import;

class Example extends Controller
{
    public function main(string $params = NULL)
    {
        View::productPage('view:/product');
    }
}
Info: Instead of sending a view to another view as data, you can also use it directly in that view.

 

 

# View Class [5.2.6]


The View:: class was created to perform the functions of exporting and loading views to views. It applies to all views called under data sent with this class. Therefore, it can also be called global data transmission class.

Since it has the same namespace as the controllers, it does not need to be defined with use.

Import View

Used to include view.

Parameters

View::get(string $path, bool $usable = false)
string $path The path of the view to load.
bool $usable = false Whether the view content is transferable.
return mixed

Usage

File: Controllers/Home.php
<?php namespace Project\Controllers;

class Home extends Controller
{
    public function main()
    {
        # Views/home.wizard.php dosyası dahil ediliyor.
        View::get('home');
    }
}
Send Data

The View:: class is mostly used for data export. Data export is valid for all views used after identification. In other words, data is sent in a global scope. It is possible to send not only a value as data, but also another files such as view, script or style.

Parametreler

View::dataName(mixed $dataValue)
5.2.6 mixed $dataValue Gönderilen verinin tutacağı değer.
return mixed

Kullanımlar

<?php namespace Project\Controllers;

class Home extends Controller
{
    public function main()
    {
        View::name('Micheal')
            ->surname('Johnson')
            ->email('[email protected]')
            ->get('home');
    }
}

In the example above, $name, $surname and $email data will be available in views loaded later.

File: Views/home.wizard.php
{{ $name }} - {{ $surname }} - {{ $email }}
Micheal - Johnson - michea@johnson
Send Content

As well as sending data to views, a view content or a style / script links can also be sent. In fact, the following codes show some kind of use of Import:: methods.

<?php namespace Project\Controllers;

class Home extends Controller
{
    public function main()
    {
        View::emailTemplate('template:email')  # Resources/Templates/email.wizard.php
            ->tableView('view:table')          # Views/table.wizard.php
            ->listView('page:list')            # Views/list.wizard.php
            ->jquery('script:js/jquery')       # Themes/Default/js/jquery.js
            ->bootstrap('style:css/bootstrap') # Themes/Default/css/bootstrap.css
            ->get('home');  
    }
}

In the above usage, we see that various contents are sent as data. The next parameters in these references are the same as the parameter string of Import:: methods.

<?php namespace Project\Controllers;

class Home extends Controller
{
    public function main()
    {
        View::tableView('view:table', ['name' => 'My Table']) # Views/table.wizard.php
            ->get('home');  
    }
}