HP-Handy

 view release on metacpan or  search on metacpan

doc/jinja2_cheatsheet.TL.txt  view on Meta::CPAN


  defined  none  string  number  sequence  mapping
  odd  even  divisibleby  upper  lower  equalto  in

  {% if x is defined %}
  {% if n is odd %}
  {% if x is not none %}

[ 6. Kondisyon ]

  {% if condition %}
      ...
  {% elif other %}
      ...
  {% else %}
      ...
  {% endif %}

  {{ "yes" if flag else "no" }}

[ 7. for loop ]

  {% for item in list %}
      {{ loop.index }}: {{ item }}
  {% endfor %}

  {% for item in list if item != "" %}
      {{ item }}
  {% else %}
      (empty)
  {% endfor %}

  {% for key, value in mapping %}
      {{ key }}: {{ value }}
  {% endfor %}

  loop.index  loop.index0  loop.first  loop.last
  loop.length  loop.odd  loop.even  loop.revindex

[ 8. set ]

  {% set x = 42 %}
  {% set s = "Hello, " ~ name %}

[ 9. Isama ]

  {% include "header.html" %}
  {% include "opt.html" ignore missing %}

[ 10. Mana ng template ]

  base.html:
    {% block title %}Default{% endblock %}
    {% block content %}{% endblock %}

  child.html:
    {% extends "base.html" %}
    {% block title %}My Page{% endblock %}
    {% block content %}<p>Hello</p>{% endblock %}

[ 11. Macro ]

  {% macro input(name, value="", type="text") %}
      <input type="{{ type }}" name="{{ name }}" value="{{ value }}">
  {% endmacro %}

  {{ input("username") }}
  {{ input("password", type="password") }}

[ 12. with ]

  {% with x = 10, y = 20 %}
      {{ x + y }}
  {% endwith %}

[ 13. raw ]

  {% raw %}
      {{ not rendered }}
  {% endraw %}

[ 14. Mga ekspresyon ]

  +  -  *  /  //  %  **   (arithmetic)
  ~                        (string concat)
  ==  !=  <  >  <=  >=    (comparison)
  and  or  not             (logic)
  in  not in               (membership)
  a if cond else b         (ternary)
  range(stop)  range(start, stop, step)

[ 15. Custom na filter at test ]

  $tmpl->add_filter('double', sub { $_[0] . $_[0] });
  # {{ text | double }}

  $tmpl->add_test('positive', sub { defined $_[0] && $_[0] > 0 });
  # {% if n is positive %}

[ 16. Mga paraan ng render ]

  my $html = $tmpl->render_file('index.html', \%vars);
  my $html = $tmpl->render_string($source, \%vars);

[ 17. Opisyal na mapagkukunan ]

  Jinja2: https://jinja.palletsprojects.com/
  HP::Handy:   https://metacpan.org/dist/HP-Handy
  HTTP::Handy: https://metacpan.org/dist/HTTP-Handy
  DB::Handy:   https://metacpan.org/dist/DB-Handy

======================================================================



( run in 0.324 second using v1.01-cache-2.11-cpan-40ba7b3775d )