zellien/zellien-mjml

PHP library for rendering MJML templates

v1.0.0 2023-06-22 16:50 UTC

This package is auto-updated.

Last update: 2024-10-22 20:06:43 UTC


README

PHP library for rendering MJML templates

Static Badge Static Badge

Installation

The preferred method of installation is via Composer. Run the following command to install the package and add it as a requirement to your project's

composer require zellien/zellien-mjml

Usage

Create an instance of the MjmlRenderer class by passing the configuration array to the constructor. The configuration array should contain the application_id and secret_key as non-empty strings.

Call the render method on the $renderer object, passing the MJML template as a non-empty string. It will return the rendered HTML as a non-empty string.

$mjmlTemplate = '<mjml>Your MJML template goes here</mjml>';
$renderedHtml = $renderer->render($mjmlTemplate);

Handle the potential exceptions that can be thrown by the render method:

  • BadRequestException if the request is invalid.
  • UnauthorizedException if authentication fails.
  • ForbiddenException if the request is unauthorized.
  • UnexpectedErrorException if an unexpected error occurs.
try {
    $renderedHtml = $renderer->render($mjmlTemplate);
} catch (BadRequestException $e) {
    // Handle bad request error
} catch (UnauthorizedException $e) {
    // Handle unauthorized error
} catch (ForbiddenException $e) {
    // Handle forbidden error
} catch (UnexpectedErrorException $e) {
    // Handle unexpected error
}

Additional Notes

Make sure you have the necessary dependencies installed, such as the cURL extension.

Ensure that the API URL (https://api.mjml.io/v1/render) is accessible from your environment.

That's it! You can now utilize the MjmlRenderer library in your project to render MJML templates using the API.