Reading through the following code was the only way I could see to learn about theme engines.
http://drupalcode.org/project/drupal.git/blob/refs/heads/8.x:/core/theme...
https://github.com/damz/mustache-drupal/blob/master/mustache.engine
http://drupalcode.org/project/drupal.git/blob/refs/heads/8.x:/core/inclu...
From what I see, a theme engine requires implementing two hooks, ENGINE_init() and ENGINE_theme(). They are documented as hook_init() and hook_theme, and mentioned on the docs for theme().
But ENGINE_extension and ENGINE_render_template also exist, aren't available to themes or modules, and aren't documented at all.
Comment | File | Size | Author |
---|---|---|---|
#27 | interdiff.txt | 3.21 KB | star-szr |
#27 | 1332068-27.patch | 2.73 KB | star-szr |
#21 | interdiff.txt | 1.68 KB | star-szr |
#21 | 1332068-21.patch | 2.86 KB | star-szr |
#15 | interdiff.txt | 1.96 KB | star-szr |
Comments
Comment #1
tim.plunkettSince we had to add a new implementation for Twig, maybe someone from the Twig team can help out.
Comment #2
star-szrThank you @tim.plunkett! This has been on my list of issues to create.
Comment #3
star-szrAdding docs tag.
Comment #4
pixelmord CreditAttribution: pixelmord commentedComment #5
pixelmord CreditAttribution: pixelmord commentedAttached a patch that adds documentation for those hooks into system.api.php
Please review
Comment #6
star-szrThank you @pixelmord! Here's an initial review:
All documentation needs to be complete sentences (usually ending in a period) per https://drupal.org/node/1354#general.
Need one more space of indent for the @return description (https://drupal.org/node/1354#return) and the description should be a complete sentence (ending in a period) per https://drupal.org/node/1354#general.
Let's please not duplicate twig_extension() and twig_render_template() for these docs. It might actually make sense to copy these from PHPTemplate instead if we're going to copy+paste :)
To make a new paragraph add a blank line (https://drupal.org/node/1354#general) and the first line is not a complete sentence. The phrasing "Here the theme engine" is a bit strange, maybe we can rephrase this line a bit? Here's a shot:
This hook is used in a theme engine implementation in the format of ENGINE_render_template(). It takes in an array of variables and returns a string containing the rendered template.
Can we specify whether or not the extension should be included? I think that would be helpful. Also this needs a @param type per https://drupal.org/node/1354#param.
The variables won't necessarily be included in the output. I think we should just say that the variables will be passed to the template. This should also document the @param type (array).
Comment #7
pixelmord CreditAttribution: pixelmord commented@Cottser: thanks for your review and reminding me of those things. DOT ;)
Attached is another patch with updated doc.
I disagree with you on the point to use the PHPtemplate implementations as an example, because even if the implementation for Twig might still be changed in the future, they are the better example, because here a 'real' template engine is used and if anybody wants to use this documentation for adding some other template engine, the code will be much more similar. See the linked sandbox with the mustache engine implementation.
Also it makes much more sense to me to find documentation to something that is actually used ;)
Comment #8
pixelmord CreditAttribution: pixelmord commentedUpdate status
Comment #9
star-szrOk, I'm fine with including Twig as an example but can we please take out the twig_debug stuff? That makes it really big. It could be one line of code I think without twig_debug.
/me wants to create an issue to pull out all that twig_debug logic into at least a separate function…
Please link up this mustache sandbox! And interdiffs are everyone's friend when updating patches :)
Comment #10
pixelmord CreditAttribution: pixelmord commentedOk here's the interdiff for convenience for the two previous patches 5-7.
@Cottser: the link to the mustache code is in the summary.
Comment #11
pixelmord CreditAttribution: pixelmord commentedHere is a new patch with interdiff that uses simple code as a twig example
Comment #14
star-szrFairly easy reroll, so here it is. I found some more minor changes that I will roll in but I think this is looking pretty good now. Thanks @pixelmord (and thanks for bumping @anavarre), I should have come back to this sooner.
Comment #15
star-szrAttached patch incorporates these fairly minor changes. This is ready to go in my opinion.
Missing period.
Missing $ in front of variable name.
s/drupal/Drupal/
I just reworded this, I found it a bit hard to read.
The line after @return needs to be indented by one more space. But actually I don't think these docs are necessary, the docs in hook_extension() can be referred to instead. I removed these lines.
Comment #16
joelpittetTo go, it is!
Comment #17
alexpottComment #18
jhodgdonI have a few concerns with this patch:
a) When declaring hooks, we normally want verbs like "Declare" not "Declares". See
https://www.drupal.org/node/1354#hooks
b)
The second sentence of the description paragraph is not necessary, since the @param and @return sections give you this information. Please omit. Also I think it's a bit misleading the way it's worded; the @param/@return are better IMO.
c)
Um... "should be"??!? You are describing a hook here. You need to tell hook implementers what they can count on being passed in, not what they *should* expect. Also I think maybe these two sentences can be combined?
Looks good other than that!
Comment #19
jhodgdonComment #20
star-szrThanks for the review @jhodgdon, I will take a look at those points.
Comment #21
star-szrNew patch incorporating feedback from #18.
Comment #22
jhodgdonOh sorry, one more thing: Those hooks should be in theme.api.php I think, not system.api.php? All the hooks related to the Theme system are normally put into theme.api.php.
The rest looks great, thanks!
Comment #23
star-szrOh I definitely should have picked up on that when reviewing.
Comment #24
star-szrActually I think upon closer inspection that either these belong in system.api.php, or (some of?) the following hooks should maybe be moved to theme.api.php as well:
hook_theme
hook_theme_registry_alter
hook_template_preprocess_default_variables_alter
Or maybe there is some special consideration needed because these are purely "theme engine" hooks and not valid for use by modules or themes.
Comment #25
jhodgdonThey all belong in theme.api.php. See #2307859: Move theme/render hooks from system.api.php to theme.api.php -- which needs review -- for the existing hooks that are in the wrong place.
Comment #26
star-szrExcellent, thanks for that, I wasn't aware of the other issue. I will review that and update the patch here.
Comment #27
star-szrMoved the hook docs to theme.api.php. No other changes.
Comment #28
joelpittetBack to RTBC, thank you @Cottser and @jhodgdon
Comment #29
alexpottCommitted be15db1 and pushed to 8.0.x. Thanks!