Closed (fixed)
Project:
Drupal core
Version:
8.0.x-dev
Component:
theme system
Priority:
Major
Category:
Feature request
Assigned:
Unassigned
Issue tags:
Reporter:
Created:
22 Jul 2012 at 17:13 UTC
Updated:
12 Sep 2023 at 03:50 UTC
Jump to comment: Most recent, Most recent file
Comments
Comment #1
chx commentedSmall moves.
Comment #2
chx commentedComment #3
chx commentedTagging.
Comment #4
stevectorI haven't fully tested this yet but I saw that #1290694: Provide consistency for attributes and classes arrays provided by template_preprocess() just got committed.
Reading the patch, some questions came up:
Can twig() have a more descriptive name? It's also setting a 'cache' key to two different values.
Also can that @TODO link to a relevant d.o issue? That looks rather important.
Where is twig_extension() going to get used? Should it get called from twig_theme() which is current hard-coding '.twig' ?
Comment #6
jenlamptonIt doesn't look like we are using twig_extension anywhere yet, so I've removed it from this patch. If we need it later on, we can add it back in.
I updated both the second cache line and the disabling of autoescape to include comments (with links to issues) that tell us when we can change those values back.
I'm not sure we need a more descriptive name than twig() but am open to suggestions if you have any.
I took a peek at the failing tests, but I'm not sure I see how swapping to twig for rendering the stark theme would cause pager/paging issues. Going to let test bot try it again.
Comment #8
chx commentedYou can't remove twig_extension(), it's necessary for the theme engine to pick up .twig files,
$extension_function = $theme_engine . '_extension';in theme(). The fatals are IMO a bot fluke .Comment #9
stevectorThis brings back twig_extension().
Comment #11
stevectorThere a lot of logic and looping in theme_item_list() that needs to get replicated in template_preprocess_item_list() for this to work. The previous patches were rendering a lot of "Array" into the markup.
I've done a copy-paste refactorof theme_item_list() to get template_preprocess_item_list() working with item_list.twig. However, I suspect this patch will fail testing because template_preprocess_item_list() will run also before theme_item_list(). So should we be taking that out now since I imagine it's coming out eventually anyway? Or should template_preprocess_item_list() just check if global $theme_engine is twig or not? I don't want to introduce a dependency on a global though. chx, do you have something in mind for that problem?
This patch also adds the wrapping div and title element present in theme_item_list to item_list.twig. I don't know if those were intentionally excluded. If so, I think that should be the topic of a separate issue.
Comment #12
chx commentedThis is not how this should be done, twig has the looping logic itself, we would like to see preprocess die eventually.
Comment #14
stevectorGreat, the preprocess feels sub-optimal. Can you be more specific as to the Twig-way to get this working? The patches prior to the #11 print this:
It should print
BTW, I am testing against Aggregator module which has a pager using item_list. To see this, I'm adding an RSS feed, running cron which imports stories, and going to /aggregator/sources/1
Comment #15
chx commentedThat's really weird. If you change twig to cache to a directory you can go into the generated php code and add a dump for those things, what's in those arrays?
Comment #16
stevectorThe "Array" inside of
<li>is because of the structure that's passed to theme('item_list'). Each item is an array with 'data' and (optionally) 'children.' See http://api.drupal.org/api/drupal/core%21includes%21theme.inc/function/th...The "Array" inside of
<ul>is because drupal_attributes() wasn't getting called anywhere on $variables['attributes'].Comment #17
jenlamptonAfter a brief discussion with chx and stevector in IRC, we decided to replace the item_list.twig example with link.twig. That new file is included in this patch.
Comment #19
stevectorThe link.twig needed a drupal_attributes() call.
Comment #21
stevectorOdd, I'm running tests locally and not getting those fails locally Block, Forum or Update modules. I haven't tried the rest yet. I thought clean urls were the problem but I ran the Block test with the new index.php/some/path structure and it still passed.
Comment #22
c4rl commentedI don't think drupal_attributes() is necessary anymore, see http://drupal.org/node/1290694#comment-6273540 for more details.
Comment #23
stevectorchx or Jen, do you know how that should be implemented? Should all calls to theme_link() be turning $options['attributes'] into an Attributes object? Or can we do that directly in the twig file?
Comment #24
effulgentsia commentedI'm concerned about theme_link() being the first example. That's the one that's treated specially by l() for performance reasons, and I don't think we want to make Stark needlessly slow. May I suggest theme_image() instead?
In order to initialize attributes for things that are currently theme functions, I suggest making template_preprocess_image() (if you accept my suggestion of using 'image' as the first example) call template_preprocess($variables, 'image') as its first line. The initializations done there, including initializing $variables['attributes'] is stuff we'll want to make available to all twig templates. We may need to micro-optimize that function a bit more before we can make theme() call it for every theme hook, but in the interim, most theme functions, including theme_image(), are not so frequently called as to make their preprocess function call template_preprocess() a problem.
Also, I unpostponed #1700382: Replace remaining references to drupal_attributes() with new Attributes() in case someone wants to work on that.
Comment #25
effulgentsia commentedOr, if we want an example that demonstrates how twig should interact with check_plain(), there's also theme_datetime().
Comment #26
jenlamptonadding Tag
Comment #26.0
jenlamptonTypo :)
Comment #27
fabianx commentedThis is the first attempt to integrate twig into core.
This includes the twig engine and node.twig and datetime.twig as examples.
Limitations
Strenghts
Explanation of functionality
The engine is straightforward besides some specialties that are needed to work around a twig limitation.
There are two special cases, which we need for Drupal and for which we are extending twig:
Render automatically
To render arrays automatically on demand every Twig_PrintNode (something that would be output) is wrapped in a call to Drupal\Core\Template\TwigReferenceFunctions::twig_render.
Internally twig converts:
to
Twig render detects that what it got from getAttribute is a render array and renders it.
In order to inject the function call, Twig is extended with TwigNodeVisitor.
Passing by reference
getAttribute above returns a value and not a reference. Such first hide / show and partial rendering were not working.
To solve this two things are done:
The variables passed to twig are encapsulated within TwigReference objects (which subclasses ArrayObject for performance).
Once getAttribute accesses the links key in TwigReference, TwigReference wraps a reference to the key in another TwigReference.
That way all twig ever is seeing is objects and such references are hold correctly.
To now get a real reference the Magic Drupal\Core\Template\TwigReferenceFunctions::my_function is used, which calls the getReference method on the TwigReference and calls the real function my_function with all arguments converted to array objects - if applicable.
The usage of a magic __callStatic class allows extending twig ny PHP functions without having to know that one needs to get a reference to the parameter first. Such the system is totally transparent to the called PHP function.
( The class was originally written when twig had much more functions it did support besides hide, show, render, unset. Now the &getReference could be also called directly in the wrapper functions though unset currently does not need one ... )
Fun? Fun! and Motivation
I think twig is lots of fun and seemingly really complicated templates get a piece of cake. To motivate another WIP example:
item-list.twig:
And now compare this to:
http://api.drupal.org/api/drupal/core!includes!theme.inc/function/theme_...
Thanks goes to chx and jenlampton for mentoring!
Screenshot
Comment #28
fabianx commentedComment #29
podarok#27 nice write-up as documentation
good to see it inside a summary - very nice documentation BTW
But why do we need integrate only two templates in this issue?
we have already much more converted and successfully working templates in sandbox
One click option
It just a one click option in twig.engine and it works like a charm when I`d test it - we just need to decide where the cache should be stored and create workaround for regenerating it
One click option
As for me - it looks like we need follow-up issue here for twig admin UI with making this options configurable via UI
#1777532-9: Extend Twig_Loader_Filesystem instance to get .twig templates paths with hierarchy for theme dependencies looks like we have to decide this or other workaround
Comment #30
chx commentedThe fabolous reference magic is totally Fabian's work, I really like it. I never heard of exchangeArray before, that's truly handy! I thought ArrayObject was almost useless.
I can't RTBC this because it contains code written by me here and there. The visitor mostly and some small plumbing. I asked fabpot in July to review and fix the node visitor and he helped us by adding
new Twig_Node(array($node->getNode('expr'))), $node->getLine())to thenew Twig_Node_Expression_Functionpart. So the visitor is guaranteed to be good :)Edit: I mean, we should credit fabpot in here and we should ask FabianX who else.
Comment #31
chx commented#29 we are adding only two templates because we want to move rapidly. Once this is in we can send a rain of conversion issues in parallel .
Comment #32
podarok#31 i got it, thanks!
As i remember we should not use
$syntaxfor variables in template help - it has to be justsyntaxComment #33
podaroktagging
Comment #34
catchHere's an initial pass through. Overall I'm really impressed how little code is actually in here to make things work, it looks great, and it's frankly a relief to see this patch since there's not massive amounts of time left and I'm sure we'll find some issues as we get into the conversions.
Is this worth making an upstream feature request for?
This seems like it could be a class that's put into the DIC. The DIC is more or less the new drupal_static() now.
Would be good to open the follow-up issues (or move them to the core queue) and link to them from the issue summary. I'm assuming we'll absolutely have to cache this to get acceptable performance since Twig is designed to work like that.
If you pass something invalid, like an object that's not an instance of Twig_Markup and doesn't have a __toString method, then this will return NULL but otherwise fail silently. Should we through an exception or just let it fail horribly on the (string) cast instead?
Why adding these templates here instead of converting the ones provided by modules? Stark is supposed to represent Drupal's default output, which means no templates. So i'd expect to see the default node.tpl.php changed (or duplicated with a twig version until it can be removed), rather than put into Stark like this.
Leaving at needs review since it needs more reviews.
Comment #35
chx commented> Is this worth making an upstream feature request for?
No, it's a render array specific thing. All the problems we have come from the fact that render (and consequently show and hide) get their arguments by reference and change them.
> I'm assuming we'll absolutely have to cache this
In twig-speak yes but in fact we will need to get the php loader functionality we added for dic and twig to work with it.
> If you pass something invalid, like an object that's not an instance of Twig_Markup and doesn't have a __toString method, then this will return NULL but otherwise fail silently. Should we through an exception or just let it fail horribly on the (string) cast instead?
It won't fail silently at all as the default is to pass on to render which expects an array so it'll fatal out pretty soon. We can add an array typehint to render() be more explicit.
Comment #36
catchPassing NULL is valid for type hinting, so it's going to fail somewhere in render, can we just remove the __toString() check then so it fails right there?
Yep. Is there an issue for this against the sandbox? If not I'm happy to open one.
Comment #37
chx commentedAs for using our php writer mechanisms, here's fuel for a followup: We instantiate the Twig_Environment class now, instead we need our own class which extends Twig_Environment and overrides writeCacheFile. As this moves into the DIC as catch asked, then we can just pass in a Reference to a a php loader service.
But, followup. Just wanted to show it's doable and not hard.
Comment #38
jenlamptonThis is the patch from #27 with an updated version of node.twig
Comment #40
jenlamptonRemoving attributes correction that broke tests
Comment #42
chx commented#40: drupal-twig-in-core-1696786-40.patch queued for re-testing.
Comment #43
podarok#40 look good for me
Comment #44
chx commentedNope it doesn't. We still need to move the twig static into the DIC.
Comment #45
podarokok, i`d found one http://symfony.com/doc/current/book/service_container.html
Comment #46
fabianx commentedI have a working DIC version and will commit shortly.
Comment #47
fabianx commentedRe-add tags ...
Comment #48
fabianx commentedNew patch:
Still to be done:
Setting to needs review, because this needs more reviews ... and I want test bot to test this.
Comment #50
fabianx commented#48: core-Integrate-twig-into-core-1696786-48.diff queued for re-testing.
Comment #51
fabianx commentedAdding "Needs tests" tag as per http://drupal.org/node/1697854#comment-6568622 (#1697854: Allow modules to provide both .twig and .tpl.php templates temporarily until twig is the default engine).
Comment #52
fabianx commented@TODO here:
Comment #53
jwilson3This patch combines the patch in comment #18 on #1697854: Allow modules to provide both .twig and .tpl.php templates temporarily until twig is the default engine into the one in #48.
UPDATE: worth noting that we need to later remove parts of this patch: #1805592: Remove static double theme engine code once all module provided .tpl.php files are converted to twig and twig is default This was originally noted in comment #18 on the other issue.
Comment #55
jwilson3Forgot to pull the latest... there was a merge conflict in core/lib/Drupal/Core/CoreBundle.php
Comment #56
younger870 commentedForgot to pull the latest... there was a merge conflict in core/lib/Drupal/Core/CoreBundle.php
Comment #57
chx commentedThanks jwilson3 for moving this forward. Note: most of #52 needs to be done yet.
Comment #58
fabianx commentedJust for myself:
Needs follow-up. Create issue to make twig the default engine with postponed status.
Needs follow-up, move http://drupal.org/node/1777532#comment-6569348 to Core.
Needs follow-up. Make use of DIC and the functionality in http://drupal.org/node/1747970.
Create followup meta issue to move Core to auto escape everywhere. Will need code like:
$vars['css'] = new Drupal_Safe_Markup($vars['css'])
or
{{ css|raw }} in template. But this needs to be discussed, etc.
Followup: Should be enabled, but the Exception thrown by the Twig Code is very hard, might want to handle Exceptions more gracefully. Needs discussions.
Follow-up: Not related to the twig.engine, but should open issue in core for that tag with Twig and theme cleanup or so.
Comment #59
jwilson3Was surprised to see that this issue wasn't listed on the issue summary on #1788918: [GTD] [META] Prepare for Twig main engine core inclusion until December 1, so I added it. Catching up on current work on the Twig in core effort is challenging!
Comment #60
jwilson3A (recent, but historic) posts involved here, should be required reading: #1537050: [meta] Should we keep / improve multiple theme engine functionality?.
Also found this one: #1678808: Defining a new theme engine with templates without .tpl.[template extension] breaks template suggestions. Could be slightly relevant?
Comment #61
chx commentedNotes: Drupal_Safe_Markup , no need, that exists already and is called Twig_Markup.
#60 , I closed the theme engine issue. The other I have no idea.
Comment #61.0
fabianx commentedUpdated issue summary.
Comment #61.1
fabianx commentedAdd follow-up issues
Comment #61.2
fabianx commentedF'up issues
Comment #61.3
fabianx commentedadd f'ups
Comment #62
fabianx commentedOpen Followup issues for the @TODOs in the code and link to it in issue queueMove templates to core/modules/node/node.twig and core/templates/theme.inc/datetime.twigCall datetime template in hook_theme of core/includes/theme.incLittle progress in sandbox d8-core branch. Now on to writing tests.
Comment #63
fabianx commentedmodules/system/lib/Drupal/system/Tests/Theme/TwigReferenceUnitTest.php has landed in sandbox:
http://drupalcode.org/sandbox/pixelmord/1750250.git/commit/fb2d987
Need more tests for .twig and .tpl.php support.
Comment #64
fabianx commentedHere is the new full patch that includes tests and also merges #1678808: Defining a new theme engine with templates without .tpl.[template extension] breaks template suggestions and fixes it. Tests are also added.
Antoine Lafontaine should be also credited for that.
Comment #65
chx commentedThis IMO is good to go. I wanted to remove the new function_exists call from theme() but phptemplate_render_template doesn't exist so you can't.
Comment #65.0
chx commentedOrder of f'ups
Comment #66
chx commentedOh :( hate to do this but there's a bunch of code style violations in the tests, like
if (isset($context["content"])) { $_content_ = $context["content"]; } else { $_content_ = null; }Comment #67
steveoliver commentedRan through Coder, fixed all but the CamelCase warnings:
Comment #67.0
steveoliver commentedrewrote summary
Comment #69
steveoliver commentedMaybe the two preceding spaces before comment text?
Comment #71
attiks commentedFrom the test details:
Comment #72
steveoliver commentedOK, let's see about this one. As chx pointed out,
TwigReferenceObjectTestclass also needed to be in its own file as per PSR-0.Comment #73
kika commentedI understand the feeling behind this -- agreed, Twig is great! -- but is it neccessary, especially when looking back to it in, say D9. Then it will sound like "proudly powered by PHP template engine".
Perhaps file a followup issue to remove this line before release -- when the feelings have been calmed down?
Comment #74
podarok#72 looks good for me with exeption we need follow-ups from #58
agreed with #73
Comment #75
amateescu commented@chx asked me to do a docs review, and who am I to refuse! :)
Comment #75.0
amateescu commentedadded steveoliver
Comment #75.1
chx commentedadded amateescu
Comment #75.2
fabianx commentedAdd more explanation to summary.
Comment #76
fabianx commentedI also hate to do that, but the patch in #75 removes a comment that probably should not be removed.
I wrongly merged those lines in #64.
Comment #77
amateescu commentedDone, no interdiff needed.
Comment #78
chx commentedThanks guys for seriously prettifying this!
Comment #79
chx commentedBetter title. We are not simply adding examples here.
Comment #80
fabianx commentedSmall nitpick, we're integrating twig as part of core; now it matches suggested commit message.
Removed: Needs tests as tests are there now.
Comment #81
vlad.dancer#77: core-Integrate-twig-into-core-1696786-77.patch queued for re-testing.
Comment #82
webchickThis seems like it should be at least major, but it is a feature not a task.
Comment #83
steveoliver commented@chx and @stevector: Even though
theme_item_listisn't included in this example, I wanted to point you to its issue, as it could use some review: #1800726: Convert theme_item_list to item-list.twigComment #84
fabianx commentedPer request updated documentation per coding-standards and changed to new \class policy for global non-namespaced objects:
Cosmetical changes in this patch:
* Removed use statements in favor of \class for global objects (per namespacing policy)
* Added missing @param, @return and class descriptions
* Added further extensive documentation
Non-cosmetical changes in this patch:
* Fixed the constructor for TwigReference to never store the array argument as internal reference or pass to the parent. The preferred way is to create the TwigReference object and then explicitly call setReference. TwigReference should be instantiated like this:
The reason to do this explicitly besides readability is that the PHP docs state:
Such $input is passed by value according to the API documentation, however in reality it is passed by reference, but as that could be fixed in PHP / change any time I decided to create a new Interface that explicitly passes by reference and document this interface.
New patch and interdiff attached.
As the interdiff is besides the doc and cosmetical changes trivial and just removes a non-used code path (i.e. TwigReference(some-non-empty-val) was never called), I leave this RTBC.
Comment #85
podarok#84 looks good
removing tag
Comment #86
moshe weitzman commentedI'm mixed about Twig engine. The templates get slightly cleaner, but at the cost of significant code complexity. Consider all the wrapping and abstracting we do in TwigReference and TwigNodeVisitor. None of that is needed with PHPTemplate. And how much simpler are templates? We still have if statements and hide() calls. But now we do it in a relatively unpopular format (.twig) which is rarely syntax checked or syntax highlighted in an IDE.
IMO, it is not OK to disable cache in the initial patch. One of the main complexities of Twig is it is a templating language on top of a templating language. To overcome this speed problem, it caches to raw PHP on disk, which is fine. IMO we need some experience with this, before we commit to it. If compilation adds little new code and complexity, then there is no problem. If it does add complexity, we need to know that before we start converting a boatload of templates. We already committed the Twig engine to core without having Twig having proven its worth. IMO buck stops here. For implementing the compilation cache, we should consider using #1675260: Implement PHP reading/writing secured against "leaky" script.
Do we want contrib to be able to add own functions and filters? Seems worthwhile.
Expand Doxygen a bit more please. No idea what priority is or why we hard code it.
IMO, it is not OK to disable cache in the initial patch. One of the main complexities of Twig is it is a templating language on top of a templating language. As such, it needs to cache to raw PHP. If compilation adds little new code and complexity, then there is no problem. If it does add complexity, we need to know that before we start converting a boatload of templates. We already committed Twig engine to core without having proven its worth. I think the buck stops here. For implementation, we might consider using #1675260: Implement PHP reading/writing secured against "leaky" script
Do we want contrib to be able to add own functions and filters? Seems worthwhile.
Expand Doxygen a bit more please. No idea what priority is or why we hard code it.
Wow. Dunno what
html?text:(text|e)could possibly mean. I bet I would know if it were written in PHP :)Thanks for the Doxygen. Classic function name.
It is pretty weird that we include this file during hook_init(). Seems like we could do that when we initiatize the theme system.
s/no/not
We typically reserve render() for use inside templates. We should use drupal_render() here instead. At some future date we could move to a render() method and deprecate drupal_render() function.
Comment #87
moshe weitzman commentedI'll add that the current Issue Summary here and in the Meta issue do not bother to justify why we need this. I think such a big change merits a careful analysis of what we gain, and how that exceeds the complexity cost.
Comment #88
moshe weitzman commentedSorry, multiple tabs.
Comment #89
c4rl commentedTwig has been suggested and generally lauded since March here: #1499460: [meta] New theme system
This present issue deals more with implementation itself, not whether to choose Twig is a good idea or not. It seems to me that philosophical concerns shouldn't be a blocker for the patches here.
Comment #90
rene bakxso you understand ternary in php, but the same in twig is a mystery all of a sudden?? :)
Comment #91
fabianx commentedLets discuss this :-).
I do think that:
is easier than
echo render($variables['content');at least for front-end people. They don't _need_ to know what it is, they can just use it.We also can get auto escaping and there is a proof-of-concept of this in the twig_engine branch of the sandbox.
Besides this, this should be a decision that the front-end'ers of drupal do, not the backend'ers and there seems to be huge support of front-enders for this transition. Themes can still use .tpl.php if they chose to.
Yes, as its a factory class, contrib can integrate with the DIC to provide their own extensions and functions.
This is the default value. According to Doc standards I understood it that functions should not be documented that are only implementing the default and inherited methods. As the parent class is abstract we _need_ to implement getPriority.
e is short for escape, we can use also the full escape syntax. We can also write it in the long form. I should have asked a frontender before.
This was copied from the default theme_test theme, I probably copied too much of it. I'll fix that and remove the function as this does not need re-testing. Please consider opening up an issue against theme_test doxygen :-).
This is _straight_ copied from phptemplate_init():
Please file a separate issue for that.
Will be fixed.
render() is correct here, because :-) this is only called from templates. That is why the documentation for the function says:
So in _only_ this case this is correct, because this is like a template doing {{ render(content) }}
Comment #91.0
fabianx commentedChange commit message
Comment #92
chx commentedI do not think this should held back. The complexity Moshe dislikes is because of integrating the render system with Twig.
I am updating the issue summary for why but the big one is: autoescape.
Comment #93
dozymoe commented#90 might be better read if the ternary operators has spacing. Also to complies with twig coding standards. Disclosure: I'm not well versed with twig yet.
Comment #94
dozymoe commentedSorry accidentally changed the status to need work.
Comment #95
mortendk commentedseriously this makes me smile a lot - this is the way to go forward if we wanna include "frontend" / Themers or whatever we call my kinda people.
can i do a +2 ?
Comment #95.0
mortendk commentedreasons.
Comment #96
webchickFolks, what we definitely don't need in this issue is a piling of +1s from front-end developers. Please articulate why Twig is valuable, why the complexity concerns that Moshe is highlighting are not worrisome for you, etc. Links to other places this has been discussed before are great, and can be added to the issue summary.
Comment #97
chx commentedAlso, I find it funny that we have a grudge with the complexity found in a 40kb patch when we are about to use a full blown, AST-using compiler written in a language most unfit to write such a compiler -- PHP, that is. Let me repeat my reasoning which I just put in the issue summary: in my not so humble opinion just having a secure theme system which has the additional bonus of liked by frontend people is enough to justify any complexity in PHP.
If you want I can get all the frontend people who are working on Twig template conversion to this issue to express their support but instead just look http://drupal.org/node/1750250/committers here, just look at the number of people you never heard and are enthusiastic about Twig enough to contribute a template or two.
Comment #98
chx commentedSorry x-post but I think the reasoning is enough.
Comment #99
podarokagree with #97
Yes - today #84 TWIG version looks not so valuable like PHPtemplate after a bunch of years after 2004(?) and with hidden background from reviewers that are familiar with tpl.php syntax, but not familiar with much more simple .twig ...
Anyway as described in roadmap http://drupal.org/sandbox/pixelmord/1750250 this is a really initial patch
We have many templates converted already in sandbox http://drupalcode.org/sandbox/pixelmord/1750250.git/tree/0c6519399921a9b...
the syntax in .twig is much more preffered for front-end developers and this is the main point decided before
The good point is that all of the theme functions will be removed and moved into .twig files for making possible to change output at the design layer without php skills needed
Autosanitization, caching, compiling from .twig into .php that can easely be integtated with APC(or any other) caching system.
And of couse - no more @sql select@ code in tpl.php files
...
Comment #99.0
podarokmore reasons
Comment #99.1
chx commentedmore
Comment #99.2
fabianx commentedAdd remove of double engine code for modules once twig is default issue
Comment #99.3
moshe weitzman commentedUpdated issue summary.
Comment #100
fabianx commentedI did some little xhprof-ing with cache enabled:
Twig is neither slower nor faster than phptemplate. It is roughly the same currently.
Also: The auto escape filter of the sandbox does not have that big of an impact because __construct and __toString, which is needed to wrap already escaped strings within Twig_Markup objects are really really fast and not double escaping makes most of the filter a trivial pass thru. This is for "normal" pages ...
Regarding the importance of security and auto escaping:
I consider myself well versed of the need to escape strings and check output carefully, but a simple usage of {{ title }} instead of {{ label }} in node.twig was enough to create the XSS. And the same is true for node.tpl.php in current core.
If I copy over a template from Drupal 7 for node.tpl.php I suddenly have a XSS hole. Yep, that is a bug. The label change seems to have been done without thinking over the consequences for security of older templates as $title was safe in node.tpl.php since 2007. The label change probably assumed that $variables['title'] is not set, but it is afterwards by the preprocess. (more about this in bug report issue: #1810710: Remove copying of node properties wholesale into theme variables)
If Drupal 8 had shipped with a node.tpl.php with $title insecure (and no auto escaping), I would probably have been the first victim ;-).
This example just shows how easy it is even for a non-themer to accidentally introduce a XSS, such I would support the statement that 100% (minus rounding errors) of custom templates are insecure and auto escape is a feature that can make drupal more secure.
The twig_engine branch of the sandbox automatically escapes the {{ title }} and the site is safe.
Comment #101
fabianx commentedI just finished a first integration of Twig's cache with drupal_php_storage(). It is quite simple to do so and works like a charm.
Here is the diff of the current work in the sandbox:
http://drupalcode.org/sandbox/pixelmord/1750250.git/commitdiff/1541e5810...
Further things for this should be discussed in: #1806546: Twig: Performance Testing / Enable twig cache by using the Drupal PHP Loading/Writing.
Comment #101.0
fabianx commentedUpdated issue summary.
Comment #101.1
moshe weitzman commentedUpdated issue summary.
Comment #101.2
podarokUpdated issue summary.
Comment #101.3
podarokUpdated issue summary.
Comment #101.4
podarokUpdated issue summary.
Comment #102
swentel commentedRe: benchmarks, on my machine twig is really slower. The CPU goes up and it's eating more memory. Now, of course, this could be because I just told bartik that its engine is now twig, might be that loading two engines causes overhead, but I just want to pass along the figures. Or am I missing something else ?
single node:
Before: Page execution time was 85.92 ms. Memory used at: devel_boot()=1.19 MB, devel_shutdown()=4.75 MB, PHP peak=5 MB.
After: Page execution time was 101.46 ms. Memory used at: devel_boot()=1.19 MB, devel_shutdown()=5.56 MB, PHP peak=6 MB.
Frontpage with 10 nodes:
Before: Page execution time was 115.5 ms. Memory used at: devel_boot()=1.18 MB, devel_shutdown()=5.05 MB, PHP peak=5.25 MB.
After: Page execution time was 131.58 ms. Memory used at: devel_boot()=1.18 MB, devel_shutdown()=5.89 MB, PHP peak=6.5 MB.
Comment #102.0
swentel commentedExpanded on downsides
Comment #103
Crell commentedswentel: Are those benchmarks with compiled Twig files, or recompiling on each request? Because recompiling Twig on each request is definitely going to be slower since the compiler is non-fast, but also not what anyone should be using day to day.
Comment #104
swentel commented@Crell - by reading the issue summary above, it seems that caching in this patch is disabled, so this is probably recompiling every time.
From the summary
It seems ironic to me to commit a patch where auto escaping is disabled by default too, while saying that we need this because of security. Of course, this is the first patch, this call be done in follow ups, but it concerns me a little though.
Comment #104.0
swentel commentedAdded Changes section
Comment #104.1
podarokUpdated issue summary.
Comment #104.2
moshe weitzman commentedShortened autoescape downside
Comment #105
podarokupdated summary with Community Benefits
Comment #105.0
podarokUpdated issue summary.
Comment #106
effulgentsia commentedGiven how relatively small the diff in #1806546: Twig: Performance Testing / Enable twig cache by using the Drupal PHP Loading/Writing is, why not merge that into this issue?
Auto escaping, I agree can be a follow up, because while that's one benefit of Twig, it's not the only one. Even if we didn't solve auto escaping integration with Drupal (though I'm confident we will), we can escape in preprocess functions or document that escaping is needed in the templates, both of which we already do in all released versions of Drupal.
Comment #107
effulgentsia commentedxpost
Comment #107.0
effulgentsia commentedUpdated issue summary. PHP
Comment #107.1
podarokUpdated issue summary.
Comment #107.2
podarokUpdated issue summary.
Comment #107.3
podarokUpdated issue summary.
Comment #108
sunI was equally confused by this when I reviewed this some days ago. The problem is that it looks like "must be some special new Twig command or something", because it does not adhere to our coding standards for ternaries; i.e., it lacks spaces:
That way the confusion is reduced to
(text|e), and I'm not familiar with Twig's template interpreter yet, but I hope you guys studied it, and so I guess the parenthesis are required since it would otherwise interpret it as(html ? text : text)|e.Template variable filters generally are a good thing and simplify a themer's interaction with templates, so I don't see anything wrong with this per se.
However, I am slightly confused why we need the escape filter in this case? Wasn't one of the main ideas behind Twig that it is more secure, removes the burden from themers to think about security, and generally tries to keep the internals out of templates? In other words: Shouldn't the preprocessor escape 'text' when needed already, so the template only ever sees 'text' and just simply uses it?
Comment #108.0
sunUpdated issue summary.
Comment #109
fabianx commentedYes, I agree that the sample template could be changed to include a preprocess function and I think we have something else in the sandbox, but this example was explicitly requested by effulgentsia as an example for how to escape.
With auto-escape on this can change to either:
html ? (text|raw) : textOr in case of a proper preprocess function it could do the clean up in preprocess.
And mortendk would probably write (if he needed to change the raw links for some reason:
Note that even if the preprocess already applied a "safe" filter on the text the double raw still works.
Comment #109.0
fabianx commentedReverted syntax downside. Last edit was not clear, and there is a corresponding item in Benefits.
Comment #109.1
fabianx commentedAdded corrections to downsides
Comment #109.2
moshe weitzman commentedUpdated issue summary.
Comment #110
dries commentedGlad to see progress being made on Twig. I still believe Twig could be a really compelling feature for Drupal 8.
My preference would be for this patch to be expanded before it gets committed. If we commit this patch as is, it would create at least one critical follow-up patch (e.g. missing compilation step) and presumably at least one major follow-up patch (eg. missing security step). We're constantly at our major and critical tasks thresholds, and I don't think Twig should be blocking official initiatives (eg. web services) right now. By being a bit more strict here, it helps us prioritize.
Another reason for this is that as the patch stands, it doesn't bring huge benefits to Drupal as the syntax simplifications alone don't seem important enough to warrant the extra code complexity. It would be best if we could demonstrate that Twig can be fast and secure as part of this patch.
Specifically, I think we need to include both the caching and the security components into this first patch. Would that be doable? I'm therefore setting this back to 'needs work'.
As mentioned at the top of my comment, I remain excited about Twig in core and encourage us to keep working on this patch. Great work!
Comment #111
webchickJust to give my impression on the Twig syntax as someone who is not a front-end developer, but who taught Drupal Theming 101 off and on for about 5 years with Lullabot...
Themers not having to understand the difference between PHP array and PHP object syntax is absolutely huge. We used to need to give a half hour presentation just on how to navigate through something like
$node->field_image[0][LANGUAGE_NONE]->file['alt']['text']or whatever. "Just use dots everywhere" is SO much easier to explain.However, it does bring up a really important question, which is "how does someone know what all the 'dot-thingies' are under 'node'"? And also, for that matter, "How do I know what all the possible 'node' like thingies there are?" We can no longer toss a
var_dump(get_defined_vars());at the top of the node.twig file. Alex pointed me to http://twig.sensiolabs.org/doc/functions/dump.html ... do we have that extension? Cos having an equivalent to that is going to be absolutely critical to themers understanding what info they have to work with. The docs are great, as far as core goes, but that completely goes away the second you install even one contributed module (or, for that matter, disable a module from standard profile, like comment).Also, on the debugging note, what happens when I put:
or
...in my twig file? I'm going to assume that in the first case it doesn't output anything, and in the second case it leaves the text as-is since it didn't complete a parse, but not sure, and don't have time to test atm. Will try and do so this weekend. But it's important to understand how Twig performs error handling, cos it's the difference between having a completely frustrating experience and being able to quickly remedy any mistakes.
Back to syntax again...
That '-' sign was weird, and I thought it was a mistake at first. Alex explained that it's short-hand for "if the attributes variable is empty, don't print a space before it" Kind of a neat short-hand, but will take some explaining to people, I think.
Do we have some general Twig docs in core that we can point themers to to help them understand these kinds of nuances?
Another huge improvement: Themers no longer have to understand when they print $foo and when they print render($foo). And yet...
...we still retain the benefits of the render API, in that we don't need to make a template file change if we add a new field to a heavily-themed content type template. Plus. Freaking. One.
However, one thing that I do find kind of confusing...
seems to always mean "print 'foo' out the screen.
seems to always mean "do this PHP logic."
So why then is it:
and not
?
Comment #112
webchickOh, one other minor nit:
is
{% if !page %}not possible? That'd definitely be more intuitive, IMO.Comment #113
webchickBtw, here is the diff I uploaded for reviewing 8.x's node.tpl.php vs. this patch's node.twig, since the patch doesn't have the removal of node.tpl.php in it for some reason: https://gist.github.com/3880359
Comment #114
fabianx commentedEven better:
Comment #115
webchickOh, cool, that works too. :)
Comment #116
fabianx commentedWe could add back easily the {% hide(xyz) syntax ( I removed it for sake of simplicity of the first patch. )
Twig itself is not totally clear on that.
The basic rule is:
{{ }} means PRINT THIS.
{% %} means statement.
So, from that understanding we should add hide / show tags to twig instead of hide / show functions as those do in twig not return something nor should print something.
Thoughts on that from anyone else? For the policy around our syntax additions?
Comment #117
effulgentsia commentedJust to clarify, it's not conditional on the content of the variable. A "-" on the left side of the twig tag means remove whitespace preceeding that tag, so
"foo" {{- bar }}is identical to"foo"{{ bar }}. The "-" allows a space to exist in the template for human readability but get removed from the output markup. This is explained in more detail in http://twig.sensiolabs.org/doc/templates.html#whitespace-control.Let's not replicate the Twig project's documentation. But finding a place in Drupal docs where we can link to that, and add any Drupal-specific isms (like that the value of "attributes" always contains its own leading space when not empty, so templates shouldn't output an additional leading space) seems like a good thing to do.
Comment #118
fabianx commented@all that are interested in how twig is integrated with caches, please follow:
#1806546: Twig: Performance Testing / Enable twig cache by using the Drupal PHP Loading/Writing
I am using a git two-patches in one approach now using the basic patch here as base and rebasing on this base as needed.
I'll update this issue here once we have something that is RTBC over there.
It would be great if we could have the heavy discussion in the other issue: #1806546: Twig: Performance Testing / Enable twig cache by using the Drupal PHP Loading/Writing
Comment #119
Crell commentedAdding Drupal-specific Twig extensions should be fine, in general, but note that it increases the work to support Twig.js in contrib (since I know core won't), so let's not do it willy nilly.
As far as documentation goes, we should primarily point people back to the official docs: http://twig.sensiolabs.org/documentation
Our own extensions we should definitely document but for things that are not Drupal specific, single-source and push upstream. If we find those docs lacking, pull requests are welcome. :-)
Comment #120
webchickSorry, to be clear I wasn't advocating us re-writing http://twig.sensiolabs.org/documentation in Drupal. More making sure that links to that were prominently placed in places folks would be looking for it. The first one I can think of is themes/README.txt maybe.
Comment #121
chx commentedAbout missing variables, when in strict mode which we want you get an exception for missing variables too so that typos are caught.
Comment #122
podarokthe first performance test
#1806546-12: Twig: Performance Testing / Enable twig cache by using the Drupal PHP Loading/Writing
+51% speed-up
Comment #123
jenlamptonThis is exactly the same problem we have today in core. There are docs at the top of each template file that explain all the variables that are used in the template file (as well as handful of variables that essentially represent Drupal's wasted efforts, since they are rarely used) but the docs to not include information about all the printable variables at every level of a renderable, they do not remain accurate after contrib adds or removes variables, nor does get_defined_vars() explain all the thingies that are actually printable below node. yes, you can get at *everything* in a node (or a renderable) but the results always contain an overwhelming amount of unnecessary information for a front-end developer, most of which shouldn't even be printed in a template - and I think that's certainly more confusing than what we will be able to get with a Twig inspector.
This patch does not include an inspector, but it is part of our plan. (see #1783134: [META] Make it possible to inspect twig variables and get information about objects and render arrays)
@Fabianx I think we should put back the {% hide(xyz) %} syntax, I agree with @webchick.
When re-rolling this patch I actually tried to change the hide statements to use the {% %] syntax, since I thought it was confusing that they were using the {{ }} syntax. But when it didn't work I abandoned the effort and put things back :)
Comment #124
fabianx commented@webchick @chx:
We cannot use twig strict mode directly as it removes some really nice performance optimizations, but we can add a semi-strict mode that gives a warning when a var is not defined (and drupal error messages are on). I personally find throwing of an Exception too much ...
I however would like to do that as a follow up.
Okay, let me summarize the TODOs for _this_.
Current TODOs
New Followups:
Anything I am missing here?
Comment #125
moshe weitzman commentedDemonstrate benefits and risks with autoescape enabled.
Comment #126
fabianx commentedThat was what I meant with "Discuss safe mode and show performance characteristics (up next after the above is done)", but yes, thanks! :-)
Comment #127
fabianx commented* On popular request merged #1806546: Twig: Performance Testing / Enable twig cache by using the Drupal PHP Loading/Writing into main patch.
Note that auto reload of templates only works if either
a) debug is enabled (from next patch on the case for enabling "dump")
b) options['auto_reload'] is set in twig_environment (default "null", defaults to "debug" setting).
In case that auto_reload is not enabled a user will need to clear the cache to have TWIG compile new templates.
* Added debug functions so that:
** {{ dump(label) }} works or any other variable
** {{ dump(_context) }} works - useful for printing all available variables
A general production / development mode switch would be really helpful here as well ...
New patch including re-adding of {% hide / show coming shortly.
Comment #128
fabianx commentedAs of now Twig is neither faster nor slower than Core, see #1806546: Twig: Performance Testing / Enable twig cache by using the Drupal PHP Loading/Writing for details.
And here is the new patch, which is optimized for very fast execution of Twig Templates.
In fact, once "Declare render variables in PHP via hook_...() and remove superfluous twig_render() calls" is in the templates look _almost_ the same like the normal node.tpl.php in compiled form :-D. (Just accessing of array elements is done via getAttribute, but that was the idea of the whole exercise ... )
But that can be left as followup as already now Twig is as fast as core for rendering 100 nodes on /node.
Still @todo:
Comment #129
fabianx commentedFor testbot
Comment #130
chx commentedFabianx told me on IRC that ab -n 100 was 583+-12.8 for Twig and 591+-10.1 for core. That's indeed about the same.
Comment #131
cosmicdreams commentedtwig_convert_to_reference no longer exists
Comment #132
fabianx commentedHave most todos cleaned up, new patch coming soon :).
Comment #132.0
fabianx commentedUpdated issue summary.
Comment #133
fabianx commentedDone @todo:
Remove superfluous test_theme functions found by MosheRemove typo found by MosheOpen up follow-up for: Declare render variables in PHP and remove superfluous twig_render() callsInvestigate webchick's questions in regards to syntax errors / compile time errors / runtime errors / undefined vars.Follow-up here: #1815250: Type twig variables in PHP and remove superfluous render_var() and getAttribute() calls from the generated code
To answer webchicks questions:
Syntax errors like {% hide content.links) %}: Give a hard exception with lineno and filename in regards to the .twig file
Undefined vars like: {{ non_existing_var }} give a warning right now if being printed (further warnings can be done as part of follow-up of #1806538: Remove @todo about enabling strict_variables in Twig)
Again I was able to get the lineno and filename and it's also very easy:
Twig_Error takes care of finding the template in the call stack and finding the right line number in the original .twig file.
I attached an example screenshot of how this looks.
Remaining @todo:
We are continuing in #1712444: Twig: Activate autoescape mode for now to work on the integration of auto escape on top of current patch like we did with Performance / Cache.
New patch attached.
Comment #133.0
fabianx commentedAdded new followup
Comment #134
podarok#133 nice error handling here
all other looks good for me
adding tag
Comment #135
jenlamptonNM this comment. Found the twig templates moved to core modules :)
Comment #136
jenlamptonEh, here's a first attempt at adding some docs. I'm not entirely sure how to reference files within twig, can I do it like this?
There's also a few bits I didn't really understand, so there are a few @todos left. Hopefully this is a step in the right direction.
Comment #137
Anonymous (not verified) commented-1 Twig
Instead of adding a new template engine, should concentrate on making the existing theming easier.
Not add a new language to learn.
It doesn't the solve the problem in my opinion. Namely making design easier in Drupal.
Being able to add some simple functions such as this:
http://drupal.org/node/1813084
Would make my life so much easier than this Twig stuff.
Should I even spent time writing such a function if we are switching to Twig?
Will I have to redesign stuff again? Please can we have a theme layer which is
for the most part independent of core changes. Designing and testing stuff takes a lot of time.
It is one big negative of a lot of CMS's that keep changing the code to do the layout. Every time you do we have to sand, spackle, and repaint the whole house, figuratively speaking. Stop it, please. I'm really not waiting for this solution.
Most designers I know, speak some kind of PHP, but Drupal is just really complicated that is the problem.
Either that or give us some simple functions such as WordPress please.
If this is going to be implemented please allow for a way to keep using PHP as well or alternate ways of creating themes. For example a module in which to easily make a query from data, add the necessary markup (using templates), and assign this to locations. It would be so much easier than this stuff.
Can we please explore that direction as well? It would fit in with the new admin backend.
For example something like an extended version of these two modules, but then also be able to add elements like div, ul, a, etc:
https://drupal.org/project/block_aria_landmark_roles
http://drupal.org/project/block_class
I believe Concrete5 also works somewhat similar to this.
Twig code way of working looks complicated and convoluted.
Is this solution really providing the answer to the problem? No, it doesn't.
What are the performance figures for adding another layer?
We really don't need another theme layer at this point.
Will we still be able to design without Twig? Who uses Twig? Most of us work with different products, none of which I know use Twig, won't this lead to vendor lock in, and/ or raising the threshold for designers to build designs for Drupal.
There is a risk that Drupal is painting itself out of the market here. Please stay with generally accepted languages and formats, and make those easier if you want more designers. Don't change the label, but not solve the underlying problem.
Thank you for your consideration.
Comment #138
Crell commentedDesignDolphin: Not to put too fine a point on it, but that ship has sailed. Twig has been in discussion since March and has broader support from the front-end community than any other front-end change in the last 7 years. All of your questions have already been answered elsewhere. Please do not drag this issue off topic.
Edit: check #1499460: [meta] New theme system
Comment #139
Anonymous (not verified) commentedCrell, not to put too fine a point on this either.
As far as I see I'm following feature request guidelines.
I'm very much on topic.
Only recently heard about Twig, the cue is set to review.
I'm reviewing.
So, please if you want to respond, please follow guidelines.
I know this Twig thing is dear to some. That is fine. I respect other people's opinion. I have stated my case.
I will leave the floor to others unless I have further arguments for the issue. My request for other (additional) options stand. I would very much like this to be taken into consideration.
You cannot expect everybody in the community to react right away, sometimes it takes time to trickle down and hear about stuff. Drupal has an huge ecosystem.
Trying to save everybody some time.
Comment #140
podarok#136 for review such longterm patches good to see interdiff
please...
Comment #141
podarok@DesignDolpin
Please
this topic is for patch and integration - please - make Your own follow up and discuss there.
Comment #142
chx commentedI have reopened the meta issue for further discussion.
Comment #143
webchickRight, I agree the "review" in this issue should be focused on the patch at hand.
For review of the meta concept of making the theme system better and where Twig fits into that, probably follow-up at #1499460: [meta] New theme system
EDIT: Link copy/paste fail. :P
Comment #144
Anonymous (not verified) commentedAs per cue request moved my comment here, as I do not want to sidetrack an issue:
http://drupal.org/node/1441320
@Crell,
Do want to make a point of order that while doing additional background research I read quite a lot of criticism and alternatives in that issue about Twig, and that it may not be as widely accepted as how your text could be interpreted. Am I to understand that this is a misunderstanding or misinterpretation on my part? I hope designers will have continued input as part of this community.
Another point of order,
This issue is labeled a feature request, something which needs broad community support. If this is a patch review then this should be labeled as a task or something else. It is confusing this way. I do not feel it proper with getting corrected for doing my utmost best to follow guidelines.
Edit:
@Webchick:
Yeah, Noticed that. Thanx for the edit, got me pointed in the right way. :-)
Comment #145
Anonymous (not verified) commentedTo avoid confusion in the future on this issue changed the title to something which (hopefully) defines the issue a bit better.
Please mark from feature request to task if there has been a definite decision made to integrate this into core?
Comment #146
cweagansPretty sure that's the direction we're going. As Crell pointed out, "Twig has been in discussion since March and has broader support from the front-end community than any other front-end change in the last 7 years."
Comment #147
webchickDesignDolphin: There's nothing inherent about feature request vs. task that means review the concept vs. review the patch. I'm not sure where you're getting that from. Normally, "concept" type issues are prefixed with "meta" (like #1499460: [meta] New theme system) but everything else is about reviewing code.
Now, let's please move any further off-topic questions/points to off-topic issues. :) We're already at 145+ comments here, and we're not nearly done with reviewing the code yet.
Comment #148
Anonymous (not verified) commented@Webchick, last post from me on this issue, but you don't have a contact form, and I would like to clear things up, please see:
http://drupal.org/node/317 -> http://drupal.org/node/945492
Also I don't see that meta thing explained anywhere, but am willing to sit down with a few people to hash out the documentation.
Edit:
Missed a link as well.
See also: http://drupal.org/node/10262
Back on topic.
Comment #149
podarok#136 looks good for me
reviewed document changes
Comment #150
chx commentedDocumented 'em.
Comment #151
podarok#150 good to see all @todo for documentation filled with a proper info
looks good
removing tag
Comment #152
catchFor anyone not following, #1818266: [meta] A secure theme system (with twig) is discussing the auto-escape behaviour (that Dries wanted included here before commit as well as performance).
Comment #153
webchickDoing some housekeeping. AFAIK this doesn't need to be assigned to Dries right now. Feel free to do so again once it hits RTBC!
Comment #154
fabianx commentedRe-rolled #150 for testing against core with DIC / removed one conflict.
Comment #154.0
fabianx commentedUpdate issue summary
Comment #154.1
fabianx commentedAdded link to twig coding standards
Comment #155
fabianx commentedIt's show time!
This patch merges all feedback and lessons learned from both #1712444: Twig: Activate autoescape mode and v3 from #1818266: [meta] A secure theme system (with twig).
The auto-escape for twig in this patch is fully enabled. To prevent double-escaping all strings in $variables are marked as secure by twig engine, which is a valid assumption given that we continue to support non autoescaped phptemplate files for now.
Very detailed benchmark results of the best approach when #1815250: Type twig variables in PHP and remove superfluous render_var() and getAttribute() calls from the generated code is in, can be found here: http://drupal.org/node/1818266#comment-6648238.
Compiled template performance
Before we get to the benchmarks, another concern was how compiled twig files look like. Here are the links:
Compiled Template: https://gist.github.com/3950182
Auto-escaped Template: https://gist.github.com/3950401
After #1815250: Type twig variables in PHP and remove superfluous render_var() and getAttribute() calls from the generated code is in:
Optimized compiled TwigTemplate: https://gist.github.com/3950254
Auto Escaped Optimized Twig Compiled Template: https://gist.github.com/3950411
The ternary (?:) operator is not used by Twig, because of a PHP 5.3 bug that makes it slower than if / else when encountering big arrays.
Note: No one ever will see this compiled code and for production mode it could be optimized even more (temporary variables would need to be checked just once).
Benchmarks
The following benchmarks are for the current status quo for 100 nodes on /node. Benchmarks are with APC on
and APC ClassLoader enabledas per msonnabaums suggestion:How to read these benchmarks;
These benchmarks show the Wall-Time (wt), used CPU time (cpu) and the Memory Usage (mu) plus the Peak Memory Usage (pmu). They are run 1000 times and the minimum is taken. For each line there are four numbers: Run 1 time, Run 2 time, Diff and Diff %. So if it shows core..twig, run 1 = core, run 2 = twig and the difference is twig - core.
The report is created via XHProf tools. They have been re-run several times to make sure the found minimum is accurate. The server is completely dedicated and has no other load during the benchmarks. URLs to XHProf raw data can be provided on a case by case basis. Just contact me for that, please.
100 nodes - /node
EDIT: Above with 1000 runs now - was not different.
Note that #1815250: Type twig variables in PHP and remove superfluous render_var() and getAttribute() calls from the generated code will make this faster as shown in http://drupal.org/node/1818266#comment-6648238.
Patch attached and needs review!
Comment #157
tim.plunkett#155: core-Integrate-twig-into-core-1696786-155.diff queued for re-testing.
Comment #158
jenlamptonMarking as RTBC to see if I can prompt another comment from Moshe :)
Comment #159
fabianx commentedTo elaborate on #155:
The patch as attached from #155 currently makes Drupal around 9% slower when 100 nodes are rendered on /node.
This can be improved to 6% with #1815250: Type twig variables in PHP and remove superfluous render_var() and getAttribute() calls from the generated code and possibly further with a production / development switch.
Comment #160
catchDiscussed this a bit with Fabian. This is an obvious performance degradation as it stands at the moment, however I'm reasonably confident we can claw it back. Also I really, really appreciate the amount of work that's gone into both performance analysis and actually optimizing based on that for this patch, this makes me confident that this work will continue to be done after commit.
For comparison, #1599108: Allow modules to register services and subscriber services (events) introduced around a 5% performance degradation, went in before the degradation or potential fixes had been properly discussed, and the issue that was supposed to fix it (#1706064: Compile the Injection Container to disk) actually didn't, instead it's being tracked in #1716048: Do not boot bundle classes on every request. Registering bundles ought to be close to free once we're doing it in a sane way, so by comparison 8% for a new rendering systems doesn't feel so bad. It's also a lot less than the 30-40% overhead we introduced in Drupal 7 when converting the node body to a field - for 30 nodes, not 100.
Since Twig was originally put forward, I've been assuming we can remove a lot of the work we currently do in preprocess preparing various versions of different variables, and rely on the Twig template compilation to handle that stuff instead. Currently all kinds of variables get prepared/sanitized in preprocess that might never even be printed in a template. I've opened #1825952: Turn on twig autoescape by default to discuss this some more. Most Drupal 7 sites spend a lot of time on preprocess, how much of that we can save is going to be hard to quantify until it's actually done though.
I've not reviewed the latest patch yet, however I'll try to do it over the next day or so (so no comment on it's RTBC-ness in general). Assigning to Dries since I think he should also have a look a this sooner rather than later.
Comment #161
podarokGood to see here plan for benefits that can speed-up twig after roadmap done
As for me these points are described at frontend of our sandbox
anyway we should commit RTBC patch with a small regression into core for Get This Done #1788918: [GTD] [META] Prepare for Twig main engine core inclusion until December 1 and for possibility to create all follow-ups
PS. looks like it is the point to connect to VDC initiative due to dynamic templating engine in views theme layer and possibility make it more deeper integration with Twig, so taging
Comment #161.0
podarokAdded merged issue
Comment #161.1
podarokUpdated issue summary. Add performance related issues
Comment #161.2
podarokUpdated issue summary. Typo fixed
Comment #162
amateescu commentedI think the assignee change was unintentional.
Comment #163
David_Rothstein commentedI think this really needs more reviews. I'm going to try to do one later today myself (assuming the power doesn't go out on the U.S. east coast...)
Also, I agree with @DesignDolphin: 99% of core issues are about reviewing the concept and code together, not just the code. For bigger issues (like this one) that can be different, but there's no way most people will ever know that. Changing the issue title a bit should help clarify things.
Comment #163.0
David_Rothstein commentedUpdated issue summary.
Comment #164
David_Rothstein commentedOK, here's a review. And for starters (before I forget), I'm totally impressed with all the work that went into this patch.
node.twig
I started by jumping into node.twig directly and playing around (figuring that's how 99% of people will encounter this). A couple comments:
The node.nid and attributes.class syntax was really, really nice to see :)
In the second part, I had no idea what the "-" was in front of the attributes. I had a few guesses which turned out to be (laughably) incorrect, then eventually read the Twig documentation and also saw comments earlier in this issue which pointed out that it strips whitespace on the left.
This syntax choice by the Twig project is... uh, interesting :)
I think it adds significantly to the complexity of the templates because it shows up in random places and therefore gives the impression that it's a lot more important than it actually is. Do we really need to use it in Drupal? (Where is the whitespace being added anyway that it needs to be stripped here - is Twig adding it itself?)
I tried playing with these, and the first worked nicely, but the second dumped garbage to the screen. I think what might be going on here is that the output of dump() is itself being auto-escaped?... oops :)
Somewhere above it was suggested this could be
{% if not page %}instead? I agree it would be more readable. (Minor point, though.)I'd be curious to see something like page.tpl.php also (which has t() calls, theme() calls, and more complex logic embedded inside of it, that kind of thing)... it's closer to a realistic example of what themers actually deal with on moderately complex websites. I suspect there's already code for page.twig out there somewhere, but it's not in this patch :)
After some playing around, I got the impression that this would work:
I guess that's not too bad, but it doesn't look like an improvement over standard PHP either. I was a bit thrown off that I had to call render() directly and that using {{page.sidebar_first}} in that statement apparently didn't work.
Security
I'm concerned that there hasn't been a full discussion of the consequences of auto-escaping. I'm personally not yet convinced that the way this patch is using it actually improves security.
Sure, if you do something like
{{ node.body.und.0.value }}it will save you from security issues, but if you do{{ node.body.und.0.safe_value }}(which is a better variable to use anyway), it will incorrectly escape your HTML. Your site will have bugs until you figure out that you need to do{{ node.body.und.0.safe_value|raw }}instead. Which is fine until you apply this "lesson learned" again and start doing{{ node.body.und.0.value|raw }}too... Oops, no more security.In short, this doesn't save template authors from having to think about text filtering. They're still responsible for making security decisions, only we're now making things very confusing because everywhere else in Drupal you do it one way (explicitly escape text), and here in the template files you do it the exact opposite way.
Second, it's worth pointing out that
{{ title }}is still an XSS hole even with this patch (#100 mentioned that as something which auto-escaping might fix, but I think the implementation changed since then). As mentioned in that comment, this is an independent bug (#1810710: Remove copying of node properties wholesale into theme variables), but the point is that stupid security mistakes in the preprocess layer still leak into templates even with this patch. And keep in mind that by removing PHP from templates, we're going to force theme developers to work more in the preprocess layer whenever they need to get complex things done, so there will be plenty of opportunities for them to introduce security mistakes there...Options #1 from #1818266: [meta] A secure theme system (with twig) avoids these problems and could provide actual security in template files (notably, it also has nothing to do with Twig directly, although it does work much more nicely with something like Twig than it would otherwise). It is also harder and might not be possible to do correctly. But I'm worried that the current patch implements solution #3 from that issue as a means of meeting the stated "security" goal and getting this patch committed, but it might actually be better simply to do nothing for now. (Especially because this also seems to be responsible for a lot of the performance hit, plus perhaps the debug() issue above, etc.)
We don't necessarily need "better security" in order to use Twig anyway; there are other benefits too (but see more on that below).
Big picture
Overall, I see a lot of good things about Twig and this patch but I share some of the concerns @moshe weitzman raised above. I think one way to phrase that (in the context of this issue) is that the issue summary provides a list of proposed benefits but the current implementation doesn't really deliver on all of them (in particular, security and performance), nor is it obvious that it ever will. So we're potentially adding a lot of code complexity for a smaller number of benefits than originally assumed. (And notably, a smaller number than assumed by many people in #1499460: [meta] New theme system as well; although the validity of those assumptions was questioned early on in that issue, my impression is that most people seemed to conclude "Twig = security" when they evaluated things and made their decision to support it, even though it was never actually that simple.)
Again, Twig may be worth it regardless (and I'm 100% in favor myself provided that the more complete solution from #1818266: [meta] A secure theme system (with twig) can get in) but I think the tradeoffs right now are really a lot more subtle than they've been given credit for.
Other issues
I didn't really review the whole patch, but a few things stood out. In no particular order (and some are relatively minor):
NULL, not null. (Actually lots of these all around, including FALSE vs. false.)
Hm, this seems to embed Twig very deeply into Drupal... what about other theme engines? Maybe in practice it doesn't matter much.
I don't see why we need this comment here; that's a bug, but it's been around a long, long time (see #405270: Strange duplicate IDs during node preview and when a node is displayed multiple times on a page) so it has nothing to do with this patch. Having it there is kind of a distraction given how many people we expect to use this file.
So presumably it should have been revisited? :) That code looks like it's going to be incorrect otherwise.
Comment #165
catchI still haven't reviewed the patch, but based on discussion with FabianX about this yesterday I'd mostly come to this conclusion as well. This was part of the intention of opening #1825952: Turn on twig autoescape by default (we might not want to do what that issue suggests in the end, but we'll not want to go with the approach currently in the patch whatever happens, and I definitely want to cut down on preprocess bloat - for me that's the main benefit of using Twig at all).
Comment #166
fabianx commented@David Rothstein:
Thanks for the very extensive review.
Let me answer the questions / remarks
node.twig
1. {{ attributes }} vs. {{- attributes }}
This was mainly decided by themers, etc. as part of the twig initiative itself. The Attribute class adds the whitespace to allow to do:
>
?>
in PHP or
in Twig, but Twig themers did not like it, so chose to use:
instead. I think this could be changed easily in the Attribute class and I don't know the background / reasoning for this choice.
I do agree though that twig work arounds a syntax problem here that could be solved directly. But I don't think you want to do this as part of this patch ...
2. Dumping variables
Uhm, the second worked for me. Adding a
<pre>{{ dump(_context) }}</pre>makes this more readable.And yes, there are Twig_Markup objects for strings in there, but it is not worse than reading a render array. I can also use the $GLOBALS variant here, but I chose not to do so, because I then need to justify the usage of $GLOBALS as part of this original patch, which gets into other discussions and need to create my own twig_escape_filter function to not decrease performance even more. (Each function call / creation of Twig_Markup object costs ...)
That said if you only want all variable names, you can use:
3. Syntax
Done: I fixed it with easier syntax.
4. page.tpl.php
@todo: I will provide a cleaned up and QA'ed page.twig as GIST. What is currently in the sandbox is still WIP.
Themes checking visibility
To solve this I need the following issues in:
* #1815250: Type twig variables in PHP and remove superfluous render_var() and getAttribute() calls from the generated code
* Create a render in-memory cache for render arrays, so that they are not rendered twice
Then I can compile time optimize that render arrays are always run through twig_render() - even if they are not printed, which can then reduce the code to:
This would be compiled roughly to:
so having render store the rendered output somewhere is a must for performance reasons.
I might be able to optimize this also to:
But I am still pondering the consequences and while for the former, I have the code ready, I have not for the latter though as we compile it and have all information, it is _possible_.
I however would have loved to keep this patch rather simple ... (uh, yes ...) and do some things as followups.
Security
Well, I did not want to open the can of worms for the $GLOBALS discussion, so #1 from "A secure theme system" was not an option for something _simple_.
Ideally we want template authors to only write:
node.body or node.field_my_nice_field and not having to deal with the other things at all. I can think both of a decorator (performance problem) or a getter called token, which works similar to how tokens work.
node.token.body (would call node->token('body')), but that is a separate discussion.
I can for sure leave auto escape out. That is one git revert away, but Dries wanted to see it in so ...
Big Picture
So we move "Performance" and "Security" out of Benefits to "Potential Benefits"?
However I agree: Yes, benefits could be stated more realistically based on the current situation.
Other issues
* NULL and FALSE fixed
*
Either Drupal commits to Twig or not. But if there is hesitation, I am happy to do:
Thats three lines of code added to twig_render, but I did not do it for performance reasons ...
* @todo removed from node.twig
* datetime.twig:
That code is a little strange now (and yes I agree that here the variables mark safe leads to a strange situation, but I think this is an edge case as escaping within templates should not be that often:
is a universal version that works always with all autoescape and non-autoescape enabled.
I see that this is a little problem right now.
Actionable Items?
Besides the cleanup, which I did now (and is attached, also re-rolled) and the @todo to provide a cleaned up page.twig, is there anything actionable I can currently do to get this in?
Or is it just sit and wait?
Comment #167
fabianx commentedIt helps to actually add the patch ;-)
Comment #169
fabianx commentedThis one test really does not like the addition of more classes, but this cleanup would apply both to node.tpl.php and node.twig, so leaving as straight conversion from node.tpl.php for now.
Comment #170
KrisBulman commentedtestbot go!
Comment #171
podarok#16 looks good
and bot is happy
Comment #172
webchickUh, let's at least let David respond to fabianx's review. :)
Comment #173
moshe weitzman commentedI need to review the autoescape stuff more closely. I will say that without security improvements, this patch does not justify its code complexity and code weight in my mind. Slightly cleaner templates and better support for an edge case (i.e. Template author must be forcibly prevented from using PHP, or at least forced until he moves to preprocess layer) are nice, but not enough to change add a complex (code-wise) theme engine IMO. I'm not sure this improves 'Increases learning curve' tag either. Again, security improvements might tip the scales for me.
I also want to take a pass at the Issue Summary in order to accurately reflect where we at. And thanks to everyone who continues to plug away at this.
Comment #174
chx commentedMoshe, people hate current theming and love Twig. Code complexity, meh.
Comment #175
chx commentedalso: will say that without security improvements, this patch does not justify its code complexity and code weight in my mind.
what? autoescape is on. strings in variables from preprocess are deemed safe but we already made that presumption but it's codified now.
Comment #176
catchReading through the patch, I don't have major complaints. Supporting show()/hide() etc. has obviously been tricky, but a lot of work has gone into supporting it and the eventual template syntax looks great.
A few nitpicks, we're also discussing the patch in irc at the moment.
This file_exists() could also be adding some overhead given most of those calls will miss - did that come up in performance testing at all? Either way it'll be gone by the time we get to release.
I can live with the coupling here given how performance-critical this is. We added the Attribute class for Twig integration in the first place.
Why $cache0 and not $cache?
Even though it's a straight copy, I'd be happy if we could Drupalize the code style a bit more.
This isn't one line. Also we know it's a class ;)
trigger_error() should handle that no?
This is weird syntax indeed. I agree it'd be better to do it (Edit: whitespace removal) in Attributes() if that's doable.
Comment #177
chx commentedPlease disregard #173. It's crystal clear now that Moshe have not even looked at the patch for quite some iterations now.
Comment #178
effulgentsia commented<article id="node-{{ node.nid }}" class="{{ attributes.class }} clearfix" {{- attributes }}>To clarify, the above is identical to:
<article id="node-{{ node.nid }}" class="{{ attributes.class }} clearfix"{{ attributes }}>The second form matches what all of our templates currently do in HEAD (with
<?phpbumping right up to the closing quote), so maybe we should revert to that form in this issue, and have a follow up for using{{-The reason we need one of the two forms is that if there are no attributes other than ones already individually printed, then {{attributes}} might be an empty string, and themers don't want to see markup with an extra space before the close of tag (
<article id="node-1" class="node clearfix" >). However, we can discuss that trade off in a follow up as well.Comment #179
Crell commentedThe sense I get on the security front is that Twig == Security iff we let Twig be responsible for security. Right now we're still doing a mix of old-style and twig-style, because conversion is not complete. To really get the full benefit of Twig security-wise we need to finish converting templates to Twig, then let Twig handle escaping rather than trying to second-guess it the way we needed to second-guess PHPTemplate. catch already pointed to #1825952: Turn on twig autoescape by default on that front.
Even then, as chx has highlighted we still block people from putting db_query() directly into a template (win), sane and secure handling of arrays (win), etc. So maybe it's not a massive security win (yet), but it's still an improvement on status-quo.
Plus, front-enders are very loud in preferring Twig to PHPTemplate. So even if performance is a wash, and security is a wash, making life easier for front-enders is worth the effort. We can continue to iterate to make it more "Twig-ish" and let Twig handle more things its good at, which should help with simplification.
The people to decide if this is "enough" to commit and move forward are the leading front-end developers in core, not the leading back-end developers in core. (Meaning, I'm not going to RTBC this; JohnAlbin, Morten, Jacine, etc. should have that authority, not me.)
Comment #180
effulgentsia commentedI finally got around to reviewing the code internals in detail here, and like what I saw. I'll save a nit review for later, but none of the little things I came across couldn't be dealt with in a follow up, so I'd be +1 for committing this once catch's feedback is addressed and there's consensus on whether to revert the auto-escaping stuff from this issue and deal with it more fully in a follow up. If we do defer auto-escaping to a follow up, per #164, then I still appreciate the work that went in to implementing what's here so far, because seeing how we're able to hook in to Twig compilation, gives me confidence that once we figure out the complete picture of what we want, that we will be able to implement it. If we don't want to revert the auto escaping that's here so far, I'd be fine with this being committed as the initial approach, and then iterating on it in follow ups.
My biggest disappointment currently is seeing the benchmarks. I really expected to see a performance gain relative to PHPTemplate in situations where the same template is rendered many times per request (e.g., 100 node teasers), and I'm confused as to why that's not turning up. I agree with #160 that the performance regressions can be clawed back, but without a performance gain, I'm concerned we'll be left with at least a dozen theme functions that for performance reasons we won't be able to convert to templates. I hope that's not the case, and that we'll find ways of further micro optimizing the compiled templates such that even extremely frequently called theme functions can be templatized.
I disagree with this. Even if all we did was convert templates from PHPTemplate to Twig, failed to make any improvement to security, and failed to optimize compilation to the point of converting theme functions, I still think it's a net positive (though not nearly as positive as if we do manage to get the security right and convert all our theme functions). What you call "Slightly cleaner templates", webchick in #111 calls "absolutely huge". What you call an "edge case", I call enabling full control of markup in a multi-tenant SaaS environment, which I consider huge to the adoption of Drupal (disclosure: I work for Acquia, the company behind Drupal Gardens). As to the complexity in this patch, it's internal to a half dozen classes in Drupal\Core\Template, and IMO, not any more cumbersome than any of our other low-level systems: IMO, well worth it if it makes things even a little bit friendlier to front-end devs and Drupal SaaS providers.
page.tpl.php calling theme() is a bug: a hold over from D7's incomplete conversion to render arrays. It's the only core template that does so, and it should be calling render() instead. There might be an issue somewhere for fixing that, but it might be made moot by #1696302: [meta] Blocks/Layouts everywhere. Similarly, the level of logic complexity in it will go away once everything's converted to blocks. forum-list.tpl.php is probably the next best example of a logically complex template. Almost everything else in core is not much more complex than node.tpl.php.
The answer to this in #166 works for me, and demonstrates a key benefit of Twig: decoupling object/array/string/boolean internals from template syntax.
Is that a better variable or is it a hold over of PHP string mentality? With Twig, we can make node.body.und.0.value into an object with methods for rendering raw or safe that hook into Twig's filter syntax.
Comment #181
David_Rothstein commentedclearfix"{{ attributes }}(no in-between space) to deal with the whitespace, since it avoids another new syntax and is consistent with what's already done. (Seeing the technique change in this patch made me think additional whitespace was being introduced somewhere in the pipeline and that the new technique was required to get rid of it).<pre>{{ dump(_context) }}</pre>I got this displayed on the screen:array 'nid' => object(Twig_Markup)[159] protected 'content' => string '1' (length=1) protected 'charset' => string 'UTF-8' (length=5) 'vid' => object(Twig_Markup)[161] protected 'content' => string '1' (length=1) protected 'charset' => string 'UTF-8' (length=5) ... (etc)(edit: fixed formatting)
So that's why I assumed auto-escape was coming into play here. Do other people see something else more readable there?
I'd say "by clearing caches" rather than "via drush cc all".
Hm, I think the best variable is probably
{{ content.body.0 }}(and same goes for the equivalent in PHP template). But for people who are choosing the best variables, Drupal's theme system is already secure :)If we got rid of strings and made it a "rule" (whether enforced or not) that you should never call the raw method in a template file this could work. But Drupal has so many deep data structures; it is hard to imagine this happening everywhere.
Overall, I have no strong opinion on whether the auto-escaping/improved security should be a followup (as opposed to a core requirement of the decision to move to Twig).... but I'm more concerned about doing it wrong and then Drupal 8 gets released with something that might not quite make sense. Leaving auto-escape off for now, until we know we get it right, is certainly the more conservative choice.
Comment #182
cosmicdreams commentedHello all,
Even though the summary underlines this point, it's worth repeating: Using Twig instead of PHPTemplate allows Large-scale Drupal projects to bring on non-Drupal specialist Front end developers to handle a large portion of theming tasks. I work at a place were we have devs that work with a number of other frameworks and a growing number of them are using Twig.
My hope is that with using Twig I'll have an easier time in the future ramping up themers onto projects. For many that I've spoken to about Drupal 8 (outside of the Drupal community) being able to use Twig's syntax is improvement they are most excited about. These folks are are Frontend developers, Expression Engine devs, Zend Framework PHP devs, and Ruby on Rails devs.
As a side effect of using Twig as a theming engine, we'd have an opportunity to tackle redundancy in our theming functions and take a stab at improving the overal developer experience.
Comment #183
David_Rothstein commentedI would say (especially for the purposes of this issue) let's worry more about building the highest quality theme system we can, whatever it is. If it's good, people will learn it. Also, I'm pretty sure I can find you a whole bunch of Wordpress and Joomla themers who are already very happy theming directly in PHP :)
I sort of had a eureka moment earlier, though, when I realized we're not even deciding between PHP and Twig anymore. In Drupal 8, what we're using in tpl.php files isn't even normal PHP. For example, this snippet in node.tpl.php:
To a person with normal PHP experience that looks like complete nonsense which is likely to result in the word "Array" being printed to the page at the end of the string. (The only reason it doesn't is that we have some magic behind the scenes to deal with it. And to support that magic we already have lots of complex machinery, even without Twig.)
In short, Drupal 8 may have broken PHPTemplate enough already that there's no way to save it :(
Whether that helps answer the question of "do we need to demonstrate security benefits in order to go with Twig" I'm not sure, but I thought it was relevant.
Comment #184
David_Rothstein commentedHm, it looks like someone who was trying to be helpful edited point 2 from my comment in #181 to "fix the formatting". Thank you, but unfortunately the "fix" completely changed the meaning of what I was trying to say :)
So, let me repeat that with a little more clarity.
When I tried
<pre>{{ dump(_context) }}</pre>I expected to see something like this displayed on the screen:array 'nid' => object(Twig_Markup)[159] protected 'content' => string '1' (length=1) protected 'charset' => string 'UTF-8' (length=5) 'vid' => object(Twig_Markup)[161] protected 'content' => string '1' (length=1) protected 'charset' => string 'UTF-8' (length=5) ... (etc)But what I actually saw was this:
Given that, I assumed (but did not verify) that this is some kind of auto-escaping issue.
Did anyone else reproduce this, or am I doing something wrong? Obviously, for debugging to be meaningful, we need it to work like the first, not the second...
Comment #185
fabianx commentedThere is a lot to answer for me here, thanks!
Lets start:
#184:
That is an edge case as xdebug is outputting HTML, while normal PHP returns plain text. That issue is clearer now.
Please use:
when using XDebug. This is here also not Drupal specific, but a Twig upstream problem / bug / limitation.
#181:
1. Will be fixed in next patch.
4. Good catch :) - Will be fixed.
5. I'll leave Dries to decide if he wants auto escape on or off as part of the first patch and provide a patch to revert auto-escape just if someone wants to work with that.
I definitely want auto escape and want it properly supported, but can't do it all in one patch ...
We'll surely find a nice way to deal with safe_value vs. value, which seems to be the strongest DX problem right now.
Thanks again for the great feedback.
Comment #186
fabianx commented#183: Yes, but almost anyone agreed (at least from what I could see) that using the new Attribute was way simpler than the classes_array[] mix.
#180:
Performance
Please be aware that node.tpl.php is a rather complicated template, because it needs to call render, show, hide, etc. and such is more of an upper bound for the overhead. The overhead for something like theme_field should be much lower, because there the compiled result should match the theme_field much closer. And if not out of the box, it is just one compilation switch away :-). That is the beauty of a compiled language: You can optimize things on a case per case basis. So I am pretty sure we can get all theme functions converted and to good performance.
And thanks for the encouraging feedback!
#178: After internal discussion and an ACK by @jenlampton and @JohnAlbin we decided to remove the whitespace control for now and do any change within Attribute() and whitespace as followup:
Its now:
<article id="node-{{ node.nid }}" class="{{ attributes.class }} clearfix"{{ attributes }}>#176: Thanks for a great review!
Here are the points:
This file_exists is called during theme registry rebuild only, so it is obviously not part of any performance testing and as it will be gone, it should be fine for now.
Great!
$cache is already taken by the parent class. I changed it to $cache_object as a compromise and druplified. I also changed $cache to $cache_filename to make it clearer.
I also changed the whole copied code to be adjusted to our coding standards. (as good as I can; if someone wants to check the interdiff, feel free ...)
DONE, see interdiff.
FIXED
Uhm, I tested this out and got:
which I found much more confusing than:
content2 could not be found in _context in "core/modules/node/templates/node.twig" at line 101.Also I don't think its necessary to expose the internals to the Themer ...
So unless using drupal_set_message directly is a huge problem, I'd like to leave the more user friendly version in.
Changed in this patch for now and changing white space in Attributes() is potential follow-up.
===
Thats all.
New patch attached! This is exciting, looks good and I hope we can get this back to RTBC and then committed soon :-).
@Dries: Please let me know if you would like the first patch with auto escape disabled. I can easily provide a patch without it and we could also do auto escape as follow up. Thanks!
Comment #187
fabianx commentedBack to Needs Review :-)
Comment #188
fabianx commentedOkay, according to feedback from several people including David_Rothstein, I'll defer the autoescape for now and have re-opened and re-titled #1712444: Twig: Activate autoescape mode.
We can definitely do this and auto escape will work great, but we can then talk about it more independently from this patch :-).
This also saves performance for now, which is good.
Comment #189
David_Rothstein commentedLooking at the interdiff that seems pretty reasonable to me. Haven't really reviewed beyond that, but assuming other people have I think this is fine.
Regarding the dump() calls not working, good call on xdebug being the culprit (haven't tested but I'm almost certain that's the case). I actually seem to remember
{{ dump(_context)|raw }}didn't work for me either... but in any case, I agree this sounds like an upstream problem for Twig to fix (though kind of an unfortunate one since a lot of developers will have xdebug turned on).Comment #190
catchI just went through this with FabianX in irc
I didn't understand why we need to store the mtime in the cache system then fetch it again for each template, which at least with a lot of templates would add a lot of overhead.
Summary:
- this is only supposed to happen when autoReload mode is on (which it is by default in core at the moment, but I think we should default to off and let people rm -f the templates directory tbh). the cache()->get() was happening whether it was enabled or not though (i.e. before the check), but Fabian's fixing this now
- php storage doesn't provide an mtime to compare against, hence using the cache system. I don't care about this bit so much if we're not doing any check at all in runtime.
- php_storage doesn't have any kind of gc mechanism, but we need that for the compiled container - that should be a critical task (or even bug) somewhere but it'll cover both Twig compilation and DIC compilation (and the latter is already in core) so this patch is the victim of that, not the perpetrator.
Comment #191
fabianx commented#190:
auto_reload is FALSE by default now. To update the compiled templates, for now you need to push the clear all caches button. The code has been refactored for performance. I added
drupal_php_storage('twig')->deleteAll();to drupal_flush_all_caches() like with DIC.DONE, will only be enabled in development mode.
Agree.
---
This patch also adresses the trigger_error vs. drupal_set_message issue and favors trigger_error until the prod vs. dev switch is in, so production sites are not inhibited.
Patch and interdiff attached!
I am getting excited here ... :-)
Comment #192
catchThat looks much better and should reduce some of the performance overhead further as well.
I definitely think we should handle auto-escape in a follow-up - it needs to be based around the template conversions and thinning out variables from preprocess etc., also removing it here means there's no longer any bleeding of Twig_Markup into Attributes().
Comment #193
catchI can't find anything else to complain about.
I'm sure a tonne of issues are going to come up during conversions etc. but this is pretty self-contained and allows the conversions to proceed.
The overhead added here is significantly less than a lot of other patches and there's a reasonably good plan for clawing it back (although it seems very unlikely it'll end up much better than PHPTemplate and that's with the compilation of templates, which is a shame). Where we'll need to be very careful is in converting theme functions (like theme_field()) which are currently theme functions because .tpl.php files are too expensive, would be great if .twig aren't by the time we get there.
Moving back to RTBC.
Comment #194
chx commentedI wonder whether Dries would commit this during his speech at BADcamp...
Comment #196
fabianx commented#191: core-Integrate-twig-into-core-1696786-191.diff queued for re-testing.
Comment #198
webflo commented#191: core-Integrate-twig-into-core-1696786-191.diff queued for re-testing.
Comment #200
webchick#191: core-Integrate-twig-into-core-1696786-191.diff queued for re-testing.
Comment #201
chx commentedBack to RTBC after bot problems.
Comment #202
schnitzel commentedcommit photo! :)
Comment #203
webchickYEEAAAHHHH!!! :D :D :D
Comment #204
chx commentedWell, my friends, how do we write a change notice for this?
Comment #205
jhodgdonI am taking on the twig change notice, will report back shortly
Comment #206
jhodgdonI've created at least the start of a change notice:
http://drupal.org/node/1831138
Comment #207
podarok#206 the code from example not yet in core repo
it is commited only in sandbox repo
possibly we should postpone this change notification untill code merge with sandbox or write another examples
Comment #208
jhodgdonRE #207 - I do not think that is all that important whether this particular example was in the patch or not. What I wanted was a quick example of the process -- the main point of the change record is to say "there is now a new process for theming that is different for what was in 7.x".
If you think it's a bad illustration of the process, or there is an error in the example, please edit the change notice and put in a better example. I was not involved in this patch/issue, was just asked to write the change notice, and if a better example would make the change notice more accurate or clearer, by all means put one in. Thanks!
Comment #209
chx commentedThanks a ton, I copied datetime over and linked node.tpl.php and node.twig.
Comment #210
jwilson3The change notice refers to files as *.html.twig but all the twig files in core right now end in just "*.twig".UPDATE: oops Chx already fixed this!I'll admit I don't know what the final solution is going to be -- the front-end twig work is using *.html.twig, but the backend engine development has been using *.twig. Which one are we going with?
Comment #211
johnalbinThat's already been decided. We're going to use *.html.twig. See #1804698: Consider using *.html.twig filename convention
Comment #212
fabianx commentedYes, the reason this patch does not include this is that this needs another bugfix to a core function to find such files. We have this fix in front-end, but I wanted to avoid to re-start the whole review process on an already complex patch, because of one added line.
We can file an issue now. :-).
Also: WOHOO!
Comment #213
jenlampton/me highfives FabianX
Comment #215
xjmComment #215.0
xjmUpdated issue summary. Add performance links
Comment #216
bhagath commentedCall to undefined method Drupal\Core\Template\TwigExtension::setLinkGenerator() This is the error i was found in my site..
Comment #217
geerlingguy commented@bhagath - I think this is related to some other problem; you might want to search the issue queue or file a new issue if you can't find any other issues matching your problem.
Comment #218
quietone commentedPublished the CR