<p>Wow Matt, awful question! You should probably have a moderator remove it so nobody else has to suffer.</p>

<p>In <code>SproutForms_HtmlDisplay</code>, change the <code>__call()</code> function's <code>echo</code>...</p>

<pre><code>public function __call($key, $args)
{
    if (isset($this-&gt;htmlFields[$key]))
    {
        echo $this-&gt;htmlFields[$key];
    }
}
</code></pre>

<p>...to a <code>return</code>...</p>

<pre><code>public function __call($key, $args)
{
    if (isset($this-&gt;htmlFields[$key]))
    {
        return $this-&gt;htmlFields[$key];
    }
}
</code></pre>

CC BY-SA 3.0
2014-07-15T23:52:24.550Z
We will be sure to remove this and change everything here on you in the next version of the plugin =)
Ben Parizek
CC BY-SA 3.0
2014-07-15T23:56:16.270Z
@Ben Once I finally accept and remove this Q&A, nobody will even know it happened anyway!
Matt Stein
CC BY-SA 3.0
2014-07-16T00:00:42.177Z
The odd thing is, I think this got introduced recently.  But the next version of the plugin is on the horizon (and that file will no longer exist), so I haven't dug in to try to figure out why.
Ben Parizek
CC BY-SA 3.0
2014-07-16T00:20:06.070Z
@Ben I extroduced it from this build, and now all's well! Happy to take one hopelessly trivial item off your list :)
Matt Stein
CC BY-SA 3.0
2014-07-15T23:38:53.503Z
2014-07-15T23:38:53.503Z
Matt Stein
<p>I'm confused over this simple problem of a conditional printing "1" instead of behaving normally, and I can't figure out why it's happening.</p>

<h2>Goal</h2>

<pre><code>&lt;p&gt;
    &lt;label for="contactFirstName" class="required"&gt;First Name*&lt;/label&gt;
    &lt;input name="contactFirstName" value="" class="required"&gt;
&lt;/p&gt;
</code></pre>

<h2>Attempt</h2>

<p>I've passed a <code>field</code> variable that contains the following (among other things)...</p>

<ul>
<li><code>handle</code> (string) => <code>"contactFirstName"</code></li>
<li><code>label</code> (string) => <code>"First Name"</code></li>
<li><code>required</code> (boolean) => <code>true</code></li>
</ul>

<p>...into a macro...</p>

<pre><code>{% macro labeledInput(field) %}
    &lt;p&gt;
        &lt;label for="{{ field.handle }}"{% if field.required %} class="required"{% endif %}&gt;
            {{ field.label }}{% if field.required %}*{% endif %}
        &lt;/label&gt;
        &lt;input name="{{ field.handle }}" value=""{% if field.required %} class="required"{% endif %}&gt;
    &lt;/p&gt;
{% endmacro %}
</code></pre>

<h2>Result/Mystery</h2>

<p>"Couldn't be easier!", we're all thinking. Except that the markup comes out like this:</p>

<pre><code>&lt;p&gt;
    &lt;label for="contactFirstName"1&gt;
        First Name1     
    &lt;/label&gt;
    &lt;input name="contactFirstName" value=""1&gt;
&lt;/p&gt;
</code></pre>

<p><strong>Potential Clues</strong></p>

<ul>
<li>If I add <code>{% if true == true %}foo{% endif %}</code> in the macro, I'll get <code>foo</code> and not a <code>1</code>.</li>
<li>When the value of <code>required</code> is <code>false</code>, nothing prints (as you'd expect).</li>
<li>It doesn't seem to matter whether I'm using a macro, an include, or whether I try the same thing in my parent template.</li>
<li>The <code>field</code> object is a Sprout Forms <code>SproutForms_HtmlDisplay</code>, if that ends up being significant.</li>
</ul>

<p><strong>How is it possible for <code>{% if field.required %}*{% endif %}</code> to print a <code>1</code>?</strong></p>

<hr>

<p><strong>{{ dump(field) }}</strong>:</p>

<pre><code>object(Craft\SproutForms_HtmlDisplay)#544 (1) {
  ["htmlFields":"Craft\SproutForms_HtmlDisplay":private] =&gt; array(10) {
    ["input"] =&gt; string(145) "
        &lt;input class="text nicetext fullwidth" type="text" id="contactFirstName" name="contactFirstName" value="" autocomplete="off" placeholder=""&gt;
    "
    ["handle"] =&gt; string(16) "contactFirstName"
    ["type"] =&gt; string(9) "plaintext"
    ["settings"] =&gt;
      array(4) {
        ["placeholder"] =&gt; string(0) ""
        ["maxLength"] =&gt; string(0) ""
        ["multiline"] =&gt; string(0) ""
        ["initialRows"] =&gt; string(1) "4"
      }
    ["validation"]=&gt;
      array(1) {
        [0] =&gt; string(8) "required"
      }
    ["required"] =&gt; bool(true)
    ["instructions"] =&gt; string(0) ""
    ["hint"] =&gt; string(0) ""
    ["label"] =&gt; string(10) "First Name"
    ["error"] =&gt; string(0) ""
  }
}
</code></pre>

