My WordPress plugin creates a few shortcodes that return blocks of HTML.
When I register the shortcodes, I do so like this:
add_shortcode('bb-loans-form', function() {
return Shortcodes::loanApplicationForm();
});
And here is the static method from the Shortcodes class:
public static function loadApplicationForm()
{
$form = new AdamWathanFormFormBuilder;
$html = $form->open()->action('/apply')->class('bb-loan-form');
$html .= '<div class="bb-form-field">';
$html .= '<h2>Loan Application Number</h2>';
$html .= $form->text('loan_app_number')->id('loan-app-number');
$html .= $form->submit('Continue Loan');
$html .= '</div>';
$html .= $form->close();
return $html;
}
This is very cumbersome, and messy. I don’t like outputting the HTML like this. I’ve also used Heredoc, but I had to use string substitution to include important values when the form is rendered.
Is there a better way to store my HTML files? I don’t want these files publicly accessible. They would have to live in my plugin directory.
It’s not a huge plugin, so I’m not overly concerned, but I’d like to know for future reference if there’s a cleaner way to include the needed HTML.
You could just use a single string with concatenations…
It at least keeps the HTML aligned.
I also don’t really see an issue with Heredoc, as long as you assign variables and substitute them in: