Created
August 4, 2020 00:44
-
-
Save royashbrook/2ddb1a7cf9081c5f046b713a60c1867f to your computer and use it in GitHub Desktop.
Generic javascript function to generate other html wrapper functions.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// this simply generates helper html wrappers | |
// whatever tags you want helpers for can be listed in the htmlhelpers array | |
const htmlhelpers = ['h4','table','thead','tbody','tr','td','th','div']; | |
// this function generates other functions based on the tag that is passed into it | |
// also any extra attributes or other properties can be added as an argument | |
const tagwrapper = (tag,txt,att='')=>`<${tag}${att.length===0?'':' ' + att}>${txt}</${tag}>`; | |
// loop through the tags we want wrapper functions for, and generate eval statements | |
for(const s of htmlhelpers){eval(`var ${s} = (txt,att='')=>tagwrapper('${s}',txt,att);`)} | |
// the output of this will be functions that allow you to write something like: | |
// h4('hello world') | |
// and get an output of | |
// <h4>hellow world</h4> | |
// this process can be accomplished with simple functions of the names listed, and in | |
// general i would say utilizing string literals and templates is better, but i find | |
// this nice when i am prototyping sometimes or if i am using arguments to generate | |
// some text. ymmv. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment