For the image template, every variable is actually an attribute: src, title, alt, class, width, height, etc. Do any of these attributes need special treatment? If we treat them all as standard attributes our image template file could end up looking like this:

<img {{ attributes }} />

Is that alright? Or do we want to pull out src and treat it specially? any others we should worry about?

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

JohnAlbin’s picture

I definitely don't think we need to treat src specially. The reason's to tweak that are so edge-case that we should just let them figure out how to pull it out using twig syntax themselves.

And I can't really think of any other attributes we'd need.

jenlampton’s picture

Alright :) great!

So then the TODO here is to merge all the current vars for this template file into attributes before it gets to the template, then we can just print them all out as attributes.

FYI - just so I'm not committing something totally broken, the code for this template looks like this right now:

<img src="{{ uri }}" {{ attributes }} />
jenlampton’s picture

Title: image template: decide if any img attributes should get special treatment » image template: move all current template variables into attributes

updating the title :)

davidneedham’s picture

It seems very unusual, but I also agree that the reasons to tweak are few and far between.

jenlampton’s picture

Project: Drupal 8 with twig - abandoned sandbox »
Component: Code » Twig templates
Status: Active » Fixed

As a follow up here, we did decide to treat class specially, so the template looks like this:

<img class="{{ attributes.class }}" {{ attributes }} />

Also. moving this to the active twig sandbox.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

steveoliver’s picture

Status: Closed (fixed) » Needs review
FileSize
3.77 KB

The image template still needs some love. Patch attached.

steveoliver’s picture

+++ b/core/includes/theme.inc
@@ -1785,19 +1785,17 @@ function theme_image($variables) {
 /**
- * Preprocess variables for theme-image template.
+ * Preprocess variables for theme('image').
  */

Still wondering about this format. ... for theme('whatever') seems to make the most sense to me.

steveoliver’s picture

Title: image template: move all current template variables into attributes » theme('image')
FileSize
3.77 KB

Hi-jacking this thread because there doesn't seem to be one about theme('image') and what else is there but a bunch of attributes. Can I get an RTBC? ;)

Also, comment about preprocess docblock titles is at #1825464: [meta] Documentation blocks for preprocess functions.

podarok’s picture

Status: Needs review » Needs work
+++ b/core/themes/stark/templates/theme.inc/image.html.twigundefined
@@ -4,31 +4,27 @@
  */
 #}
+<img class="{{ attributes.class }}" {{ attributes }} />

*/
#}
looks like this image has no src to image.extension itself

Fabianx’s picture

-
+  $variables['attributes'] = new Attribute($variables['attributes'] ?: array());

The ?: might seem clever here, but achieves nothing as:

* this will create a notice if attributes is not set
* this will resolve to array() if $variables['attributes'] is null or empty.

Such this statement is equivalent to:

$variables['attributes'] = new Attribute($variables['attributes']);

What you want is the long form of:

isset($x)?$x:array()

Thanks!

steveoliver’s picture

Status: Needs work » Needs review
FileSize
3.83 KB

1.

$variables['attributes'] = new Attribute(!empty($variables['attributes']) ? $variables['attributes'] : array();

2. src is an attribute, so I put it in attributes.src, which would have printed with this:

<img class="{{ attributes.class }}" {{ attributes }} />

...but is a little easier to look at this way, I suppose:

<img src="{{ attributes.src }}" class="{{ attributes.class }}" {{ attributes }} />
podarok’s picture

Status: Needs review » Needs work

#12 commited / pushed to front-end
thanks!

looks like we need follow-up here for TODO in template

joelpittet’s picture

Component: Twig templates » Twig templates conversion (front-end branch)
Status: Needs work » Fixed

Looks as though the TODO has been fixed in front-end - marking as fixed

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.