FoilPHP / Foil

PHP template engine for native PHP templates
https://foilphp.github.io/Foil/
MIT License
170 stars 24 forks source link

Engine option to echo rendered output #37

Open matthew-dean opened 8 years ago

matthew-dean commented 8 years ago

Hi, I'd love an engine option to echo the output when you call $this->render() (instead of echo $this->render(). The reason being that it makes things more consistent when used with systems like Slim, where "render" echoes the output on a view.

I can't think of a reason where I'd ever use "render" without echo. I can think of reasons why others might, but echoing seems like it should be the default option. But, for backwards compatibility, being able to drop the echo word would make my code more consistent.

gmazzap commented 8 years ago

Not against it. It could appear easier than it is because render() is used also for partials and just echo by default when an engine setting is there will cause issues.

Will try to do it for 0.7, but if you think you can do it and make a PR, I will be happy to merge.

matthew-dean commented 8 years ago

Another option might be a convenience function? Or.... doing something like "compile" for just creating the string? I could see something like splitting into compile / render (echo), or splitting into render / write (echo). Or some other naming. Maybe that's better than switching functionality.

gmazzap commented 8 years ago

An helper method sounds ok. For BC I want to leave render() as the function that returns the string. write() seems ok for the method that echo the result.

What do you think?

matthew-dean commented 8 years ago

Perfect. ^_^ It may seem like a small thing, but I would likely use write() in templates as opposed to echo ...render().

gmazzap commented 8 years ago

@matthew-dean I took this a bit further...

I noticed that some people is already using Foil as a file builder (and they are happy render() does not echo result ;)).

Moreover, write() suggests me something is going to be written in a file or stream.

This is why I thought to introduce a write() method that accepts same $template and $data parameters of render(), but also an optional third $target parameter.

When the third parameter ($target) is not provided, write() will just echo the rendered string, so it will just be an alias for echo render($template, $data).

However, it is also possible to provide a third parameter as:

In this way, I facilitate both the echoing of rendered template and the usage of Foil as file builder.

The implementation is currently in a separate branch, you can see it here: b8e9439

As soon as I can, I'll write some unit tests and then include this change in 0.7 release, that will probably released in the first half of July.

I'll appreciate your feedback. If you want to do some tests meanwhile, you can require Foil using "foil/foil" : "dev-issue-37" in your composer.json.

SlavaAurim commented 7 years ago

What do you think about other option for method name? For example: display | includeTemplate | insert? For me "includeTemplate" sounds most intuitive, like native-php "include". However "write" seems more shortly and easy for use too.