Render Element content

Basic Structure

The layout file should be a PHP template that renders your element's HTML. Here's the basic structure:

Important Notes

  • Security:

    • Always use defined('ABSPATH') || exit;

    • Escape all output using appropriate functions

    • Sanitize all input data

  • Styling:

    • Use TemplateHelpers::get_style() for consistent styling

    • Use TemplateHelpers::get_spacing_value() for padding/margin

    • Use TemplateHelpers::get_font_family_value() for fonts

  • Content Processing:

    • Use do_shortcode() for shortcode processing

    • Use yaymail_kses_post() for HTML content

    • Use output buffering for complex HTML structures

<?php

defined('ABSPATH') || exit;

use YayMail\Utils\TemplateHelpers;

if (empty($args['element'])) {
    return;
}

$element = $args['element'];
$data    = $element['data'];

// Define styles
$wrapper_style = TemplateHelpers::get_style( [
    'word-break'       => 'break-word',
    'background-color' => $data['background_color'] ?? '#fff',
    'padding'          => TemplateHelpers::get_spacing_value( $data['padding'] ?? [] ),
] );

$title_style = TemplateHelpers::get_style( [
    'text-align'    => yaymail_get_text_align(),
    'color'         => $data['title_color'] ?? 'inherit',
    'margin-bottom' => '10px',
    'font-size'     => '18px',
    'font-weight'   => 'bold',
    'font-family'   => TemplateHelpers::get_font_family_value( $data['font_family'] ?? 'inherit'),
] );

// Process content
$title = isset( $data['title'] ) ? do_shortcode( $data['title'] ) : '';
$content = isset( $data['rich_text'] ) ? do_shortcode( $data['rich_text'] ) : '';

// Build HTML
ob_start();
?>
<div class="your-element">
    <h2 style="<?php echo esc_attr( $title_style ); ?>">
        <?php echo yaymail_kses_post( $title ); ?>
    </h2>
    <div class="content">
        <?php echo yaymail_kses_post( $content ); ?>
    </div>
</div>
<?php
$element_content = ob_get_clean();

// Wrap and output
TemplateHelpers::wrap_element_content( $element_content, $element, $wrapper_style );

Last updated

Was this helpful?