<hr>

<p><strong>compiled template for macro</strong>:</p>

<p>

<pre><code>/* macros/forms */
class __TwigTemplate_6b53fb724811bc086c93653f7ad8519f9f9b0f194a80bfde00a078376bde6a36 extends Craft\BaseTemplate
{
    public function __construct(Twig_Environment $env)
    {
        parent::__construct($env);

        $this-&gt;parent = false;

        $this-&gt;blocks = array(
        );
    }

    protected function doDisplay(array $context, array $blocks = array())
    {
    }

    // line 1
    public function getlabeledInput($_field = null)
    {
        $context = $this-&gt;env-&gt;mergeGlobals(array(
            "field" =&gt; $_field,
        ));

        $blocks = array();

        ob_start();
        try {
            // line 2
            echo "
\t&lt;p&gt;
\t\t&lt;label for=\"";
            // line 4
            echo twig_escape_filter($this-&gt;env, $this-&gt;getAttribute((isset($context["field"]) ? $context["field"] : $this-&gt;getContext($context, "field")), "handle"), "html", null, true);
            echo "\"";
            if ((!twig_test_empty($this-&gt;getAttribute((isset($context["field"]) ? $context["field"] : $this-&gt;getContext($context, "field")), "required")))) {
                echo " class=\"required\"";
            }
            echo "&gt;
\t\t\t";
            // line 5
            echo twig_escape_filter($this-&gt;env, $this-&gt;getAttribute((isset($context["field"]) ? $context["field"] : $this-&gt;getContext($context, "field")), "label"), "html", null, true);
            if ((!twig_test_empty($this-&gt;getAttribute((isset($context["field"]) ? $context["field"] : $this-&gt;getContext($context, "field")), "required")))) {
                echo "*";
            }
            // line 6
            echo "\t\t&lt;/label&gt;
\t\t&lt;input name=\"";
            // line 7
            echo twig_escape_filter($this-&gt;env, $this-&gt;getAttribute((isset($context["field"]) ? $context["field"] : $this-&gt;getContext($context, "field")), "handle"), "html", null, true);
            echo "\" value=\"\"";
            if ((!twig_test_empty($this-&gt;getAttribute((isset($context["field"]) ? $context["field"] : $this-&gt;getContext($context, "field")), "required")))) {
                echo " class=\"required\"";
            }
            echo "&gt;
\t&lt;/p&gt;

";
        } catch (Exception $e) {
            ob_end_clean();

            throw $e;
        }

        return ('' === $tmp = ob_get_clean()) ? '' : new Twig_Markup($tmp, $this-&gt;env-&gt;getCharset());
    }

    public function getTemplateName()
    {
        return "macros/forms";
    }

    public function isTraitable()
    {
        return false;
    }

