Problem/Motivation
In template_preprocess_html() we call out to Safemarkup::format() 4 times on every request.
This function itself is not slow, but why do we use it, when we know that $type is a basic string and $token is fine as well.
Proposed resolution
Use Safestring
Remaining tasks
Discuss whether its worth to microopt. this.
User interface changes
API changes
Data model changes
| Comment | File | Size | Author |
|---|---|---|---|
| #18 | 2538950.18.patch | 5.31 KB | alexpott |
| #18 | 16-18-interdiff.txt | 4.58 KB | alexpott |
| #16 | 2538950.15.patch | 5.64 KB | alexpott |
| #16 | 14-15-interdiff.txt | 2.72 KB | alexpott |
| #14 | 2538950.14.patch | 3.4 KB | alexpott |
Comments
Comment #1
dawehnerDiff%
Diff
(microsec)
Diff%
MemUse
Diff
(bytes)
Diff%
PeakMemUse
Diff
(bytes)
Diff%
Comment #2
fabianx commentedWhat is wrong to use SafeString::create() - more effort?
Otherwise +1, SafeString did not exist at that time of writing this.
Comment #3
dawehnerWell, I think its just odd as an API when you know the string already being a string.
Comment #4
fabianx commentedOkay, agree.
Comment #5
alexpottI'm not yay about adding a constructor but at the very least we should enforce when it should be used. An exception should be thrown if $string is not a string and when it is empty. Also the docs need improving to say when to use the constructor and when to use create.
Comment #6
dawehnerYeah let's be clear, this change was not needed.
Comment #7
stefan.r commentedThis looks good now.
Nit: are
Comment #8
alexpottI think we need to decide on #2544684: Expand @internal documentation on SafeString and SafeStringInterface and introduce ViewsRenderPipelineSafeString first.
Comment #9
stefan.r commentedLet's have @alexpott have another look at this
Comment #11
stefan.r commentedreroll
Comment #12
alexpottI think a completely different and maybe more honest approach is available. Let's just print the raw variables in the twig template and attach everything to the html render. 4 less calls to SafeMarkup::format and 4 less calls to render.
Comment #13
stefan.r commentedAre we sure the data we receive in the template comes from template_preprocess_html(), will this still be OK if a theme has its own preprocess implementation?
Comment #14
alexpottWe can even do this which makes it way more obvious what is going on in the template.
Comment #15
stefan.r commentedThat seems clearer, so realistically themes would never touch that anyway
Comment #16
alexpottFixing the template docs. I still think we should print the raw token - if a developer changes the token and adds raw html imo that is just silly and we shouldn't use auto-escape to prevent that. What auto-escape should be for is ensuring user generated input is escaped when printed in templates.
Comment #17
wim leersWow, this sure is a very interesting approach!
I have a few concerns about going in this direction:
Yet both of these concerns are arguably good things: both help ensure themers don't accidentally break CSS or JS loading. In that sense, it even clarifies things: because
{{ styles }}and{{ scripts }}and{{ scripts_bottom }}make themers think they are printing CSS/JS, but they're not: they're printing placeholders.But in that case, I'd propose some renaming to make it at least less cryptic:
<css-attachments-placeholder token="{{ attachments_token|raw }}">So: "head", "css", "js" and "js-bottom" is what I would propose.
And renaming the "token" variable to "attachments_token".
Comment #18
alexpottAfter discussing with @Wim Leers a bit more on IRC we went for the following approach...
Comment #19
wim leersLooks good! Let's see what testbot says.
One important remark: every D8 theme that has its own
html.html.twigtemplate will need to be updated.Comment #20
star-szrFancy! No objections from me. Just want to confirm this docs removal is because something is being removed (or is just out of date/wrong/etc.):
Comment #21
wim leersGreat! With front-end/themer sign-off, I think this is RTBC.
#20: That
cssvariable is indeed out of date/wrong:$variables['css']is not being set anywhere.Comment #22
alexpottComment #23
catchCommitted/pushed to 8.0.x, thanks!
Comment #25
catchPosthumous priority change.
Comment #26
alexpottAdded https://www.drupal.org/node/2565021
Comment #27
wim leersFiled a follow-up: #2565033: Follow-up for #2538950: simplify syntax.
Comment #30
Jeff Burnz commentedInitially this worked in my theme, however after a recent update (no idea which one) all I get now is this:
<head-placeholder token="GyB5DIbKTxnBAR1qiGzevTSOCsi4EQ9dLK3mMCYAANc6HDRx1EA4MMiqsw7Y9_f7KKFOo8Xa5A" />...so no styles, js etc is output, only what you see above. Anyone have an insight into why this might be?Ok, so this is patently obvious now I have looked at this again - for some reason I have treated these like self closing html tags and used a forward slash, this caused them to fail - they should look like this:
<head-placeholder token="{{ placeholder_token|raw }}">Not...
<head-placeholder token="{{ placeholder_token|raw }}" />edit - I updated this in case any one else made the same silly mistake. Apologies for the noise.
Comment #31
joelpittetWe try to avoid |raw to discourage it's use. Unfortunately this got in to RC release. Any chance we can wrap this placeholder in a Markup/SafeString type object?
I just noticed this when trying to get basic theme upgraded to D8
Comment #32
joelpittetMade a follow-up to get those out. #2603074: Remove |raw from use in core templates