{"id":13622365,"url":"https://github.com/arnaud-lb/MtHaml","last_synced_at":"2025-04-15T06:30:24.813Z","repository":{"id":1496391,"uuid":"1748684","full_name":"arnaud-lb/MtHaml","owner":"arnaud-lb","description":"Multi target HAML (HAML for PHP, Twig, \u003cyour language here\u003e)","archived":false,"fork":false,"pushed_at":"2022-10-23T09:03:16.000Z","size":353,"stargazers_count":358,"open_issues_count":28,"forks_count":54,"subscribers_count":22,"default_branch":"master","last_synced_at":"2025-04-08T14:12:19.408Z","etag":null,"topics":["haml","haml-php","php","twig"],"latest_commit_sha":null,"homepage":"","language":"PHP","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/arnaud-lb.png","metadata":{"files":{"readme":"README.markdown","changelog":"CHANGELOG","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":"2011-05-14T19:14:51.000Z","updated_at":"2025-02-20T01:53:11.000Z","dependencies_parsed_at":"2022-08-16T13:25:19.791Z","dependency_job_id":null,"html_url":"https://github.com/arnaud-lb/MtHaml","commit_stats":null,"previous_names":[],"tags_count":17,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arnaud-lb%2FMtHaml","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arnaud-lb%2FMtHaml/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arnaud-lb%2FMtHaml/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arnaud-lb%2FMtHaml/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/arnaud-lb","download_url":"https://codeload.github.com/arnaud-lb/MtHaml/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249020568,"owners_count":21199581,"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":["haml","haml-php","php","twig"],"created_at":"2024-08-01T21:01:18.180Z","updated_at":"2025-04-15T06:30:23.817Z","avatar_url":"https://github.com/arnaud-lb.png","language":"PHP","funding_links":[],"categories":["模板","模板 Templating","Table of Contents","PHP","目录","Templating","模板引擎( Templating )"],"sub_categories":["Templating","模板 Templating","Globalization"],"readme":"# Multi target HAML\n\n[![Build Status](https://secure.travis-ci.org/arnaud-lb/MtHaml.png)](http://travis-ci.org/arnaud-lb/MtHaml)\n\nMtHaml is a PHP implementation of the [HAML language][1] which can target multiple languages.\n\nCurrently supported targets are PHP and [Twig][4], and new ones can be added easily.\n\nMt-Haml implements the exact same syntax as ruby-haml; the only difference is that any supported language can be used everywhere HAML expects Ruby code:\n\n## HAML/Twig:\n\n``` haml\n%ul#users\n  - for user in users\n    %li.user\n      = user.name\n      Email: #{user.email}\n      %a(href=user.url) Home page\n```\n\nRendered:\n\n``` jinja\n\u003cul id=\"users\"\u003e\n  {% for user in users %}\n    \u003cli class=\"user\"\u003e\n      {{ user.name }}\n      Email: {{ user.email }}\n      \u003ca href=\"{{ user.url }}\"\u003eHome page\u003c/a\u003e\n    \u003c/li\u003e\n  {% endfor %}\n\u003c/ul\u003e\n```\n\n## HAML/PHP:\n\n``` haml\n%ul#users\n  - foreach($users as $user)\n    %li.user\n      = $user-\u003egetName()\n      Email: #{$user-\u003egetEmail()}\n      %a(href=$user-\u003egetUrl()) Home page\n```\n\nRendered:\n\n``` php\n\u003cul id=\"users\"\u003e\n  \u003c?php foreach($users as $user) { ?\u003e\n    \u003cli class=\"user\"\u003e\n      \u003c?php echo $user-\u003egetName(); ?\u003e\n      Email: \u003c?php echo $user-\u003egetEmail(); ?\u003e\n      \u003ca href=\"\u003c?php echo $user-\u003egetUrl(); ?\u003e\"\u003eHome page\u003c/a\u003e\n    \u003c/li\u003e\n  \u003c?php } ?\u003e\n\u003c/ul\u003e\n```\n\n## Usage\n\nPHP:\n\n``` php\n\u003c?php\n$haml = new MtHaml\\Environment('php');\n$executor = new MtHaml\\Support\\Php\\Executor($haml, array(\n    'cache' =\u003e sys_get_temp_dir().'/haml',\n));\n\n// Compiles and executes the HAML template, with variables given as second\n// argument\n$executor-\u003edisplay('template.haml', array(\n    'var' =\u003e 'value',\n));\n\n```\n\n[Twig][4]:\n\n``` php\n\u003c?php\n$haml = new MtHaml\\Environment('twig', array('enable_escaper' =\u003e false));\n\n// Use a custom loader, whose responsibility is to convert HAML templates\n// to Twig syntax, before handing them out to Twig:\n$hamlLoader = new MtHaml\\Support\\Twig\\Loader($haml, $twig-\u003egetLoader());\n$twig-\u003esetLoader($hamlLoader);\n\n// Register the Twig extension before executing a HAML template\n$twig-\u003eaddExtension(new MtHaml\\Support\\Twig\\Extension());\n\n// Render templates as usual\n$twig-\u003erender('template.haml', ...);\n```\n\nSee [examples][7] and [MtHaml with Twig](https://github.com/arnaud-lb/MtHaml/wiki/Use-MtHaml-with-Twig)\n\n## Escaping\n\nMtHaml escapes everything by default. Since Twig already supports\nauto escaping it is recommended to enable it in Twig and disable it in MtHaml:\n\n`new MtHaml\\Environment('twig', array('enable_escaper' =\u003e false));`\n\nHAML/PHP is rendered like this when auto escaping is enabled:\n\n``` haml\nEmail #{$user-\u003egetEmail()}\n%a(href=$user-\u003egetUrl()) Home page\n```\n\n``` php\nEmail \u003c?php echo htmlspecialchars($user-\u003egetEmail(), ENT_QUOTES, 'UTF-8'); ?\u003e\n\u003ca href=\"\u003c?php echo htmlspecialchars($user-\u003egetUrl(), ENT_QUOTES, 'UTF-8'); ?\u003e\"\u003eHome page\u003c/a\u003e\n```\n\n## Twig\n\nUsing [Twig][4] in HAML gives more control over what can be executed, what variables and functions are exposed to the templates, etc. This also allows to use all of Twig's awesome features like template inheritance, macros, blocks, filters, functions, tests, ...\n\n``` haml\n- extends \"some-template.haml\"\n\n- macro printSomething()\n  %p something\n\n- block body\n  %h1 Title\n  = _self.printSomething()\n```\n\n### Integration in Twig\n\nMtHaml comes with an example Twig_Loader that will automatically convert HAML into Twig at loading time (Twig will then compile the resulting Twig script and cache it). Templates with a `.haml` extension, or whose source starts with `{% haml %}` will be converted, and the others will be left untouched.\n\nThe loader acts as a proxy and takes an other loader as parameter:\n\n``` php\n\u003c?php\n\n$haml = new MtHaml\\Environment(...);\n\n$twig_loader = new Twig_Loader_Filesystem(...);\n$twig_loader = new MtHaml\\Support\\Twig\\Loader($haml, $twig_loader);\n```\n\n### Runtime support\n\nCompiled MtHaml/Twig templates need support from MtHaml at runtime in some cases. Because of this, a Twig extension must be loaded before executing the templates.\n\n\n``` php\n\u003c?php\n// Register the MtHaml extension before executing the template:\n$twig-\u003eaddExtension(new MtHaml\\Support\\Twig\\Extension());\n$twig-\u003erender(\"rendered_twig_template.twig\");\n```\n\n## Syntax\n\nThe syntax is the same as [HAML/Ruby][1]'s syntax, except that PHP or Twig have to be used where Ruby is expected.\n\nSee the [tutorial][2] and the [reference][3]\n\n## Performance\n\nMtHaml converts HAML to PHP or Twig code. The resulting code can be cached and executed any number of times, and\ndoesn't depend on HAML at runtime.\n\nMtHaml has no runtime overhead.\n\n## Helpers\n\nHelpers in HAML/Ruby are just ruby functions exposed to templates.\nAny function can be made available to HAML templates by the target language\n(the function only have to be available at runtime).\n\nIn HAML/Twig you can use all of Twig's functions, filters, and tags. In HAML/PHP, you can use all PHP functions.\n\n## Filters\n\nFilters take plain text input (with support for `#{...}` interpolations) and transform it, or wrap it.\n\nExample with the `javascript` filter:\n\n``` haml\n%p something\n:javascript\n  some.javascript.code(\"#{var|escape('js')}\");\n```\n\n``` jinja\n\u003cp\u003esomething\u003c/p\u003e\n\u003cscript type=\"text/javascript\"\u003e\n//\u003c![CDATA[\n  some.javascript.code(\"{{ var|escape('js') }}\");\n//]]\u003e\n\u003c/script\u003e\n```\n\nThe following filters are available:\n\n - **css**: wraps with style tags\n - **cdata**: wraps with CDATA markup\n - **coffee***: compiles coffeescript to javascript\n - **escaped**: html escapes\n - **javascript**: wraps with script tags\n - **less***: compiles as Lesscss\n - **markdown***: converts markdown to html\n - **php**: executes the input as php code\n - **plain**: does not parse the filtered text\n - **preseve**: preserves preformatted text\n - **scss***: converts scss to css\n - **twig**: executes the input as twig code\n\nFilter marked with `*` have runtime dependencies and are not enabled by default. Such filters need to be provided to MtHaml\\Environment explicitly.\n\nExample with the Coffee filter:\n\n``` php\n\u003c?php\n\n$coffeeFilter = new MtHaml\\Filter\\CoffeeScript(new CoffeeScript\\Compiler);\n\n$env = new MtHaml\\Environment('twig', array(\n    'enable_escaper' =\u003e false,\n), array(\n    'coffee' =\u003e $coffeeFilter,\n));\n```\n\n## Sass\n\n[Sass][6] can be used in PHP projects without problem. It only depends on Ruby and does not need to be installed on production servers. So MtHaml will not re-implement Sass.\n\n## Frameworks and CMS support\n \n - CakePHP: https://github.com/TiuTalk/haml\n - Drupal: https://github.com/antoinelafontaine/oxide\n - FuelPHP: https://github.com/fuel/parser\n - Laravel (PHP): https://github.com/BKWLD/laravel-haml\n - Laravel (Twig): https://github.com/SimonDegraeve/laravel-twigbridge\n - PHPixie: https://github.com/dracony/PHPixie-HAML\n - Silex: https://github.com/arnaud-lb/Silex-MtHaml\n - Sprockets-PHP: https://github.com/Nami-Doc/Sprockets-PHP\n - Symfony2: https://github.com/arnaud-lb/MtHamlBundle\n - Yii2 Framework: https://github.com/mervick/yii2-mthaml\n - Zend Framework 1: https://github.com/bonndan/mthaml-zf1\n - PhileCMS: https://bitbucket.org/jacmoe/templatemthaml\n\nAdd yours: https://github.com/arnaud-lb/MtHaml/edit/master/README.markdown\n\n## License\n\nMtHaml is released under the MIT license (same as HAML/Ruby).\n\n[1]: http://haml-lang.com/\n[2]: http://haml-lang.com/tutorial.html\n[3]: http://haml-lang.com/docs/yardoc/file.HAML_REFERENCE.html\n[4]: http://www.twig-project.org/\n[5]: http://haml-lang.com/docs/yardoc/file.HAML_REFERENCE.html#attribute_methods\n[6]: http://sass-lang.com/\n[7]: https://github.com/arnaud-lb/MtHaml/blob/master/examples/README.md\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Farnaud-lb%2FMtHaml","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Farnaud-lb%2FMtHaml","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Farnaud-lb%2FMtHaml/lists"}