    public function getDebugInfo()
    {
        return array (  36 =&gt; 4,  169 =&gt; 122,  163 =&gt; 114,  155 =&gt; 112,  147 =&gt; 110,  143 =&gt; 109,  139 =&gt; 108,  131 =&gt; 106,  127 =&gt; 105,  124 =&gt; 104,  122 =&gt; 103,  119 =&gt; 102,  117 =&gt; 101,  46 =&gt; 28,  40 =&gt; 26,  33 =&gt; 22,  21 =&gt; 1,  54 =&gt; 21,  44 =&gt; 5,  38 =&gt; 25,  32 =&gt; 2,  26 =&gt; 7,  19 =&gt; 1,  347 =&gt; 180,  330 =&gt; 168,  318 =&gt; 159,  314 =&gt; 158,  303 =&gt; 150,  299 =&gt; 149,  295 =&gt; 148,  266 =&gt; 126,  263 =&gt; 125,  260 =&gt; 124,  255 =&gt; 121,  238 =&gt; 107,  232 =&gt; 106,  224 =&gt; 103,  216 =&gt; 100,  191 =&gt; 80,  187 =&gt; 78,  176 =&gt; 75,  171 =&gt; 74,  167 =&gt; 73,  159 =&gt; 113,  135 =&gt; 107,  110 =&gt; 22,  87 =&gt; 182,  85 =&gt; 180,  82 =&gt; 29,  80 =&gt; 124,  77 =&gt; 123,  75 =&gt; 26,  72 =&gt; 25,  70 =&gt; 21,  65 =&gt; 19,  56 =&gt; 22,  49 =&gt; 6,  42 =&gt; 14,  34 =&gt; 6,  29 =&gt; 8,  25 =&gt; 2,  23 =&gt; 1,  151 =&gt; 111,  148 =&gt; 94,  146 =&gt; 93,  123 =&gt; 73,  115 =&gt; 68,  107 =&gt; 21,  98 =&gt; 57,  94 =&gt; 56,  81 =&gt; 45,  79 =&gt; 28,  61 =&gt; 23,  52 =&gt; 7,  50 =&gt; 19,  31 =&gt; 4,  28 =&gt; 3,);
    }
}
</code></pre>

<hr>

<p><strong>Point of Failure !</strong></p>

<p>I'm not even sure what this means, but this automatically-generated conditional from the compiled template...</p>

<pre><code>if ($this-&gt;getAttribute((isset($context["field"]) ? $context["field"] : $this-&gt;getContext($context, "field")), "required")) {
    echo "pony";
}
</code></pre>

<p>...produces a <code>1</code>. If I cheat...</p>

<pre><code>if ($context["field"]-&gt;required) {
     echo "pony";
}
</code></pre>

<p>...I get <code>pony</code> instead.</p>

<hr>

<p><strong>Hacking at SproutFormsVariable.php</strong></p>

<p>If I make <code>_getFieldOutput()</code> return a dumb array instead of an instance of <code>SproutForms_HtmlDisplay</code>, the template will parse properly...</p>

<pre><code>$field['handle']   = $handle;
$field['label']    = $fieldInfo-&gt;name;
$field['required'] = in_array('required', $fieldModel-&gt;validation) ? true : false;

return $field;
</code></pre>

