# Templating PHP and quoting vars

**URL:** <https://forum.ansible.com/t/templating-php-and-quoting-vars/25126>\
**Category:** Ansible Project\
**Created:** [July 29, 2017, 9:52am UTC](https://forum.ansible.com/t/templating-php-and-quoting-vars/25126 "2017-07-29T09:52:02Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [July 29, 2017, 9:52am UTC](https://forum.ansible.com/t/templating-php-and-quoting-vars/25126/1 "2017-07-29T09:52:02Z")

</div>

Hi

I'm templating PHP configuration files, but having some issues trying  
to make variables work that can be different types.  
Take for instance this snippet of config that I'm trying to template:

&nbsp;&nbsp;&nbsp;&nbsp;/\*  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\* Cookie domain.

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [July 29, 2017, 10:59am UTC](https://forum.ansible.com/t/templating-php-and-quoting-vars/25126/2 "2017-07-29T10:59:14Z")

</div>

I've come up with this custom filter:

def phpescape(var, quote='\''):  
&nbsp;&nbsp;&nbsp;&nbsp;"""Escape a value based on it's type.  
&nbsp;&nbsp;&nbsp;&nbsp;This is usefull for templating PHP files, where you need to quote  
&nbsp;&nbsp;&nbsp;&nbsp;strings but not booleans/null/etc.  
&nbsp;&nbsp;&nbsp;&nbsp;"""  
&nbsp;&nbsp;&nbsp;&nbsp;if isinstance(var, string\_types):  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return quote + var + quote  
&nbsp;&nbsp;&nbsp;&nbsp;elif var is None:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return 'null'  
&nbsp;&nbsp;&nbsp;&nbsp;return var

This way I can keep the template lines as simple as:

&nbsp;&nbsp;&nbsp;'session.cookie.domain' =\> {{ session\_cookie\_domain | phpescape }},

This will do the right thing for strings, null, booleans, and integers.

Dick
