we'll need to create a new function for twig that will allow us to not print stuff in the templates. In PHPTemplate we do this by unsetting something from the renderable. We'll need to create a new best practice for achieving the same end with twig.

Comments

jenlampton’s picture

Status: Active » Fixed

This is resolved, we added the unset() function as both a function and a filter in the twig engine.

jenlampton’s picture

Project: Drupal 8 with twig - abandoned sandbox »
Component: Code » Twig engine

Even though it's fixed - still moving to the other sandbox.

Status: Fixed » Closed (fixed)

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

c4rl’s picture

Can you provide sample code or documentation?

fabianx’s picture

unset is not needed anymore as hide() can be used on render arrays.

c4rl’s picture

Okay, would unset() still exist?

It seems to have some applicability for people who want control over attributes via the templates. That is, novice themers may be more comfortable doing this in a template than a preprocessor. Example:

{# @file foo.html.twig #}
{% unset attributes.class %}
<article{{ attributes }}>
...

Rather than

// @file mytheme/template.php
function mytheme_preprocess_foo(&$vars) {
  unset($vars['attributes']->class);
}

Would this be feasible?

c4rl’s picture

Tagging

fabianx’s picture

Yes, we might want to re-add unset as TAG like hide / show.

jenlampton’s picture

Status: Closed (fixed) » Active

Re opening, let's add unset back :)

c4rl’s picture

Title: Come up with a way to unset() in twig templates. » Provide unset() method in twig templates

Clarifying title

fabianx’s picture

Assigned: Unassigned » fabianx

Assigned to me

jwilson3’s picture

Correct me if wrong, but wouldnt the proper syntax for #6 be {% attributes.class|unset %} ?

Also, I've proposed something, along very similar lines, that might duplicate this issue: #1835396: Attribute modifier functions for themers

My proposal there includes the "remove" function, which would essentially do exactly what "unset" in this issue is proposing to do.

fabianx’s picture

#12:

Not really, either:

{% unset(attributes.class) %}

or

{{ attributes|unset('class') }}

But using a filter as part of a _statement_ makes no sense to me ...

jwilson3’s picture

Thanks Fabianx, that subtle point wasn't clear to me from my read of the twig docs here, where it states that filters should be used for Value transformation but doesn't really mention where it should be used. Your point about putting filters inside {{ }} as opposed to {% %} makes perfect sense tho.

fabianx’s picture

Component: Twig engine » Twig templates conversion (front-end branch)
diff --git a/core/lib/Drupal/Core/Template/TwigFactory.php b/core/lib/Drupal/Core/Template/TwigFactory.php
index ef1a40d..91dd369 100644
--- a/core/lib/Drupal/Core/Template/TwigFactory.php
+++ b/core/lib/Drupal/Core/Template/TwigFactory.php

@@ -67,6 +67,7 @@ public static function get() {
     $twig->addNodeVisitor(new TwigNodeVisitor());
     $twig->addTokenParser(new TwigFunctionTokenParser('hide'));
     $twig->addTokenParser(new TwigFunctionTokenParser('show'));
+    $twig->addTokenParser(new TwigFunctionTokenParser('unset')); // @todo needs to support multiple arguments
 
     // @todo Figure out what to do about debugging functions.
     // @see http://drupal.org/node/1804998
@@ -76,6 +77,8 @@ public static function get() {
       'hide' => 'twig_hide',
       'render' => 'twig_render',
       'show' => 'twig_show',
+      'unset' => 'twig_unset',
diff --git a/core/themes/engines/twig/twig.engine b/core/themes/engines/twig/twig.engine
index e7de4cf..dc6bc40 100644
--- a/core/themes/engines/twig/twig.engine
+++ b/core/themes/engines/twig/twig.engine

@@ -124,3 +131,14 @@ function twig_show($element) {
   }
   // @todo Add warning in else case
 }
+
+/**
+ * Wrapper around unset()
+ */
+function twig_unset($element, $key) {
+  if ($element instanceof TwigReference) {
+    $element = &$element->getReference();
+    unset($element[$key]);
+  }
+  // @todo Add warning in else case
+}

Syntax is:

{{ unset(array,key) }} for now.

Please create manually and apply.

fabianx’s picture

Component: Twig templates conversion (front-end branch) » Twig engine (twig_engine branch)
harshil.maradiya’s picture

Issue summary: View changes

Hello ,
Please correct me if i am wrong

We have to introduce unset method on twig template level so particular can hide element on frontend level .

Thanks

joelpittet’s picture

Status: Active » Closed (won't fix)

@harshil.maradiya we actually have a method for that. It replaced show/hide @see https://drupal.org/node/2114563

and the change record, https://www.drupal.org/node/2212845

harshil.maradiya’s picture

Thanks jowlpittet :)

Project: » Lost & found issues

This issue’s project has disappeared. Most likely, it was a sandbox project, which can be deleted by its maintainer. See the Lost & found issues project page for more details. (The missing project ID was 1750250)