Skip to main content

Setting Source File

Simplifying Callback Logic with Source Set Function In Phlame, when handling callbacks for routes, lengthy logic can lead to cluttered code. To mitigate this issue and improve code organization, it's recommended to utilize the Source::set() function. This function allows developers to manage the content of their pages effectively while keeping the callback logic concise.

Streamlining Content Management with Source Set

By using the Source::set() function, you can seamlessly manage the content displayed on your web pages. Rather than embedding complex logic directly within the callback functions, you can simply set the appropriate source file using Source::set(). This approach enhances code readability and maintainability, making it easier to manage the application's structure.

Creating Source Files

To create a source file, you need to navigate to the ./source folder and create a file with the following structure: <filename>.src.php. Once the source file is created, you can use the Source::set() function to specify the file to be used for rendering the page content like this:

Source::set('<filename>');

Passing Variables to Source Files

you can pass variables to source files by using the Source::set() function with an additional parameter. For example:

Source::set('filename', ['variablename' => 'variable value', ...]);

This allows for dynamic content rendering based on the provided variables, enhancing the flexibility of the application.

Controlling Content Type

By default, the Source::set() function assumes a content type of JSON. However, you can specify other content types by passing the desired type as a parameter. For example:

To specify no content type:

Source::set('filename', format: Source::nulltype());

To specify a different content type:

Source::set('filename', format: 'content/type');

This flexibility enables you to tailor the content type according to their application's requirements, ensuring compatibility with various data formats and rendering methods.

Example of a Phlame Setting Source

index.php
<?php
session_start();
require_once '.config/_init.php';

$router = new Router();

// Add a static route
$router->addRoute('/<name>', function($name) {
Source::set('index', ['username' => $name]);
});

// More routes can be created

$router->route();
?>

Code in source file

source/index.src.php
<?php
echo json_encode(["Result" => $username]);
?>

This is a simple example of source file setting

note

It is essential that the source file is found withing the ./source folder and follows the format <filename>.src.php.

info

You can use the method Source::empty() to set value to a variable if it is missing. Example

Source::empty($page, "newpage");

will check if the variable $page exists, if not it'll create the variable and set its value as newpage.