added the template_from_string function
merged branch fabpot/template_from_string (PR #874)

This PR was merged into the master branch.

Commits
-------

4599188 added the template_from_string function

Discussion
----------

added the template_from_string function

One of the most often asked question is how someone can evaluate a template string from a template.

The template_from_string function solves this problem (more in the included docs).

I have two questions before merging this:

 * What about the name? I find it quite long but also readable (`{% include template_from_string(template) %}`).

 * Does it belongs to Twig core?

---------------------------------------------------------------------------

by boutell at 2012-10-28T16:45:08Z

Interesting. What are the use cases? Would it cache in some way, the md5 of the string maybe, to avoid having this be a major performance hit on every use?

---------------------------------------------------------------------------

by gcoguiec at 2012-10-28T16:46:04Z

And what about include_from_string ?

---------------------------------------------------------------------------

by lsmith77 at 2012-10-28T16:46:35Z

Heh, was just going to ask the same thing, would a use case be loading a twig snippet from a database? something like https://github.com/symfony-cmf/ContentBundle/issues/8 ..

---------------------------------------------------------------------------

by stof at 2012-10-28T16:48:09Z

@gcoguiec It is not including anything but creating a Twig_Template instance. ``{% extends include_from_string(foo) %}`` would be very ugly.

---------------------------------------------------------------------------

by gruzilla at 2012-10-28T16:48:37Z

i'd say yes.
im not like the core must can do everything. but besides that, what are the reasons against it?
i can already think of some usecases: user defined dynamic templates, ...

---------------------------------------------------------------------------

by alexandresalome at 2012-10-28T16:49:34Z

IMHO it's a mistake to put it in Twig. People won't get how to use it. My reasons are also regarding cache: will the template be regenerated on each call?

Looks magic

---------------------------------------------------------------------------

by stof at 2012-10-28T16:50:06Z

@fabpot I'm not really sure this should be in the core.
But FYI, there is a [PR on your Twig-extensions repository](https://github.com/fabpot/Twig-extensions/pull/68) adding an ``eval`` function with the same goal (but an incomplete implementation)

---------------------------------------------------------------------------

by fabpot at 2012-10-28T16:51:00Z

The template is loaded like any other ones, so it is using the cache if you have configured it. No magic included!

---------------------------------------------------------------------------

by fabpot at 2012-10-28T16:52:44Z

@lsmith77 yes, that's the main use case.

You can also have a look at this thread on the mailing-list: https://groups.google.com/group/twig-devs/browse_thread/thread/a8b7bbae31cade18

---------------------------------------------------------------------------

by fabpot at 2012-10-28T16:54:28Z

I forgot to say that we can also move this function into its own extension, so that enabling it would have to come from a conscious choice from the developer (like the debug function in the debug extension).

---------------------------------------------------------------------------

by lsmith77 at 2012-10-28T16:55:32Z

OK great. Looks like something the CMF will need then. But it would also work for us if its part of the twig extension repo. we could of course also just drop the code into https://github.com/symfony-cmf/CoreBundle/blob/master/Twig/TwigExtension.php

---------------------------------------------------------------------------

by fabpot at 2012-10-28T16:56:59Z

I have proposed it for inclusion in Twig core (but not necessarily in the core extension) as it seems to be something many developers want to do in their code (mainly people writing a CMS in top of Twig).

---------------------------------------------------------------------------

by dlsniper at 2012-10-28T17:05:45Z

Hi

- why not name it `template_string` or `string_template`? Then it would read like: ` {% include template_string(template) %} ` or ` {% include string_template(template) %} `

- imo this shouldn't be in the core but rather in the extensions part of Twig as you just extend existing Twig functionality :)

Cheers.

---------------------------------------------------------------------------

by fabpot at 2012-10-28T17:37:12Z

I've moved the function to a new extension that should be enabled explicitely.

---------------------------------------------------------------------------

by EvanK at 2012-10-28T18:05:57Z

I would love to see this as a built-in feature to Twig, as my coworkers & I have had to implement a workaround for something similar.

(Our workaround involved writing said string to a file named after an md5 of the string's content, and then calling render() on said file, all within the php userspace. It was less than elegant, but functional.)

---------------------------------------------------------------------------

by Crell at 2012-10-28T19:11:34Z

This would lend itself to some of the wacky stuff Drupal has been discussing, too.  +1 on the feature.  No opinion on the name.

---------------------------------------------------------------------------

by jorgelbg at 2012-10-28T20:45:37Z

+1 for built-in support i think this is something Twig should do out of the box, regarding the name I guess that I'll vote for this name ```{% include from_string(template) %}``` The only thing that you can include is a template righ? So why put this explicitly in the function's name?

---------------------------------------------------------------------------

by pylebecq at 2012-10-29T09:53:05Z

+1 for me. And for the name, it seems I'm the only one but I would have vote for `eval` :smiling_imp:

---------------------------------------------------------------------------

by sstok at 2012-10-29T10:14:25Z

eval is bad name as can be confused with eval() in PHP, aka that what is passed is treated as PHP (similar to embeded PHP in Smarty). Please don't..

+1 for as it is now.

---------------------------------------------------------------------------

by drak at 2012-10-29T17:25:24Z

Love it +1

---------------------------------------------------------------------------

by acasademont at 2012-10-30T12:02:40Z

Very useful feature! +1

---------------------------------------------------------------------------

by acasademont at 2012-10-30T12:20:46Z

For clarification, now if you wanted to make dynamic include or dynamic inheritance like this ```{% extends some_var %}``` you had to supply a valid Twig_Template object. Now with this function you can supply a simple string that will be evaluated and transformed into a Twig_Template object. So this PR is not inventing dynamic inheritance but providing a helper to make it easier. Am I right?

---------------------------------------------------------------------------

by stof at 2012-10-30T12:45:08Z

@acasademont no. It is provinding a way to evaluate a string stored somewhere (in a database for instance) as a template. It is not about making dynamic inheritance easier.
If your template can be loaded by your loader (for instance a file when using the Twig_Loader_Filesystem), you can use the template name in your variable for the dynamic inheritance. You are not required to build the Twig_Template instance yourself

---------------------------------------------------------------------------

by fabpot at 2012-10-30T12:53:02Z

When you have `{% extends some_var %}`, `some_var` must be the template name. Using `{% extends template_from_string(some_var) %}` means that `some_var` contains the parent template code.

---------------------------------------------------------------------------

by acasademont at 2012-10-30T13:15:37Z

Perfectly understood, thanks!