{"id":17942640,"url":"https://github.com/petercamilleri/mini_erb","last_synced_at":"2025-04-03T13:27:35.866Z","repository":{"id":56883827,"uuid":"169365820","full_name":"PeterCamilleri/mini_erb","owner":"PeterCamilleri","description":"A simplified, streamlined, fast, pure-ruby replacement for erb. Experimental.","archived":false,"fork":false,"pushed_at":"2021-05-19T19:19:33.000Z","size":124,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-04-25T21:06:36.062Z","etag":null,"topics":["erb","ruby","ruby-gem"],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/PeterCamilleri.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-02-06T06:45:46.000Z","updated_at":"2023-03-05T04:20:47.000Z","dependencies_parsed_at":"2022-08-20T23:40:26.277Z","dependency_job_id":null,"html_url":"https://github.com/PeterCamilleri/mini_erb","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PeterCamilleri%2Fmini_erb","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PeterCamilleri%2Fmini_erb/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PeterCamilleri%2Fmini_erb/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PeterCamilleri%2Fmini_erb/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/PeterCamilleri","download_url":"https://codeload.github.com/PeterCamilleri/mini_erb/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247008772,"owners_count":20868422,"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":["erb","ruby","ruby-gem"],"created_at":"2024-10-29T03:06:35.034Z","updated_at":"2025-04-03T13:27:35.842Z","avatar_url":"https://github.com/PeterCamilleri.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# MiniErb\n\nThe mini_erb gem is another spin off from the mysh project. In testing the\nhandlebars embedded Ruby system used there, it was observed that the test\ncode was faster than both the native erb and the faster erubi.\n\nThe mini_erb gem returns to the syntax of erb but is based on the same\nalgorithm of the mysh handlebars.\n\nIn performance it is still faster. This is laid out in this\n[report](https://github.com/PeterCamilleri/mini_erb/blob/master/docs/embbed_ruby_study.pdf)\n.\n\n### Embedding Ruby into text or html\n\nThe use of erb to embed ruby code into text or html is widely covered, so only\na brief summary of this topic is presented here.\n\n1. **Embedding ruby computed values into the text.**\n\nThe following embeds the results of the execution of the ruby into the results.\n\n```ruby\nenv = binding\n\nstr = \"ABCD\u003c%= (1..9).to_a.join %\u003eEFGH\"\nputs MiniErb.new(str).result(env)\n```\nproduces\n\n    ABCD123456789EFGH\n\n2. **Using ruby to control the generated text.**\n\nYou can also use ruby to control what text or html is included in the output.\nThis allows the ruby code to select which text/html is included and even to\nrepeat sections.\n\n```ruby\nx   = 42\nenv = binding\n\nstr = \"\u003c% if x==42 %\u003eLife, the Universe and Everything\u003c% else %\u003eSome stuff\u003c% end %\u003e\"\nputs MiniErb.new(str).result(env)\n```\nproduces\n\n    Life, the Universe and Everything\n\nand\n\n```ruby\nenv = binding\n\nstr = \"\u003c% 5.times { |i| %\u003e \u003c%= i+1 %\u003e sheep \u003c% } %\u003e\"\nputs MiniErb.new(str).result(env)\n```\nproduces\n\n     1 sheep  2 sheep  3 sheep  4 sheep  5 sheep\n\n3. **Control of debug code.**\n\nSometimes, there is a need to add code for debug or testing purposes. In\nproduction, we do not want this code interfering with normal operation but we\nmay not wish to simply delete it. The following example shows some \"debug\"\ncode.\n\n```ruby\nenv = binding\n\nstr = \"Now is the hour of our discontent! \u003c%= \"Answer=42\" %\u003e\"\nputs MiniErb.new(str).result(env)\n```\nproduces\n\n    Now is the hour of our discontent! Answer=42\n\nIn production use, we can turn off that code with:\n```ruby\nenv = binding\n\nstr = \"Now is the hour of our discontent! \u003c%#= \"Answer=42\" %\u003e\"\nputs MiniErb.new(str).result(env)\n```\nand now we get this clean output\n\n    Now is the hour of our discontent!\n\n4. **Removal of unwanted new-lines**\n\nSometimes in formatting the embedded ruby code, it is desired to add extra\nnew-lines to make the code easier to read and understand. These extra lines\nmay not be desirable in the output. These can be controlled as follows:\n\n```ruby\nenv = binding\n\nstr = \u003c\u003c-end_of_string\nABCD\u003c%= (1..9).to_a.join -%\u003e\nEFGH\nend_of_string\n\nputs MiniErb.new(str).result(env)\n```\nproduces\n\n    ABCD123456789EFGH\n\nNote how the output does not contain the spurious new line between the\n\"9\" and \"E\" characters.\n\n### Differences from traditional erb.\n\nThe mini_erb gem supports a subset of the functionality of erb.\nThe following lays out the differences:\n\n* The suppression of new lines with a tag ending in -\u0026#37;\u003e is active by default.\n* The use of code lines starting with a % sign is not supported. Instead use\n\u003c%  ... %\u003e embedded code blocks.\n* In erb, \u003c%% maps to \u003c%. In mini_erb use \u003c\\\u0026#37; instead. The same goes for\n%%\u003e which is replaced by \\\u0026#37;\u003e.\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'mini_erb'\n```\n\nAnd then execute:\n\n    $ bundle\n\nOr install it yourself as:\n\n    $ gem install mini_erb\n\n## Usage\n\nThe use of the mini_erb gem comes in two distinct phases.\n\n1. Creating a mini_erb object. This object contains the source input transpiled\ninto ruby code.\n2. Executing the transpiled code in an execution environment.\n\n### Phase 1\n\nCreating a mini_erb object is done simply with the standard new method. The\ncorresponding initialize method is:\n\n```ruby\ndef initialize(string, safe_level=nil, _=nil, eoutvar='_erbout')\n  @safe_level, @eoutvar = safe_level, eoutvar\n  @filename, @lineno = \"(mini_erb)\", 0\n  @src = compile(string)\nend\n```\n\nwhere:\n\n* *string* is a string containing the source text embedded with ruby code.\n* *safe_level* is an optional parameter controlling the level of distrust of the\nembedded ruby code. While outside the purview of this file, the following gives\na nice summary of the available options:\n\nsafe_level | constraints\n-----------|--------------\n0          | No checking of the use of externally supplied (tainted) data is performed. This is the default.\n≥ 1        | Ruby disallows the use of tainted data by potentially dangerous operations.\n≥ 2        | Ruby prohibits the loading of program files from globally writable locations.\n≥ 3        | All newly created objects are considered tainted and untrusted.\n≥ 4        | Ruby effectively partitions the running program in two. Nontrusted objects may not be modified.\n\n(Source: Programming Ruby by Dave Thomas with Chad Fowler and Andy Hunt)\n\n* *_* is an optional and unused parameter retained only for erb compatibility.\n* *eoutvar* is an optional parameter used to specify a different local variable\nname to contain the results of the embedding process.\n\nIn many cases, only one parameter is required as in the many examples above.\n\n#### Adding location info.\n\nFor purposes of debugging, it may be useful to tie the transpiled code to a\nfile or line number. This information can be added to the mini_erb object as\nfollows:\n\n```ruby\nfile_name = \"my_file.erb\"\nerbed = MiniErb.new(IO.read(file_name))\nerbed.filename = file_name\n```\nThis sets up the erb file and establishes its file name for debug purposes.\n\n#### The transpiled code.\n\nOptionally, the transpiled ruby code may be accessed using the *src* attribute.\nThis is most often done to verify that the transpiler is operating correctly\nbut is also useful to those who wish to gain insights into the operation of\nthe mini_erb gem. It was this same access provided by the erb library that\nmade this gem possible.\n\n### Phase 2\n\nThe next step is to execute the transpiled code in an environment. This\nenvironment is a ruby binding to the virtual location of the code execution.\nIn effect, code is executed as if it was placed in the spot where the binding\nis taken.\n\nThis use of bindings allows access to any variables that may have been defined\nin that a location and also allows the state of that location to be modified.\nFor example:\n\n```ruby\nx = 42\nstr = \"x is now \u003c%=x%\u003e.\u003c%x+=1%\u003e x is now \u003c%=x%\u003e.\"\nputs MiniErb.new(str).result(binding), x\n```\nproduces\n\n    x is now 42. x is now 43.\n    43\n\nNote how the environment was modified by adding one to x.\n\nIf no binding is specified, the binding of the top level file of the current\nprogram is used as a default.\n\n\n## Contributing\n\n1. Fork it\n2. Create your feature branch (`git checkout -b my-new-feature`)\n3. Commit your changes (`git commit -am 'Add some feature'`)\n4. Push to the branch (`git push origin my-new-feature`)\n5. Create new Pull Request\n\nOR...\n\n* Make a suggestion by raising an\n [issue](https://github.com/PeterCamilleri/mini_erb/issues)\n. All ideas and comments are welcome.\n\n## License\n\nThe gem is available as open source under the terms of the\n[MIT License](./LICENSE.txt).\n\n## Code of Conduct\n\nEveryone interacting in the mini_erb project’s codebases, issue trackers,\nchat rooms and mailing lists is expected to follow the\n[code of conduct](./CODE_OF_CONDUCT.md).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpetercamilleri%2Fmini_erb","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpetercamilleri%2Fmini_erb","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpetercamilleri%2Fmini_erb/lists"}