README 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. ErlyDTL
  2. =======
  3. ErlyDTL implements most but not all of the Django Template Language.
  4. Project homepage: http://code.google.com/p/erlydtl/
  5. Compilation
  6. -----------
  7. To compile ErlyDTL, type "make" in this directory.
  8. Template compilation
  9. --------------------
  10. Four ways:
  11. erlydtl:compile("/path/to/template.dtl", my_module_name)
  12. erlydtl:compile("/path/to/template.dtl", my_module_name, Options)
  13. erlydtl:compile(<<"<html>{{ foo }}</html>">>, my_module_name)
  14. erlydtl:compile(<<"<html>{{ foo }}</html>">>, my_module_name, Options)
  15. Options is a proplist possibly containing:
  16. out_dir - Directory to store generated .beam files. If not specified, no
  17. .beam files will be created.
  18. doc_root - Included template paths will be relative to this directory;
  19. defaults to the compiled template's directory.
  20. custom_tags_dir - Directory of DTL files (no extension) includable as tags.
  21. E.g. if $custom_tags_dir/foo contains "<b>{{ bar }}</b>", then
  22. "{{ foo bar=100 }}" will evaluate to "<b>100</b>". Get it?
  23. vars - Variables (and their values) to evaluate at compile-time rather than
  24. render-time.
  25. reader - {module, function} tuple that takes a path to a template and returns
  26. a binary with the file contents. Defaults to {file, read_file}. Useful
  27. for reading templates from a network resource.
  28. compiler_options - Proplist passed directly to compiler:forms/2
  29. force_recompile - Recompile the module even if the source's checksum has not
  30. changed. Useful for debugging.
  31. locale - The locale used for template compile. Requires erlang_gettext. It
  32. will ask gettext_server for the string value on the provided locale.
  33. For example, adding {locale, "en_US"} will call {key2str, Key, "en_US"}
  34. for all string marked as trans ({% trans "StringValue"%} on templates).
  35. See README_I18N.
  36. Usage (of a compiled template)
  37. ------------------------------
  38. my_compiled_template:render(Variables) -> {ok, IOList} | {error, Err}
  39. Variables is a proplist, dict, gb_tree, or a parameterized module
  40. (whose method names correspond to variable names). The variable
  41. values can be atoms, strings, binaries, or (nested) variables.
  42. IOList is the rendered template.
  43. my_compiled_template:render(Variables, TranslationFun) ->
  44. {ok, IOList} | {error, Err}
  45. Same as render/1, but TranslationFun is a fun/1 that will be used to
  46. translate strings appearing inside {% trans %} tags. The simplest
  47. TranslatioFun would be
  48. fun(Val) -> Val end
  49. my_compiled_template:translatable_strings() -> [String]
  50. List of strings appearing in {% trans %} tags that can be overridden
  51. with a dictionary passed to render/2.
  52. my_compiled_template:source() -> {FileName, CheckSum}
  53. Name and checksum of the original template file.
  54. my_compiled_template:dependencies() -> [{FileName, CheckSum}]
  55. List of names/checksums of templates included by the original template
  56. file. Useful for frameworks that recompile a template only when the
  57. template's dependencies change.
  58. Tests
  59. -----
  60. From a Unix shell, run:
  61. make test
  62. Note that the tests will create some output in examples/rendered_output.