4 ms·
The most common pattern to treat html as string is to use a a template language that works by replacing some patterns like {$variable} in the html with real dat
by givan 3y ago
The most common pattern to treat html as string is to use a a template language that works by replacing some patterns like {$variable} in the html with real data from a database.
A better way is to use html as what it really is, a dom document and with a simple glue language fill it with data, in this way you keep the html code unchanged for better maintainability and reuse.
For example a very basic way is to use a list of css selectors with the data that you want to fill eg:
div#id > span.class a = "lorem ipsum"
#product > .product-title = $product.title
Something like https://github.com/givanz/vtpl https://github.com/givanz/vtpl
- cnity 3y agoYou know, I'm not sure why I've thought of it this way but you're right. Interestingly this fits the model for most JS SPAs, but I've never seen it done server-side.
- TylerE 3y agoCSS selectors get really annoying when you need to assign something to the 7th column in the 4th row. Most browsers don't even support the full set of selectors they're already supposed to! Realistically this could result in having ugly ids on everything just because writing things with selectors sucks.
- Izkata 3y agoFor a strange idea, TinyButStrong is a PHP template language that works halfway between this and traditional templating: You work in an HTML template file, but the engine understands the HTML structure so the values you insert are like vtpl but in reverse. It was designed to work with WYSIWIG HTML editors. Here's their example page for arrays, you can see how the template is completely valid HTML without having to make the engine render it: https://www.tinybutstrong.com/examples.php?e=dataarray&m=template https://www.tinybutstrong.com/examples.php?e=dataarray&m=tem... (then on the upper-left you can click "Result" to see how the "tr" elements, as specified in "block=tr" in the template, are duplicated for the passed-in array)