CC BY-SA 3.0
2014-07-15T18:37:25.393Z
Odd... what happens if you do {% if field.required == true %}?
Brad Bell
CC BY-SA 3.0
2014-07-15T18:39:02.897Z
@BradBell The exact same thing. Ditto `is empty`.
Matt Stein
CC BY-SA 3.0
2014-07-15T18:40:11.817Z
Can you {{ dump(field) }} and see what it outputs?
Brad Bell
CC BY-SA 3.0
2014-07-15T18:47:34.970Z
@BradBell Added. That "required" value is always present, and correctly `true` or `false`.
Matt Stein
CC BY-SA 3.0
2014-07-15T18:49:08.790Z
Hrm... try clearing out your craft/storage/runtime/compiled_templates folder, reproduce the problem, look into that folder and find the compiled template that contains your macro, and post the PHP / compiled Twig code for your macro.
Brad Bell
CC BY-SA 3.0
2014-07-15T19:13:13.917Z
I'm stumped. If you're familiar with xDebug at all, you can throw a breakpoint on: "if ((!twig_test_empty($this->getAttribute((isset($context["field"]) ? $context["field"] : $this->getContext($context, "field")), "required")))) {" and step into it to see what's going on.
Brad Bell
CC BY-SA 3.0
2014-07-15T19:17:37.290Z
@BradBell I'll get the xDebug microscope on it now that I know where to look. Thanks for helping and confirming I'm not crazy (this time)!
Matt Stein
CC BY-SA 3.0
2014-07-15T22:31:36.277Z
Let us [continue this discussion in chat](http://chat.stackexchange.com/rooms/15764/discussion-between-brad-bell-and-matt-stein).
Brad Bell
<templating><plugin-sproutforms>
Why would an if statement insist on printing a value?
# Why would an if statement insist on printing a value?

(Asked by: username_0 on 2014-07-15)

I'm confused over this simple problem of a conditional printing "1" instead of behaving normally, and I can't figure out why it's happening.
Goal

    First Name*
    


Attempt
I've passed a field variable that contains the following (among other things)...

handle (string) => "contactFirstName"
label (string) => "First Name"
required (boolean) => true

...into a macro...
{% macro labeledInput(field) %}
    
        
            {{ field.label }}{% if field.required %}*{% endif %}
        
        
    
{% endmacro %}

Result/Mystery
"Couldn't be easier!", we're all thinking. Except that the markup comes out like this:

    
        First Name1     
    
    


Potential Clues

If I add {% if true == true %}foo{% endif %} in the macro, I'll get foo and not a 1.
When the value of required is false, nothing prints (as you'd expect).
It doesn't seem to matter whether I'm using a macro, an include, or whether I try the same thing in my parent template.
The field object is a Sprout Forms SproutForms_HtmlDisplay, if that ends up being significant.

How is it possible for {% if field.required %}*{% endif %} to print a 1?

{{ dump(field) }}:
object(Craft\SproutForms_HtmlDisplay)#544 (1) {
  ["htmlFields":"Craft\SproutForms_HtmlDisplay":private] => array(10) {
    ["input"] => string(145) "
        
    "
    ["handle"] => string(16) "contactFirstName"
    ["type"] => string(9) "plaintext"
    ["settings"] =>
      array(4) {
        ["placeholder"] => string(0) ""
        ["maxLength"] => string(0) ""
        ["multiline"] => string(0) ""
        ["initialRows"] => string(1) "4"
      }
    ["validation"]=>
      array(1) {
        [0] => string(8) "required"
      }
    ["required"] => bool(true)
    ["instructions"] => string(0) ""
    ["hint"] => string(0) ""
    ["label"] => string(10) "First Name"
    ["error"] => string(0) ""
  }
}


compiled template for macro:

/* macros/forms */
class __TwigTemplate_6b53fb724811bc086c93653f7ad8519f9f9b0f194a80bfde00a078376bde6a36 extends Craft\BaseTemplate
{
    public function __construct(Twig_Environment $env)
    {
        parent::__construct($env);

        $this->parent = false;

        $this->blocks = array(
        );
    }

    protected function doDisplay(array $context, array $blocks = array())
    {
    }

    // line 1
    public function getlabeledInput($_field = null)
    {
        $context = $this->env->mergeGlobals(array(
            "field" => $_field,
        ));

        $blocks = array();

        ob_start();
        try {
            // line 2
            echo "
\t
\t\tenv, $this->getAttribute((isset($context["field"]) ? $context["field"] : $this->getContext($context, "field")), "handle"), "html", null, true);
            echo "\"";
            if ((!twig_test_empty($this->getAttribute((isset($context["field"]) ? $context["field"] : $this->getContext($context, "field")), "required")))) {
                echo " class=\"required\"";
            }
            echo ">
\t\t\t";
            // line 5
            echo twig_escape_filter($this->env, $this->getAttribute((isset($context["field"]) ? $context["field"] : $this->getContext($context, "field")), "label"), "html", null, true);
            if ((!twig_test_empty($this->getAttribute((isset($context["field"]) ? $context["field"] : $this->getContext($context, "field")), "required")))) {
                echo "*";
            }
            // line 6
            echo "\t\t
\t\tenv, $this->getAttribute((isset($context["field"]) ? $context["field"] : $this->getContext($context, "field")), "handle"), "html", null, true);
            echo "\" value=\"\"";
            if ((!twig_test_empty($this->getAttribute((isset($context["field"]) ? $context["field"] : $this->getContext($context, "field")), "required")))) {
                echo " class=\"required\"";
            }
            echo ">
\t

";
        } catch (Exception $e) {
            ob_end_clean();

            throw $e;
        }

        return ('' === $tmp = ob_get_clean()) ? '' : new Twig_Markup($tmp, $this->env->getCharset());
    }

    public function getTemplateName()
    {
        return "macros/forms";
    }

    public function isTraitable()
    {
        return false;
    }

    public function getDebugInfo()
    {
        return array (  36 => 4,  169 => 122,  163 => 114,  155 => 112,  147 => 110,  143 => 109,  139 => 108,  131 => 106,  127 => 105,  124 => 104,  122 => 103,  119 => 102,  117 => 101,  46 => 28,  40 => 26,  33 => 22,  21 => 1,  54 => 21,  44 => 5,  38 => 25,  32 => 2,  26 => 7,  19 => 1,  347 => 180,  330 => 168,  318 => 159,  314 => 158,  303 => 150,  299 => 149,  295 => 148,  266 => 126,  263 => 125,  260 => 124,  255 => 121,  238 => 107,  232 => 106,  224 => 103,  216 => 100,  191 => 80,  187 => 78,  176 => 75,  171 => 74,  167 => 73,  159 => 113,  135 => 107,  110 => 22,  87 => 182,  85 => 180,  82 => 29,  80 => 124,  77 => 123,  75 => 26,  72 => 25,  70 => 21,  65 => 19,  56 => 22,  49 => 6,  42 => 14,  34 => 6,  29 => 8,  25 => 2,  23 => 1,  151 => 111,  148 => 94,  146 => 93,  123 => 73,  115 => 68,  107 => 21,  98 => 57,  94 => 56,  81 => 45,  79 => 28,  61 => 23,  52 => 7,  50 => 19,  31 => 4,  28 => 3,);
    }
}


