{"id":17549828,"url":"https://github.com/andydevs/general","last_synced_at":"2025-03-29T06:48:51.887Z","repository":{"id":56873876,"uuid":"52054303","full_name":"andydevs/general","owner":"andydevs","description":"General is a simple templating system in ruby that allows you to create templates from both pure strings and files (with the extension .general), as well as create new strings and files with these created objects.","archived":false,"fork":false,"pushed_at":"2020-02-08T23:21:28.000Z","size":173,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-05T08:33:44.885Z","etag":null,"topics":["ruby","templates"],"latest_commit_sha":null,"homepage":"http://andydevs.github.io/general","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/andydevs.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-02-19T02:07:57.000Z","updated_at":"2021-01-19T16:07:21.000Z","dependencies_parsed_at":"2022-08-20T22:30:25.844Z","dependency_job_id":null,"html_url":"https://github.com/andydevs/general","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andydevs%2Fgeneral","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andydevs%2Fgeneral/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andydevs%2Fgeneral/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andydevs%2Fgeneral/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/andydevs","download_url":"https://codeload.github.com/andydevs/general/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246150409,"owners_count":20731419,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ruby","templates"],"created_at":"2024-10-21T03:04:40.815Z","updated_at":"2025-03-29T06:48:51.831Z","avatar_url":"https://github.com/andydevs.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# General\n\n\tHello, I am @(name: gordon ramsay -\u003e capitalize all) and I like @(food: cat food)!\n\nGeneral is a simple templating system in ruby that allows you to create templates from both pure strings and files (with the extension .general), as well as create new strings and files with these created objects.\n\n## Table of Contents\n\n- [General Templates](#general-templates)\n\t- [Basic Templates](#basic-templates)\n\t- [Array Templates](#array-templates)\n\t- [Placeholder Operations](#placeholder-operations)\n\t\t- [To-Array Operations](#to-array-operations)\n- [General Time Format](#general-time-format)\n- [General IO](#general-io)\n\t- [PrePartials](#prepartials)\n\t\t- [Including Other Templates](#including-other-templates)\n\t\t- [Extending Templates](#extending-templates)\n- [Special Characters](#special-characters)\n\n## General Templates\n\n### Basic Templates\n\n\t\"Hello, I am @(name: Gordon Ramsay) and I like @(food: cat food)!\"\n\nA general template consists of regular text along with placeholders, which are defined as follows: `@(-name-)`. You can also specify default text as well like so: `@(-name-: -default text-)`.\n\nHere's an exmple of a general template text: `\"Hello, I am @(name: Gordon Ramsay) and I like @(food: cat food)!\"`\n\nTo import all classes in the library, type `require 'general'`. You can then create a general template from a string within your ruby code using the GTemplate object:\n\n```ruby\ntemplate = General::GTemplate.new \"Hello, I am @(name: Gordon Ramsay) and I like @(food: cat food)!\"\n```\n\nTo generate a string from the template, use the \"apply\" method and add your data as named arguments.\n\n```ruby\nstring = template.apply name: \"Joe\", food: \"Joe's Schmoes\"\n\n# string now equals \"Hello, I am Joe and I like Joe's Schmoes\"\n```\n\nIf you don't specify a value for a particular placeholder, it will take on it's default value.\n\n```ruby\nstring = template.apply food: \"Joe's Schmoes\"\n\n# string now equals \"Hello, I am Gordon Ramsay and I like Joe's Schmoes\"\n```\n\nIf you have an array of data, you can use `apply_all` to apply the template to each individual element separately.\n\n```ruby\narray = template.apply_all [{name: \"Joe\", food: \"Joe Schmoes\"}, {name: \"Jane\", food: \"Jane's Danes\"}, {name: \"Denny\", food: \"Denny's Fennies\"}]\n\n#array now has these elements:\n#   - Hello, I am Joe and I like Joe's Schmoes\n#   - Hello, I am Jane and I like Jane's Danes\n#   - Hello, I am Denny and I like Denny's Fennies\n```\n\n### Array Templates\n\n\t@[people]\n\t\tHello, @(name)! How's the @(pet)?\n\t@[]\n\nArray templates will format each value in an array of data according to a general template\n\nA general array template is as follows: `@[-name-] -general template for each value- @[]`. You can also specify the delimeter, which will be appended to the end of each element. The delimeter should be added in the end tag, like so: `@[-name-] -general template for each value- @[-delimeter-]`. If no delimeter is given, the default is a newline. The start and end tags are invariant with atleast 1 whitespace or newline. So you can also define the template like such: \n\n```\n@[-name-]\n-general template for each value-\n@[-delimeter-]\n```\n\nAn example of an array template:\n\n```ruby\ntemplate = General::GTemplate.new \\\n\"List of Film Crew:\n@[crew] \n\t@(name): @(role)\n@[]\"\n```\n\nYou can now specify an array of values for \"crew\" in template, and it will format the data accordingly:\n\n```ruby\nstring = template.apply crew: [\n\t{name: \"Chris Nolan\", role: \"Director\"}, \n\t{name: \"Wally Pfister\", role: \"Director of Photography\"}, \n\t{name: \"Christian Bale\", role: \"Bruce Wayne/Batman\"}, \n\t{name: \"Michael Caine\", role: \"Alfred Pennyworth\"}\n]\n\n# string now equals:\n# \"List of Film Crew:\n#  \t\tChris Nolan: Director\n#\t\tWally Pfister: Director of Photography\n#\t\tChristian Bale: Bruce Wayne/Batman\n#\t\tMichael Caine: Alfred Pennyworth\"\n```\n\n### Placeholder Operations\n\n\t@(name -\u003e capitalize all)\n\nYou can also specify operations to be performed on values passed to placeholders, akin to AngularJS's filters. For example: `@(name -\u003e capitalize)` will capitalize whatever name is inputted before applying it to the text. Placeholder operations also have arguments, which change behaviour. `@(name -\u003e capitalize first)` will capitalize the first word of name, whereas `@(name -\u003e capitalize all)` will capitalize all words in the name.\n\n- capitalize: capitalizes the string\n\t- Argument 1:\n\t\t- 'first': only first word is capitalized\n\t\t- 'all': all words are capitalized\n\t\t- default: 'first'\n- uppercase: converts a string to uppercase\n- lowercase: converts a string to lowercase\n- money: converts an integer to a monetary amount in the given format\n\t- Argument 1:\n\t\t- 'USD': United States Dollar\n\t\t- 'EUR': European Euro\n\t\t- default: 'USD'\n- time: converts an integer to a formatted time in the given time format\n\t- Argument 1:\n\t\t- Time format to set to\n\t\t- default: '@I:@MM:@SS @A'\n\n#### To-Array Operations\n\n\t@[groceries -\u003e split]\n\t\tI need to get some @#!\n\t@[\\n].\n\nCertain operations can convert data values to arrays to be used by an array template. These can be applied to array placeholders as follows `@[text -\u003e split] -general- @[].` These return pure data values, and they can be accessed by a special placeholder `@#` which applies the entire data value as is.\n\n```ruby\ntemplate = General::GTemplate.new \"@[text -\u003e split] -\u003e @# @[\\n]\"\nstring = template.apply text: \"Good Afternoon World\nI am Chef Gordon Ramsay\"\n\n# string now equals\n# \"-\u003e Good Afternoon World\n# -\u003e I am Chef Gordon Ramsay\"\n```\n\n- split: splits a string based on a given delimeter\n\t- Argument 1:\n\t\t- The delimeter regex to split the string by\n\t\t- default: \\r?\\n\n- splitwords: splits a string at a given number of words\n\t- Argument 1:\n\t\t- The words at which to split the string\n\t\t- default: 10\n\n## General Time Format\n\nGeneral offers a separate special template called a GTimeFormat used for formatting times. The GTimeFormat syntax is also used by the time operation. \n\nTime format placeholders are written as an @ followed by one or more characters of the same letter: `@LL...` (except for `@A`) The letter indicates the part of time information to format and the number of letters indicates the number of zeros.\n\n| Letter |       Description       |\n|:-------|:------------------------|\n| @H...  | Hour (0 to 23)          |\n| @I...  | Time interval (1 to 12) |\n| @M...  | Minutes (0 to 59)       |\n| @S...  | Seconds (0 to 59)       |\n| @A     | AM/PM                   |\n\n## General IO\n\nYou can also write to files using GIO, a general template capable of writing to files. You can create a GIO like a GTemplate:\n\n```ruby\ngio = General::GIO.new \"Hello, I am @(name: Gordon Ramsay) and I like @(food: cat food)!\"\n```\n\nYou can also load a GIO from a file. For example, here's how you create a template file from the file \"example.general\"\n\n```ruby\ngio = General::GIO.load \"example.general\"\n```\n\nTo write to a file, simply call the write method, pass in the file and the data to apply (like in GTemplate#apply):\n\n```ruby\ngio.write file, name: \"Joe\", food: \"Joe's Schmoes\"\n```\n\nWhere `file` is the file you are writing to. You can also pass the name of the file to write to as well:\n\n```ruby\ngio.write \"example.txt\", name: \"Joe\", food: \"Joe's Schmoes\"\n```\n\nTo get the original source filename of the GIO, just call `source`\n\n```ruby\ngio.source # == \"example.general\"\n```\n\n### PrePartials\n\n\t@@extend basic_layout\n\t@@include header\n\n\t\u003cp\u003eI am a subtemplate! My name is @(name -\u003e capitalize all)\u003c/p\u003e\n\nPrePartials are parsed before the general template is parsed, and perform file template manipulation like including or extending another template.\n\n#### Including Other Templates\n\nFile to include\n\n\t@(title)\n\t@(author)\n\t@(subject)\n\t@(date)\n\nIncluding file\n\n\t@@include file\n\n\t@(content)\n\nResulting File\n\n\t@(title)\n\t@(author)\n\t@(subject)\n\t@(date)\n\n\t@(content)\n\nTo include another template file in your file, you use `@@include [name of template]` (excluding the suffix). This will read the corresponding template file and append the content in place of the `@@include`.\n\nNOTE: `@@include` requires a new line or carriage return to be parsed (it takes up one entire line). It also appends a newline at the end of the included template.\n\n#### Extending Templates\n\nFile to Extend\n\t\n\t\u003c!doctype html\u003e\n\t\u003chtml\u003e\n\t\u003ctitle\u003e@(title -\u003e capitalize all)\u003c/title\u003e\n\t\u003cbody\u003e\n\t@@yield\n\t\u003c/body\u003e\n\t\u003c/html\u003e\n\nExtending File\n\n\t@@extend file\n\n\t\u003ch1\u003e@(name)'s Profile!\u003c/h1\u003e\n\t\u003cp\u003eFavorite Color: @(color)\u003c/p\u003e\n\t\u003cp\u003eFavorite Sport: @(sport)\u003c/p\u003e\n\nResulting File\n\n\t\u003c!doctype html\u003e\n\t\u003chtml\u003e\n\t\u003ctitle\u003e@(title -\u003e capitalize all)\u003c/title\u003e\n\t\u003cbody\u003e\n\t\u003ch1\u003e@(name)'s Profile!\u003c/h1\u003e\n\t\u003cp\u003eFavorite Color: @(color)\u003c/p\u003e\n\t\u003cp\u003eFavorite Sport: @(sport)\u003c/p\u003e\n\t\u003c/body\u003e\n\t\u003c/html\u003e\n\nExtending a file wraps the contents of the file around the current template. This is good when many templates will share a single common layout. Extend prepartials are as follows `@@extend [name of template]` (also excluding extension). The `@@extend` must be defined at the beginning of the template, and there can be only one `@@extend` in any template.\n\nThe extending file should define a `@@yield` where the file will be extended from. This is by default at the end of the file. Note that if `@@yield` is defined in a template, the template is considered a meta-template and cannot be parsed. Also, like extend, only one `@@yield` statement can be defined in a single file.\n\n## Special Characters\n\nSpecial characters can be entered into general using the format `@[code];`. The table of codes are below:\n\n| Code | Character |\n|:----:|:---------:|\n|  at  |     @     |\n|  pd  |     #     |\n|  lt  |     \u003c     |\n|  gt  |     \u003e     |\n|  op  |     (     |\n|  cp  |     )     |\n|  ob  |     [     |\n|  cb  |     ]     |\n|  oc  |     {     |\n|  cc  |     }     |\n|  ms  |     -     |\n|  ps  |     +     |\n|  st  |     *     |\n|  pc  |     %     |\n|  bs  |     \\     |\n|  fs  |     /     |\n|  dl  |     $     |\n\n\n## Notes\n\nRegex and matching have now been officially depricated, and will be removed in General 2.1.0.\n\n-------------------------------------------------------------------------------------------------------------------------------------\nAnshul Kharbanda\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fandydevs%2Fgeneral","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fandydevs%2Fgeneral","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fandydevs%2Fgeneral/lists"}