|
1 | 1 | # liquidpy |
2 | 2 | A port of [liquid][1] template engine for python |
3 | 3 |
|
4 | | -[![Pypi][2]][9] [![Github][3]][10] [![PythonVers][4]][9] [![ReadTheDocs building][13]][8] [![Travis building][5]][11] [![Codacy][6]][12] [![Codacy coverage][7]][12] |
| 4 | +[![Pypi][2]][9] [![Github][3]][10] [![PythonVers][4]][9] [![Codacy][6]][12] [![Codacy coverage][7]][12] ![Docs building][13] ![Building][5] |
| 5 | + |
| 6 | +This is compatible with [standard Liquid][1] template engine. Variations, such as Shopify and Jekyll are not fully supported yet. |
5 | 7 |
|
6 | 8 | ## Install |
7 | 9 | ```shell |
8 | | -pip install liquidpy |
| 10 | +pip install -U liquidpy |
9 | 11 | ``` |
10 | 12 |
|
11 | | -## Current status and plans |
12 | | -- Note that this branch is not fully compatible with shopify's liquid. For compatible versions, please check branches [lark][14] and [larkone][15]. |
13 | | -- This branch is current NOT safe agaist malicious input ([#22][16]), so we tried to re-implement the engine using `lark-parser`. However, both versions were very slow for lexer. |
14 | | -- With branch `lark`, we tried to tokenize each tag and parse the content of the tags later using independent parsers, while with `larkone`, we tried to put all grammars together, and made it into a universal parser. However, both of them are slow, due to tokenization of whole tags (`raw` and `comment`) and literals (See `grammar.lark` in the code). |
15 | | -- If you have a better grammar or idea for tokenization, you are very welcome to submit issues or PRs (writing naive lexer is just too much work). |
16 | | -- We left some APIs to extend the `lark` ones with some functions from `master`. However, it won't happen before we find a faster lexer. |
17 | | -- A temporary plan for the `master` branch is to do some security check to address [#22][16]. |
18 | | - |
19 | | -## Full Documentation |
20 | | -[ReadTheDocs][8] |
21 | | - |
22 | 13 | ## Baisic usage |
23 | 14 | ```python |
24 | 15 | from liquid import Liquid |
25 | 16 | liq = Liquid('{{a}}') |
26 | | -ret = liq.render(a = 1) |
| 17 | +ret = liq.render(a=1) |
27 | 18 | # ret == '1' |
28 | 19 |
|
29 | | -# load template from a file |
30 | | -liq = Liquid('/path/to/template', liquid_from_file=True) |
| 20 | +# with environments pre-loaded |
| 21 | +liq = Liquid('{{a}}', a=1) |
| 22 | +ret = liq.render() |
| 23 | +# ret == '1' |
| 24 | + |
| 25 | +# With debug on: |
| 26 | +liq = Liquid('{{a}}', liquid_config={'debug': True}) |
| 27 | +``` |
| 28 | + |
| 29 | +## Python mode |
| 30 | + |
| 31 | +We also support a python mode template engine, which acts more pythonic and powerful. |
| 32 | +```python |
| 33 | +from liquid import Liquid |
| 34 | +# standard liquid doesn't support this |
| 35 | +liq = Liquid('{{a + 1}}', {'mode': 'python'}) |
| 36 | +ret = liq.render(a=1) |
| 37 | +# ret == '2' |
31 | 38 | ``` |
32 | | -With environments: |
| 39 | + |
| 40 | +Both modes can accept a path, a file-like object or a stream for the template: |
33 | 41 | ```python |
34 | | -liq = Liquid('{{a | os.path.basename}}', os=__import__('os')) |
35 | | -ret = liq.render(a="path/to/file.txt") |
36 | | -# ret == 'file.txt' |
| 42 | +Liquid('/path/to/template') |
| 43 | +# or |
| 44 | +with open('/path/to/template') as f: |
| 45 | + Liquid(f) |
37 | 46 | ``` |
38 | 47 |
|
| 48 | +## Full Documentation |
| 49 | +- Liquid's [documentation][1] |
| 50 | +- Liquidpy's [documentation][14] |
| 51 | + |
| 52 | +## Backward compatiblility warning |
| 53 | + |
| 54 | +`v0.6.0+` is a remodeled version to make it compatible with standard liquid engine. If you are using a previous version, stick with it. `0.6.0+` is not fully compatible with previous versions. |
| 55 | + |
39 | 56 | [1]: https://shopify.github.io/liquid/ |
40 | 57 | [2]: https://img.shields.io/pypi/v/liquidpy.svg?style=flat-square |
41 | 58 | [3]: https://img.shields.io/github/tag/pwwang/liquidpy.svg?style=flat-square |
42 | 59 | [4]: https://img.shields.io/pypi/pyversions/liquidpy.svg?style=flat-square |
43 | | -[5]: https://img.shields.io/travis/pwwang/liquidpy.svg?style=flat-square |
| 60 | +[5]: https://img.shields.io/github/workflow/status/pwwang/liquidpy/Build%20and%20Deploy?style=flat-square |
44 | 61 | [6]: https://img.shields.io/codacy/grade/aed04c099cbe42dabda2b42bae557fa4?style=flat-square |
45 | 62 | [7]: https://img.shields.io/codacy/coverage/aed04c099cbe42dabda2b42bae557fa4?style=flat-square |
46 | 63 | [8]: https://liquidpy.readthedocs.io/en/latest/ |
47 | 64 | [9]: https://pypi.org/project/liquidpy/ |
48 | 65 | [10]: https://github.com/pwwang/liquidpy |
49 | | -[11]: https://travis-ci.org/pwwang/liquidpy |
50 | 66 | [12]: https://app.codacy.com/manual/pwwang/liquidpy/dashboard |
51 | | -[13]: https://img.shields.io/readthedocs/liquidpy?style=flat-square |
52 | | -[14]: https://github.com/pwwang/liquidpy/tree/lark |
53 | | -[15]: https://github.com/pwwang/liquidpy/tree/larkone |
54 | | -[16]: https://github.com/pwwang/liquidpy/issues/22 |
| 67 | +[13]: https://img.shields.io/github/workflow/status/pwwang/liquidpy/Build%20Docs?label=docs&style=flat-square |
| 68 | +[14]: https://pwwang.github.io/liquidpy/ |
0 commit comments