Point of Failure !
I'm not even sure what this means, but this automatically-generated conditional from the compiled template...
if ($this->getAttribute((isset($context["field"]) ? $context["field"] : $this->getContext($context, "field")), "required")) {
    echo "pony";
}

...produces a 1. If I cheat...
if ($context["field"]->required) {
     echo "pony";
}

...I get pony instead.

Hacking at SproutFormsVariable.php
If I make _getFieldOutput() return a dumb array instead of an instance of SproutForms_HtmlDisplay, the template will parse properly...
$field['handle']   = $handle;
$field['label']    = $fieldInfo->name;
$field['required'] = in_array('required', $fieldModel->validation) ? true : false;

return $field;

**Tags:** templating, plugin-sproutforms

> **Comments:**
> **username_1**: Odd... what happens if you do {% if field.required == true %}?  
> **username_0**: @BradBell The exact same thing. Ditto `is empty`.  
> **username_1**: Can you {{ dump(field) }} and see what it outputs?  
> **username_0**: @BradBell Added. That "required" value is always present, and correctly `true` or `false`.  
> **username_1**: Hrm... try clearing out your craft/storage/runtime/compiled_templates folder, reproduce the problem, look into that folder and find the compiled template that contains your macro, and post the PHP / compiled Twig code for your macro.  
> **username_1**: I'm stumped. If you're familiar with xDebug at all, you can throw a breakpoint on: "if ((!twig_test_empty($this->getAttribute((isset($context["field"]) ? $context["field"] : $this->getContext($context, "field")), "required")))) {" and step into it to see what's going on.  
> **username_0**: @BradBell I'll get the xDebug microscope on it now that I know where to look. Thanks for helping and confirming I'm not crazy (this time)!  
> **username_1**: Let us [continue this discussion in chat](http://chat.stackexchange.com/rooms/15764/discussion-between-brad-bell-and-matt-stein).  

---

## 1 answers

### Answer by username_0 on 2014-07-15. Score: 4 | Accepted answer

Wow Matt, awful question! You should probably have a moderator remove it so nobody else has to suffer.
In SproutForms_HtmlDisplay, change the __call() function's echo...
public function __call($key, $args)
{
    if (isset($this->htmlFields[$key]))
    {
        echo $this->htmlFields[$key];
    }
}

...to a return...
public function __call($key, $args)
{
    if (isset($this->htmlFields[$key]))
    {
        return $this->htmlFields[$key];
    }
}

> **Comments:**
> **username_2**: We will be sure to remove this and change everything here on you in the next version of the plugin =)  
> **username_0**: @Ben Once I finally accept and remove this Q&A, nobody will even know it happened anyway!  
> **username_2**: The odd thing is, I think this got introduced recently.  But the next version of the plugin is on the horizon (and that file will no longer exist), so I haven't dug in to try to figure out why.  
> **username_0**: @Ben I extroduced it from this build, and now all's well! Happy to take one hopelessly trivial item off your list :)  

---

