mercredi 21 septembre 2016

What is the best way to turn a HTML start and end tag into a function?

I have a complex HTML tag, with many attributes and it appears in very different parts of the code.

Example:

<div class="blabla" data-test="blablabla" ... data-another-attribute="blabla" >
    <some html code ... >
</div>

And I do not want to repeat this <div></div> with all its attributes in different parts of the code as it changes quite often during development.

If I create a function like this (example in PHP):

function myDivStart() { ?>
    <div class="blabla" data-attribute="blablabla" data-another-attribute="blabla">
<?php }

then my resulting code would look like

<?php myDivStart() ?>
    <some html code ... >
</div>

and the finishing </div> would look kind of out-of-place, since there is no visual starting <div>. My text editor would also not parse this correctly and syntax highlighting is messed up.

Then, if I create another function for the closing </div>, it would be a very silly function indeed:

function myDivEnd() { ?>
    </div>
<?php }

and turn the original code into

<?php myDivStart() ?>
    <some html code ... >
<?php myDivEnd() ?>

This would solve the syntax highlighting problem, but it still feels very unclean to have such a silly function to close.

Is there any pattern that would solve for this?

Aucun commentaire:

Enregistrer un commentaire