Merge pull request #17 from ak04nv/patch-1

Update README
This commit is contained in:
Christopher Ramírez 2015-05-08 12:16:45 -06:00
commit c907945510

View file

@ -14,18 +14,19 @@ Rendered documents are produced in ODT format, and can then be converted to PDF,
pip install secretary pip install secretary
## Rendering a Template ## Rendering a Template
```python
from secretary import Renderer from secretary import Renderer
engine = Renderer() engine = Renderer()
result = engine.render(template, foo=foo, bar=bar) result = engine.render(template, foo=foo, bar=bar)
```
Secretary implements a class called `Renderer`. `Renderer` takes a single argument called `environment` which is a jinja **[Environment][3]**. Secretary implements a class called `Renderer`. `Renderer` takes a single argument called `environment` which is a jinja **[Environment][3]**.
To render a template create an instance of class `Renderer` and call the instance's method `render` passing a template file and template's variables as keyword arguments. `template` can be a filename or a file object. `render` will return the rendered document in binary format. To render a template create an instance of class `Renderer` and call the instance's method `render` passing a template file and template's variables as keyword arguments. `template` can be a filename or a file object. `render` will return the rendered document in binary format.
Before rendering a template, you can configure the internal templating engine using the `Renderer` instance's variable `environment`, which is an instance of jinja2 **[Environment][3]** class. For example, to declare a custom filter use: Before rendering a template, you can configure the internal templating engine using the `Renderer` instance's variable `environment`, which is an instance of jinja2 **[Environment][3]** class. For example, to declare a custom filter use:
```python
from secretary import Renderer from secretary import Renderer
engine = Renderer() engine = Renderer()
@ -36,6 +37,7 @@ Before rendering a template, you can configure the internal templating engine us
output = open('rendered_document.odt', 'wb') output = open('rendered_document.odt', 'wb')
output.write(result) output.write(result)
```
## Composing Templates ## Composing Templates
@ -44,9 +46,10 @@ Secretary templates are simple ODT documents. You can create them using Writer.
### Printing Variables ### Printing Variables
Since Secretary use the same template syntax of Jinja2, to print a varible type a double curly braces enclosing the variable, like so: Since Secretary use the same template syntax of Jinja2, to print a varible type a double curly braces enclosing the variable, like so:
```jinja
{{ foo.bar }} {{ foo.bar }}
{{ foo['bar'] }} {{ foo['bar'] }}
```
However, mixing template instructions and normal text into the template document may become confusing and clutter the layout and most important, in most cases will produce invalid ODT documents. Secretary recommends using an alternative way of inserting fields. Insert a visual field in LibreOffice Writer from the menu `Insert` > `Fields` > `Other...` (or just press `Ctrl+F2`), then click on the `Functions` tab and select `Input field`. Click `Insert`. A dialog will appear where you can insert the print instructions. You can even insert simple control flow tags to dynamically change what is printed in the field. However, mixing template instructions and normal text into the template document may become confusing and clutter the layout and most important, in most cases will produce invalid ODT documents. Secretary recommends using an alternative way of inserting fields. Insert a visual field in LibreOffice Writer from the menu `Insert` > `Fields` > `Other...` (or just press `Ctrl+F2`), then click on the `Functions` tab and select `Input field`. Click `Insert`. A dialog will appear where you can insert the print instructions. You can even insert simple control flow tags to dynamically change what is printed in the field.
@ -58,33 +61,38 @@ Most of the time secretary will handle the internal composing of XML when you in
#### Examples document structures #### Examples document structures
**Printing multiple records in a table** **Printing multiple records in a table**
``` plain ```jinja
----------------------------------------- ┌───────────────────┬────────────────────┐
| RECORDS ID | NAME | │ RECORD ID │ RECORD NAME │
----------------------------------------- ├───────────────────┴────────────────────┤
| {% for record in records %} | │ {% for record in records %} │
----------------------------------------- ├───────────────────┬────────────────────┤
| {{ record.id }} | {{ record.name }} | │ {{ record.id }} │ {{ record.name }} │
----------------------------------------- ├───────────────────┴────────────────────┤
| {% endfor %} | │ {% endfor %} │
----------------------------------------- └────────────────────────────────────────┘
``` ```
**Conditional paragraphs** **Conditional paragraphs**
> `{% if already_paid %}` ```jinja
> YOU ALREADY PAID {% if already_paid %}
> `{% else %}` YOU ALREADY PAID
> YOU HAVEN'T PAID {% else %}
> `{% endif %}` YOU HAVEN'T PAID
{% endif %}
```
The last example could had been simplified into a single paragraph in Writer like: The last example could had been simplified into a single paragraph in Writer like:
```jinja
> `{% if already_paid %}`YOU ALREADY PAID`{% else %}`YOU HAVEN'T PAID`{% endif %}` {% if already_paid %}YOU ALREADY PAID{% else %}YOU HAVEN'T PAID{% endif %}
```
**Printing a list of names** **Printing a list of names**
```jinja
> * `{% for name in names %}` {% for name in names %}
> * `{{ name }}` {{ name }}
> * `{* endfor %}` {% endfor %}
```
Automatic control flow in Secretary will handle the intuitive result of the above examples and similar thereof. Automatic control flow in Secretary will handle the intuitive result of the above examples and similar thereof.
@ -151,7 +159,6 @@ Pad zeroes to `value` to the left until output value's length be equal to `lengt
{{ invoice.number|pad(6) }} {{ invoice.number|pad(6) }}
### Features of jinja2 not supported ### Features of jinja2 not supported
Secretary supports most of the jinja2 control structure/flow tags. But please avoid using the following tags since they are not supported: `block`, `extends`, `macro`, `call`, `include` and `import`. Secretary supports most of the jinja2 control structure/flow tags. But please avoid using the following tags since they are not supported: `block`, `extends`, `macro`, `call`, `include` and `import`.