I am trying to make the following bit of code easier to maintain. I am not a web developer so bear with me. I think the following approach is appropriate.
I would like to dynamically add content and attributes to an html file using either javascript or jQuery. The items could reside in a .csv or .json (or something else?) file.
Given content like this
<div class="filtr-container">
<div class="col-12 col-sm-6 col-md-4 card filtr-item" data-category="cat-1" data-date="2018-02-09">
<div class="card-inner-border box-shadow">
<a href="address-1.html">
<img class="card-img-top rounded-top" src="./images/image-1.jpg" alt="img-2-alt">
</a>
<div class="card-body">
<h5 class="card-title">Title-1</h5>
<p class="card-text card-desc">
This is a description for title-1 content.
</p>
<a href="address-1.html">
<button type="button" class="btn btn-sm btn-outline-secondary">View</button>
</a>
<p class="card-text">
<small class="text-muted">Last updated February 2, 2018</small>
</p>
</div>
</div>
</div>
<div class="col-12 col-sm-6 col-md-4 card filtr-item" data-category="cat-2, cat-3" data-date="2018-02-14">
<div class="card-inner-border box-shadow">
<a href="address-2.html">
<img class="card-img-top rounded-top" src="./images/image-2.jpg" alt="img-2-alt">
</a>
<div class="card-body">
<h5 class="card-title">Title-2</h5>
<p class="card-text card-desc">
Here is a long description for title-2 content.
</p>
<a href="address-2.html">
<button type="button" class="btn btn-sm btn-outline-secondary">View</button>
</a>
<p class="card-text">
<small class="text-muted">Last updated February 14, 2018</small>
</p>
</div>
</div>
</div>
<!-- MANY MORE CARDS / ITEMS ... -->
</div> <!-- End of filtr-container -->
I think we could abstract the details into something like this (.csv)
item-id,title,description,categories,address,image,image-alt,update
1,Title-1,This is a description for title-1 content.,cat-1,address-1.html,image-1.jpg,img-1-alt,2018-02-09
2,Title-2,Here is a long description for title-2 content.,"cat-2, cat-2",address-2.html,image-2.jpg,img-2-alt,2018-02-14
What's a nice approach of attack for using `javascript` or `jQuery` to add this content from the `.csv` or `.json` file?
A few concerns:
- The headers of the
.csvwill not match verbatim (e.g.<p class="card-desc">aligns with the.csvheader ofdescription) - There could be embedded comma-separated items (e.g. item 2 has categories
cat-2, cat-3so it gets quotes"in the.csv-- maybe.jsonwould better (?) or perhaps its a non-issue) - If possible can we reuse the
dateitem for bothdata-date=and the final piece of text<small class="text-muted">which converts the date intoLast updated month-name-long, dd, yyyyinstead ofyyyy-mm-dd. - Some attributes are partial references (e.g. the src for an image is just the final part of the path; stated as
image-1.jpgin the.csvnot./images/image-jpg).
To hopefully help make this feel less complicated, here's a picture with the highlighted elements that could be "referenced" from the .csv file.
To me this feels like:
- Read in the .csv file.
- For each item in the .csv file, append objects to
$(".filtr-container")with the shell layout...
But I'm lost when it comes to the particulars or if that's an appropriate approach.

divelements are currently maintained by hand. So why not store them incsvorjsonfile. I believe the string processing is limited with the exception of the date field? I am also not exactly sure what you mean by your last statement: "If you have the structure, you have the attributes, you have the attribute values, why not just store all that as HTML and allow the browser to do its job?" Can you elaborate?div,image,a,p,textNodeDOM nodes , so how do you cross reference each line of your CSV to the DOM node it modifies? It should become self evident that there is a lot more to think about. Even the single answer you've received has avoided this very topic by simplifying it down to "it's a matter of rendering it to the DOM". Best of luck to you.divelement will have the 'shell' same structure (see the picture) at the bottom; all that changes is the highlighted elements sourced from the csv file. The order of rendering in the DOM does not matter, they can simply be rendered in-order they appear in the.csvfile (there is already a javascript function that will rearrange the "cards" by date upon load).