Package: tmple
tmpleGeneral files or content generator, usingtemplateanddata/variables.Content of the file will be controlled by thetemplate(inJinja),
and you can control thetemplateusingdata/variables(inYaml).Use CasesGenerate simple file from templateGenerate multiple filesJust printRecipe ConceptInstallationUsageUse Cases👨‍👩‍👦 Generate simple file, based on a templateLet say you want to generate a repeated SQL file like this:WITHtop_user(SELECTname,phone,address,gender,birth_dateFROMtop_userLIMIT10),mid_user(SELECTname,phone,address,gender,birth_dateFROMmid_userLIMIT10),low_user(SELECTname,phone,address,gender,birth_dateFROMlow_userLIMIT10),base_user(SELECTname,phone,address,gender,birth_dateFROMbase_userLIMIT10)SELECT*top_userUNIONALLSELECT*mid_userUNIONALLSELECT*low_userUNIONALLSELECT*base_user*just imagine it has 1000 lines with another repetitive complex query😂Now, what you need to do, is having a template! a Jinja template!WITH{%-fortbintables%}{{tb.name}}AS (SELECT{%forcincolumns%}{{c}}{{','ifnotloop.last}}{%endfor%}FROM{{tb.name}}LIMIT{{tb.limit}}){{','ifnotloop.last}}{%-endfor%}{%fortbintables%}SELECT * FROM{{tb.name}}{{'UNION ALL'ifnotloop.last}}{%-endfor%}Save it to.jinja2file to your folderworking_dir/sample.jinja2But for sure that 👆 template can't generate by it self, it needs a data or variables to work on, right?
Let's just create a yaml file with this format:var:#hold all variables/dictionary that needed in your jinja filestables:-name:top_userlimit:10-name:mid_userlimit:20-name:low_userlimit:10-name:base_userlimit:30columns:-name-phone-address-gender-birth_dategenerator:-template:sample.jinja2#where you save your jinja filedestination:-output.sql-log# to also print the result on your terminalSave it to.yamlfile to your folder asworking_dir/recipe.yamlDone! above yaml is what we callrecipe, for more detail, please take a lookRecipe Conceptsection.
Now, lets run your recipe! and see the result!cdworking_dir

tmple--reciperecipe.yamlUh oh, you can't run tmple yet?install firstman! 😂😂Generate list of files based on multiple templatesWhen you expect to generate multiple files, you can do setup on the recipe it self!You can do write to multiple files for the same template, it will generate the same content but in different filesvar:variablesgenerator:-template:samples/templates/sample.jinja2destination:-samples/results/sample.sql-/Users/personname/folder/tmple/src/results/simple.sql-sample/result/log.txtbut if let say you need to generate with different template, then do this instead:var:variablesgenerator:-template:samples/templates/sample.jinja2destination:-samples/results/sample.sql-/Users/personname/folder/tmple/src/results/simple.sql-template:samples/templates/sample2.jinja2destination:-samples/results/sample2.sql-template:samples/templates/sample3.jinja2destination:-samples/results/sample3.sqlWhen do this, make sure you have all variables setup invar:that needed from all your template.Just print, not generate filesThis will generate content fromsample.jinja2but will not generate a file, it will only do log in your terminal.
You need adddestination:to generate a file.var:variablesgenerator:-template:samples/templates/sample.jinja2This also only do log/print onlyvar:variablesgenerator:-template:samples/templates/sample.jinja2destination:logRecipe conceptRecipe is a config file to tell thetmpleabout:all the variables that needed in yourtemplates(var:)which template that you gonna use to generate file (generator:template:)where to save the generated content from your template (generator:destination:)var:# mandatoryany_variable:you needyou_can:-write-down-hereand_also:it_support:nested:variables!# above variables it just mentionable in the Jinja files# like {{ it_also.support.nested }}extend:# optional-samples/recipes/parent_recipe.yaml# do inherit# all variables or generator written from this parent_recipe.yaml# will be also extended into current recipegenerator:# mandatory-template:samples/templates/sample.jinja2destination:-samples/results/sample.sql# directly create a file in this relateve path-/Users/personname/folder/tmple/src/results/simple.sql#absolute path to place to some folder-log# just print in the log of your terminal# you could also generate multiple files in different template using the same variables-template:samples/templates/sample2.jinja2# if there is no destination key here, then it will only print the generated contentInstallationInstall using pip / pip3pipinstalltmpleUsageRunning from terminaltmple-rpath/your_recipe.yamlortmple--recipepath/your_recipe.yamlRunning from python codefromtmple.recipeimportReciperecipe=Recipe.from_path('your_recipe_path.yaml')recipe.generate()Running from tmple repositorycdtmple
python3main.py-ryour_recipe.yaml