mbaynton / csv4twig
A CSV escaping filter for the Twig templating engine.
Installs: 8 818
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 1
Forks: 1
Open Issues: 1
Requires
- php: >=5.4.0
Requires (Dev)
- phpunit/phpunit: ~4.8
This package is auto-updated.
Last update: 2024-11-05 11:47:46 UTC
README
CSV escaping filter for the Twig templating engine
CSV4Twig enables Twig templates to generate safe, properly-escaped CSV output. It's useful when you need to get CSV reports out of an existing application that has a Twig-enabled output layer.
Usage
-
Add it to your project with composer:
composer require mbaynton/csv4twig:1.0.*
-
Tell Twig about it. You'll need to get a hold of the
\Twig_Environment
instance that will generate the CSV; then just pass it to\mbaynton\CSV4Twig\Filter::registerFilters()
. -
Use it in your template with the autoescape tag:
{% autoescape "csv" %} {{ some_value }},{{ another_value }} {% endautoescape %}
The contents of
some_value
andanother_value
will be escaped using the default CSV-escaping conventions of PHP'sfputcsv()
function.fputcsv()
is the function this filter uses internally.If you prefer, you can also escape certain values explicitly:
{% autoescape false %} {{ some_value|e("csv") }},{{ another_value }} {% endautoescape %}
That's it!