I was wondering whether it is "best practice" or even recommended for my case to use a string that contains variable expressions, ex:
$block = <<<HTML
<div class="image">
<img src="{$image_url}" />
</div>
HTML;
The problem with using such strings is that I have to use them in a function so that they don't get called before their variables are defined. ex:
function image() {
if( have_rows('image') ) {
while ( have_rows('image') ) {
the_row();
$image = get_sub_field('image');
$image_url = $image['url'];
}
return <<<HTML
<div class="image">
<img src="{$image_url}" />
</div>
HTML;
}
}
Instead of being able to pass in the string from outside the function so that I can abstract the functionality (the if/while statment, get_sub_field), I have to define the string inside the function so that I don't get a "cannot find variable $image_url" error.
Is there a better way of doing this? Perhaps an object-oriented way, or is functional programming best?
Aucun commentaire:
Enregistrer un commentaire