Skip to main content

Loading Configurations

In Quanta, configuration files are used to store various settings for the application, such as assets, database credentials, or other parameters. These configurations can be loaded and parsed easily, allowing you to centralize and manage important values.

Example Configuration File

In this example, we'll demonstrate how to load a configuration file containing details about assets (such as CSS and JavaScript files to load). The configuration file might look something like this:

{
"assets": [
{
"name": "bootstrap-css",
"type": "link",
"params": {
"rel": "stylesheet",
"href": "https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css"
}
},
{
"name": "code-highlight-css",
"type": "link",
"params": {
"rel": "stylesheet",
"href": "https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.11.1/styles/dark.min.css"
}
},
{
"name": "site-style",
"type": "link",
"params": {
"rel": "stylesheet",
"href": "{QUANTA_DOMAIN}/style/style.css"
}
},
{
"name": "bootstrap-icons",
"type": "link",
"params": {
"rel": "stylesheet",
"href": "https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css"
}
},
{
"name": "code-highlight-js",
"type": "script",
"params": {
"src": "https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js"
}
},
{
"name": "code-highlight-languages-js",
"type": "script",
"params": {
"src": "https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/languages/go.min.js"
}
}
]
}

This config.json file includes an array of assets to be loaded, such as stylesheets and scripts. Each asset contains a name, type (link or script), and parameters needed to load the asset (like the href for CSS or src for JavaScript).

Loading a Configuration File in Quanta

To load and process the configuration file in Quanta, use the loadConfig method. This method reads the configuration file and parses the JSON content, allowing you to access the values within your application.

Example Code to Load the Configuration:

$quanta->loadConfig(__DIR__ . "/config/config.json");

Here, loadConfig loads the JSON configuration from the specified file path. The method is flexible and can be used to load configurations in different formats (e.g., JSON, YAML).

How to Handle the Loaded Configuration

Once the configuration file is loaded, you can access and use its contents. For example, if the configuration contains assets, you can loop through the assets and render them on the page.

Example: Rendering Assets from the Configuration

<?php echo $quanta->renderAssets("link"); ?>

<!-- Code Highlight JS and Styles -->
<?php echo $quanta->renderAsset("code-highlight-js"); ?>
<?php echo $quanta->renderAsset("code-highlight-languages-js"); ?>

Conclusion

The loadConfig method in Quanta makes it easy to load and manage configuration files. Whether you are dealing with assets, database credentials, or other settings, Quanta provides a convenient way to centralize and load this configuration. Once loaded, the configuration data can be accessed throughout the application, enabling a flexible and maintainable codebase.