{"id":13878723,"url":"https://github.com/ruby/erb","last_synced_at":"2025-05-14T15:05:42.186Z","repository":{"id":38361412,"uuid":"266090524","full_name":"ruby/erb","owner":"ruby","description":"An easy to use but powerful templating system for Ruby","archived":false,"fork":false,"pushed_at":"2025-01-15T04:07:31.000Z","size":315,"stargazers_count":134,"open_issues_count":1,"forks_count":19,"subscribers_count":33,"default_branch":"master","last_synced_at":"2025-04-13T04:56:21.884Z","etag":null,"topics":["ruby"],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ruby.png","metadata":{"files":{"readme":"README.md","changelog":"NEWS.md","contributing":null,"funding":null,"license":"COPYING","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-05-22T11:09:15.000Z","updated_at":"2025-04-07T10:24:10.000Z","dependencies_parsed_at":"2024-06-18T15:23:56.472Z","dependency_job_id":"75dec2a0-ce1c-40b1-a56f-ec1d64cf4df9","html_url":"https://github.com/ruby/erb","commit_stats":{"total_commits":234,"total_committers":30,"mean_commits":7.8,"dds":0.7008547008547008,"last_synced_commit":"8ba8bb7acf407bb0d226fcddcd94fc559fb60070"},"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ruby%2Ferb","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ruby%2Ferb/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ruby%2Ferb/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ruby%2Ferb/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ruby","download_url":"https://codeload.github.com/ruby/erb/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248665763,"owners_count":21142123,"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"],"created_at":"2024-08-06T08:01:57.818Z","updated_at":"2025-04-13T04:56:36.824Z","avatar_url":"https://github.com/ruby.png","language":"Ruby","readme":"# ERB\n\nAn easy to use but powerful templating system for Ruby.\n\n## Introduction\n\nERB provides an easy to use but powerful templating system for Ruby.  Using\nERB, actual Ruby code can be added to any plain text document for the\npurposes of generating document information details and/or flow control.\n\nA very simple example is this:\n\n```rb\nrequire 'erb'\n\nx = 42\ntemplate = ERB.new \u003c\u003c-EOF\n  The value of x is: \u003c%= x %\u003e\nEOF\nputs template.result(binding)\n```\n\nPrints: `The value of x is: 42`\n\nMore complex examples are given below.\n\n## Recognized Tags\n\nERB recognizes certain tags in the provided template and converts them based\non the rules below:\n\n```erb\n\u003c% Ruby code -- inline with output %\u003e\n\u003c%= Ruby expression -- replace with result %\u003e\n\u003c%# comment -- ignored -- useful in testing %\u003e (`\u003c% #` doesn't work. Don't use Ruby comments.)\n% a line of Ruby code -- treated as \u003c% line %\u003e (optional -- see ERB.new)\n%% replaced with % if first thing on a line and % processing is used\n\u003c%% or %%\u003e -- replace with \u003c% or %\u003e respectively\n```\n\nAll other text is passed through ERB filtering unchanged.\n\n## Options\n\nThere are several settings you can change when you use ERB:\n* the nature of the tags that are recognized;\n* the binding used to resolve local variables in the template.\n\nSee the ERB.new and ERB#result methods for more detail.\n\n## Character encodings\n\nERB (or Ruby code generated by ERB) returns a string in the same\ncharacter encoding as the input string.  When the input string has\na magic comment, however, it returns a string in the encoding specified\nby the magic comment.\n\n```rb\n# -*- coding: utf-8 -*-\nrequire 'erb'\n\ntemplate = ERB.new \u003c\u003cEOF\n\u003c%#-*- coding: Big5 -*-%\u003e\n  __ENCODING__ is \u003c%= __ENCODING__ %\u003e.\nEOF\nputs template.result\n```\n\nPrints: `__ENCODING__ is Big5.`\n\n## Examples\n\n### Plain Text\n\nERB is useful for any generic templating situation.  Note that in this example, we use the\nconvenient \"% at start of line\" tag, and we quote the template literally with\n`%q{...}` to avoid trouble with the backslash.\n\n```rb\nrequire \"erb\"\n\n# Create template.\ntemplate = %q{\n  From:  James Edward Gray II \u003cjames@grayproductions.net\u003e\n  To:  \u003c%= to %\u003e\n  Subject:  Addressing Needs\n\n  \u003c%= to[/\\w+/] %\u003e:\n\n  Just wanted to send a quick note assuring that your needs are being\n  addressed.\n\n  I want you to know that my team will keep working on the issues,\n  especially:\n\n  \u003c%# ignore numerous minor requests -- focus on priorities %\u003e\n  % priorities.each do |priority|\n    * \u003c%= priority %\u003e\n  % end\n\n  Thanks for your patience.\n\n  James Edward Gray II\n}.gsub(/^  /, '')\n\nmessage = ERB.new(template, trim_mode: \"%\u003c\u003e\")\n\n# Set up template data.\nto = \"Community Spokesman \u003cspokesman@ruby_community.org\u003e\"\npriorities = [ \"Run Ruby Quiz\",\n               \"Document Modules\",\n               \"Answer Questions on Ruby Talk\" ]\n\n# Produce result.\nemail = message.result\nputs email\n```\n\nGenerates:\n\n```\nFrom:  James Edward Gray II \u003cjames@grayproductions.net\u003e\nTo:  Community Spokesman \u003cspokesman@ruby_community.org\u003e\nSubject:  Addressing Needs\n\nCommunity:\n\nJust wanted to send a quick note assuring that your needs are being addressed.\n\nI want you to know that my team will keep working on the issues, especially:\n\n    * Run Ruby Quiz\n    * Document Modules\n    * Answer Questions on Ruby Talk\n\nThanks for your patience.\n\nJames Edward Gray II\n```\n\n### Ruby in HTML\n\nERB is often used in .rhtml files (HTML with embedded Ruby).  Notice the need in\nthis example to provide a special binding when the template is run, so that the instance\nvariables in the Product object can be resolved.\n\n```rb\nrequire \"erb\"\n\n# Build template data class.\nclass Product\n  def initialize( code, name, desc, cost )\n    @code = code\n    @name = name\n    @desc = desc\n    @cost = cost\n\n    @features = [ ]\n  end\n\n  def add_feature( feature )\n    @features \u003c\u003c feature\n  end\n\n  # Support templating of member data.\n  def get_binding\n    binding\n  end\n\n  # ...\nend\n\n# Create template.\ntemplate = %{\n  \u003chtml\u003e\n    \u003chead\u003e\u003ctitle\u003eRuby Toys -- \u003c%= @name %\u003e\u003c/title\u003e\u003c/head\u003e\n    \u003cbody\u003e\n\n      \u003ch1\u003e\u003c%= @name %\u003e (\u003c%= @code %\u003e)\u003c/h1\u003e\n      \u003cp\u003e\u003c%= @desc %\u003e\u003c/p\u003e\n\n      \u003cul\u003e\n        \u003c% @features.each do |f| %\u003e\n          \u003cli\u003e\u003cb\u003e\u003c%= f %\u003e\u003c/b\u003e\u003c/li\u003e\n        \u003c% end %\u003e\n      \u003c/ul\u003e\n\n      \u003cp\u003e\n        \u003c% if @cost \u003c 10 %\u003e\n          \u003cb\u003eOnly \u003c%= @cost %\u003e!!!\u003c/b\u003e\n        \u003c% else %\u003e\n           Call for a price, today!\n        \u003c% end %\u003e\n      \u003c/p\u003e\n\n    \u003c/body\u003e\n  \u003c/html\u003e\n}.gsub(/^  /, '')\n\nrhtml = ERB.new(template)\n\n# Set up template data.\ntoy = Product.new( \"TZ-1002\",\n                   \"Rubysapien\",\n                   \"Geek's Best Friend!  Responds to Ruby commands...\",\n                   999.95 )\ntoy.add_feature(\"Listens for verbal commands in the Ruby language!\")\ntoy.add_feature(\"Ignores Perl, Java, and all C variants.\")\ntoy.add_feature(\"Karate-Chop Action!!!\")\ntoy.add_feature(\"Matz signature on left leg.\")\ntoy.add_feature(\"Gem studded eyes... Rubies, of course!\")\n\n# Produce result.\nrhtml.run(toy.get_binding)\n```\n\nGenerates (some blank lines removed):\n\n```html\n\u003chtml\u003e\n  \u003chead\u003e\u003ctitle\u003eRuby Toys -- Rubysapien\u003c/title\u003e\u003c/head\u003e\n  \u003cbody\u003e\n\n    \u003ch1\u003eRubysapien (TZ-1002)\u003c/h1\u003e\n    \u003cp\u003eGeek's Best Friend!  Responds to Ruby commands...\u003c/p\u003e\n\n    \u003cul\u003e\n        \u003cli\u003e\u003cb\u003eListens for verbal commands in the Ruby language!\u003c/b\u003e\u003c/li\u003e\n        \u003cli\u003e\u003cb\u003eIgnores Perl, Java, and all C variants.\u003c/b\u003e\u003c/li\u003e\n        \u003cli\u003e\u003cb\u003eKarate-Chop Action!!!\u003c/b\u003e\u003c/li\u003e\n        \u003cli\u003e\u003cb\u003eMatz signature on left leg.\u003c/b\u003e\u003c/li\u003e\n        \u003cli\u003e\u003cb\u003eGem studded eyes... Rubies, of course!\u003c/b\u003e\u003c/li\u003e\n    \u003c/ul\u003e\n\n    \u003cp\u003e\n         Call for a price, today!\n    \u003c/p\u003e\n\n  \u003c/body\u003e\n\u003c/html\u003e\n```\n\n## Notes\n\nThere are a variety of templating solutions available in various Ruby projects.\nFor example, RDoc, distributed with Ruby, uses its own template engine, which\ncan be reused elsewhere.\n\nOther popular engines could be found in the corresponding\n[Category](https://www.ruby-toolbox.com/categories/template_engines) of\nThe Ruby Toolbox.\n\n## License\n\nThe gem is available as open source under the terms of the [2-Clause BSD License](https://opensource.org/licenses/BSD-2-Clause).\n","funding_links":[],"categories":["Ruby"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fruby%2Ferb","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fruby%2Ferb","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fruby%2Ferb/lists"}