php - What's a cleaner way to output HTML from a Wordpress plugin? -


my wordpress plugin creates few shortcodes return blocks of html.

when register shortcodes, this:

    add_shortcode('bb-loans-form', function() {         return shortcodes::loanapplicationform();     }); 

and here static method shortcodes class:

public static function loadapplicationform() {     $form = new \adamwathan\form\formbuilder;      $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 cumbersome, , messy. don't outputting html this. i've used heredoc, had use string substitution include important values when form rendered.

is there better way store html files? don't want these files publicly accessible. have live in plugin directory.

it's not huge plugin, i'm not overly concerned, i'd know future reference if there's cleaner way include needed html.

you use single string concatenations...

$form = new \adamwathan\form\formbuilder; $html = $form->open()->action('/apply')->class('bb-loan-form') .            '<div class="bb-form-field">                 <h2>loan application number</h2>' .                 $form->text('loan_app_number')->id('loan-app-number') .                  $form->submit('continue loan') .            '</div>' .         $form->close();  return $html; 

it @ least keeps html aligned.

i don't see issue heredoc, long assign variables , substitute them in:

$form = new \adamwathan\form\formbuilder;  $form_start = $form->open()->action('/apply')->class('bb-loan-form'); $loan_app = $form->text('loan_app_number')->id('loan-app-number'); $submit = $form->submit('continue loan'); $form_end = $form->close();  $html = <<<html     {$form_start}         <div class="bb-form-field">             <h2>loan application number</h2>             {$loan_app}             {$submit}         </div>     {$form_end} html;   return $html; 

Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -