Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Conditional template overriding #101

Closed
niki-eng opened this Issue · 8 comments

4 participants

@niki-eng

I'm trying to build a html forms using dust templates with blocks and partials.
The main concept is that (almost) every form input has the same structure (using twitter bootstrap) - base template is called "input_skeleton.dust":

<div class="control-group">
   <label class="control-label">
      {+label}{label}{/label}
   </label>
   <div class="controls">
      {+input/}
   </div>
</div>

Using the skeleton above, a simple input text looks like this ("input_text.dust"):

{>input_skeleton/}
{?disabled}
   {<input}DISABLED INPUT {/input}
{:else}
   {<input}
      <input name="{name}" maxlength="{maxlength}" class="{class}"  value="{value}" type="text"/>
   {/input}
{/disabled}

And finally in the actual html form (a dust template as well):

{#phone}
   {>input_text/}
{/phone}

JSON data:

{"phone":{"name":"phone","label":"Phone","value":"123567890"}}

The problem is that, no matter if phone has "disabled" key set or not, input_text template always returns "DISABLED INPUT".
Am I doing this wrong or this is not possible with dust?

P.S. The conditional block

{?disabled}
{:else}
{/disabled}

works correct itself.

@vybs
Collaborator

thats really weird, let me play with this

So it is sure taking the first definition of <input

{^disabled}
  {<input}
     <input name="{name}" maxlength="{maxlength}" class="{class}"  value="{value}" type="text"/>
  {/input}
   {:else}
    {<input}DISABLED INPUT {/input}
  {/disabled}

Inline partials are not evaluated in any context seems very odd. Sounds like a bug,

@vybs
Collaborator

@rragan have you both encountered this?

@rragan
Owner

The problem is summarized by this sentence from the docs of the original dust page:

"Inline partials never output content themselves, and are always global to the template in which they are defined, so the order of their definition has no significance."

Like JavaScript globals, inline partials go into the global space. I suspect the compiler is storing the first definition of an inline partial name into global space and ignoring the others. You can see the definition for DISABLED statically shifted into globals in the generated code.

The problem is in thinking of inline partials as real variables that can be assigned or re-assigned at runtime. Sadly they are not. I think a more robust mechanism for inheritable base tempates is badly needed. The current one is a bit of hack riding on top of the available bits of the dust framework.

@vybs
Collaborator

Why cant the above be solved by moving the condtional out to a dynamic partial

input_skeleton/}
display_input_{disabled}

so there are 2 partials once with value >display_input_disabled that prints DISABLED INPUT
and another with the display_input_ with

So only one is evaulated into the global context based on the value of the diasbled

@vybs
Collaborator

@rragan btw

{#loop}
{<input} something {$idx} {/input}

{+input/}
{/loop}

the above works, have you tried this

@zzen

Perhaps related - I'd like the inline partial to affect child templates, not just parent templates. Example:

this works:

--parent template--
{+holder/}

--child template--
{>parent/}
{<holder}works{/holder}

this doesn't:

--main page-
{>components/}
{+loginForm/}

--components template--
{<loginForm}works{/loginForm}

That's a pitty since the above would allow to create a single "library" of reusable components and then selectively import from it (by declaring blocks that are covered by the child template).

Any strong reasons why an inline-partial couldn't be global to the child page, too?

@vybs
Collaborator

In the other thread related to this ticket, we had other proposals.

#125

Happy to hear your thoughts and contributions @zzen

@rragan
Owner

This is just how the language was designed for inline partials. There are other approaches that can provide a solution for your needs. See #125.

@rragan rragan closed this
This was referenced
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.