XaminProject / handlebars.php

Handlebars processor for php
331 stars 134 forks source link

Helpers auto escape and triple braces syntax #42

Closed JustBlackBird closed 10 years ago

JustBlackBird commented 10 years ago

I've found differences between handlebars.js and handlebars.php.

Handlebars.js automatically escape result of line helper if it was not wrapped into Handlebars.SafeString object. To disable this behavior and display results "as is" three braces ({{{) are used for helpers.

Handlebars.php does not escape result of line helpers automatically. Thus three braces does not have any sense for line helpers.

This leads to different results for the same templates.

To illustrate the problem i've made tests for js version (http://jsfiddle.net/eSH6s/3/) and php version (https://github.com/Mibew/handlebars.php/commit/390bc70a0c6767defb95cb4dde532a99acab01be).

everplays commented 10 years ago

Thanks @JustBlackBird, I used your test to fix it.

JustBlackBird commented 10 years ago

I've created the pull request (#43) with better escape logic.

Also i can implement something like Handlebars.js Handlebars.SafeString to provide an ability for helpers to return markup.

everplays commented 10 years ago

if you want to implement a class or something that helpers can make use of to avoid multiple escaping, be my guest. Probably it'll be quite much like Handlebars\String class.

JustBlackBird commented 10 years ago

I already thought about extending Handlebars\String but there are multiple conditions like $smth instanceof \Handlebars\String in the code. It brings problems.

For example if i return an instance of derived class from a helper it will be treated as template source and rendered again (see https://github.com/XaminProject/handlebars.php/blob/master/src/Handlebars/Template.php#L278). But in the general case it's now what i'm waiting for.

So i have two ways:

  1. Extend \Handlebars\String class and update conditions like $smth instanceof \Handlebars\String to ($smth instanceof \Handlebars\String) && !($smth instanceof \Handlebars\SafeString) (or something similar) wherever it's needed.
  2. Add a base class for strings (Handlebars\BaseString for example) and inherit Handlebars\String and Handlebars\SafeString classes from it.

What do you think?

everplays commented 10 years ago

I wish we could use things like monkey patching of ruby or implicit conversion of scala but for our purpose the closest thing that php offers is SplString which is experimental, so we need to do it ourselves.

I'd go with second way.