| name | docx-templates-3-conditional-content |
| description | Sub-skill of docx-templates: 3. Conditional Content. |
| version | 1.0.0 |
| category | data |
| type | reference |
| scripts_exempt | true |
3. Conditional Content
3. Conditional Content
If-Else Logic in Templates:
"""
Use conditionals to include or exclude content based on data.
"""
from docxtpl import DocxTemplate
from typing import Dict, Any, Optional
from dataclasses import dataclass
from enum import Enum
():
FULL_TIME =
PART_TIME =
CONTRACTOR =
():
STANDARD =
HIGH =
RESTRICTED =
:
employee_name:
position:
department:
start_date:
contract_type: ContractType
salary:
bonus_eligible:
stock_options: [] =
confidentiality_level: ConfidentialityLevel = ConfidentialityLevel.STANDARD
probation_period_months: =
remote_work_allowed: =
relocation_package: =
() -> [, ]:
{
: .employee_name,
: .position,
: .department,
: .start_date,
: ,
: .contract_type == ContractType.FULL_TIME,
: .contract_type == ContractType.PART_TIME,
: .contract_type == ContractType.CONTRACTOR,
: .contract_type.value.replace(, ).title(),
: .bonus_eligible,
: .stock_options ,
: .stock_options,
: .confidentiality_level.value,
: .confidentiality_level [
ConfidentialityLevel.HIGH,
ConfidentialityLevel.RESTRICTED
],
: .probation_period_months,
: .remote_work_allowed,
: .relocation_package
}
() -> :
template = DocxTemplate(template_path)
context = contract.to_context()
template.render(context)
template.save(output_path)
() -> :
template = DocxTemplate(template_path)
template.render(data)
template.save(output_path)
contract = EmployeeContract(
employee_name=,
position=,
department=,
start_date=,
contract_type=ContractType.FULL_TIME,
salary=,
bonus_eligible=,
stock_options=,
confidentiality_level=ConfidentialityLevel.HIGH,
remote_work_allowed=
)
() -> :
template = DocxTemplate(template_path)
score = performance_data.get(, )
context = {
**performance_data,
: (
score >=
score >=
score >=
),
: (performance_data.get(, [])) >
}
template.render(context)
template.save(output_path)