For a wordpress shortcode I have to return a string containing HTML markup and I'm wondering what the best way to build/store it is.
My first attempt was an unreadable mess of concatenated parts like this:
$output = '<div ';
if( $atts['ID'] ) {
$output .= 'id="'.$atts['ID'].'"';
}
$output .= ' class="responsive-map icon-'.$atts['icon'].'"';
Thanks to sprintf()
I could trim it down into a much more readable shape:
$format = '
<!--[if lt IE 9]>
<div %1$s class="ie responsive-map %2$s" %3$s>
<![endif]-->
<!--[if !(lt IE 9)]><!-->
<div %1$s class="responsive-map %2$s" %3$s>
<!--<![endif]-->
<iframe src="%4$s"></iframe>
</div>
';
$output = sprintf( $format, $id, $icon, $height, $content );
I wonder if there is a better way to do this, especially if the complexity of the markup and number of variable parts goes up. Usually I would simply escape the HTML but in this case that is not an option as wordpress will always place the escaped HTML at the top of a post rather than the intended placement.
First, a better approach then storing the HTML in template at all could be to use a template engine, using template files.
While you could use a double quoted multiline string, I would use a here document . You can interpolate variables like in a double quoted string:
$format = <<<EOF
<!--[if lt IE 9]>
<div $variable1 class="ie responsive-map $variable2" %3$s>
<![endif]-->
<!--[if !(lt IE 9)]><!-->
<div $variable3 class="responsive-map $variable4" $variable5>
<!--<![endif]-->
<iframe src="$variable6"></iframe>
</div>
EOF
Compared with multiline double quoted string, it improves readability, will try explain by example:
$format = "<!--[if lt IE 9]> <--- breaks indention
<div $variable1 class=\"ie responsive-map $variable2\" %3$s> <-- need to escape "
<![endif]-->
<!--[if !(lt IE 9)]><!-->
<div $variable3 class=\"responsive-map $variable4\" $variable5>
<!--<![endif]-->
<iframe src=\"$variable6\"></iframe>
</div>";
There are many ways to achieve what you want with PHP. If readability is the most important thing for you simply use HEREDOC or NOWDOC.
<?php
$output = <<<HTML
<!--[if lt IE 9]>
<div {$var} class="ie responsive-map {$var}" {$var}>
<![endif]-->
<!--[if !(lt IE 9)]><!-->
<div {$var} class="responsive-map {$var}" {$var}>
<!--<![endif]-->
<iframe src="{$var}"></iframe>
</div>
HTML;
I hope you are targeting the view part of the MVC pattern . In this case you could do use it just like this:
<!--[if lt IE 9]>
<div <?= $format ?> class="ie responsive-map <?= $id ?>" <?= $icon ?>>
<![endif]-->
<!--[if !(lt IE 9)]><!-->
<div <?= $format ?> class="responsive-map <?= $id ?>" <?= $icon ?>>
<!--<![endif]-->
<iframe src="<?= $height ?>"></iframe>
</div>
It's important that you hold this source in a seperate file form our controller and models which hold the actual logic and data.
Here you can find an introduction: http://php-html.net/tutorials/model-view-controller-in-php/
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.