Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Engine option to echo rendered output #37

Open
matthew-dean opened this issue Jun 15, 2016 · 6 comments
Open

Engine option to echo rendered output #37

matthew-dean opened this issue Jun 15, 2016 · 6 comments
Assignees
Milestone

Comments

@matthew-dean
Copy link

matthew-dean commented Jun 15, 2016

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 gmazzap self-assigned this Jun 20, 2016
@gmazzap gmazzap added this to the 0.7 milestone Jun 20, 2016
@gmazzap
Copy link
Contributor

gmazzap commented Jun 20, 2016

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
Copy link
Author

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
Copy link
Contributor

gmazzap commented Jun 21, 2016

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
Copy link
Author

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

gmazzap added a commit that referenced this issue Jun 22, 2016
and `WritableEngineInterface`.

Purpose is to facilitate the echoing of rendered content and the usage
of Foil as file builder.

See #37
@gmazzap
Copy link
Contributor

gmazzap commented Jun 22, 2016

@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:

  • file name, in which case write() becomes an alias for file_put_contents(render($template, $data))
  • resource handle, in which case write() becomes an alias for fwrite(render($template, $data))

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
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants