linkedin / dustjs

Asynchronous Javascript templating for the browser and server
http://dustjs.com
MIT License
2.91k stars 479 forks source link

Trying to pass a parameter using the name block #273

Closed specialk1234 closed 11 years ago

specialk1234 commented 11 years ago

base.html {@eq key="{navMenu}" value="true"}

{+topNav/}

 {:else}  {/eq}

index.html {< topNav navMenu="true"} Show Menu {/ topNav}

vybs commented 11 years ago

do we include the base.htm in the index? the child needs to include the parent base.html no?

specialk1234 commented 11 years ago

It does... I was just trying to simplify the question.

Sent from my iPhone

On May 31, 2013, at 4:42 PM, Veena Basavaraj notifications@github.com wrote:

do we include the base.htm in the index? the child needs to include the parent base.html no?

— Reply to this email directly or view it on GitHub.

specialk1234 commented 11 years ago

Here's the updated code that has the base.html linked to index.html base.html {@eq key="{navMenu}" value="true"} {+topNav/}

{:else} {/eq}

index.html {>"base.html"/} {< topNav navMenu="true"} Show Menu {/ topNav}

vybs commented 11 years ago

the inline partials dont support params as per the grammar today, only # and helpers and partials do ( that LI added support for )

TimNZ commented 11 years ago

So there is no way to do this in a single file?

{<reusable_block} {passed_in_param}{/reusable_block}
{! Main content !}
#1. Output of reusuable_block: {+reusable_block passed_in_param="smoo" /}
#2. Output of reusuable_block: {+reusable_block passed_in_param="smee" /}
vybs commented 11 years ago

blocks are inlined in the same file and do not take parameters, language can be extended, if you'd like to do a PR:)

TimNZ commented 11 years ago

It's nuts that this is not supported already, surely it's a common requirement?

Are you saying there is no way to define a reusable inline block or partial with parameter support in the same template file as the caller?

vybs commented 11 years ago

you can use helpers if you case is reusability.

TimNZ commented 11 years ago

Here's my requirement:

I need this supported as users of my system define their own HTML page templates and it is super painful to force them to define separate page templates for reusable blocks, and I have to track these separate template files and compile them first and so on..

Helpers = code -> users can't add code

Currently I use swig as the template engine for server side and it supports {% macro m_f(param1,param2) %} for reusable blocks. I'd use it for the user defined page rendering, but the rendering pipeline is a lot more complex and I need async support. http://paularmstrong.github.io/swig/docs/tags/#macro

So if you understand the need from above, and are saying that it is not supported in anyway, then damn it!

Not all of us have the skills to modify libraries outside of using them.

vybs commented 11 years ago

I dont know much about swig, but literring code in the same template file makes it less reusable for another template. Hence the concept of helpers and partials. But , whatever works for the your use case is best to use., if you want to add everything in a single template file, go for it.!

TimNZ commented 11 years ago

I just discovered your SlideShare http://www.slideshare.net/veenabs/curious-case-of-dust, which then led me to the gist for @partial, which led me to a workable solution:

{<tag}{smoo}{/tag}Hello {name}, {@partial smoo="123"}{+tag /}{/partial},{@partial smoo="456"}{+tag /}{/partial}

It's not ideal as it is a bit verbose, but I'll use this approach for now and look to do a PR at some point to incorporate this so we can do {+tag smoo="1234" /} which becomes a substitute for {@partial ...}{+tag /}{/partial}

Ideally the core team decides it would be a good idea to do this itself as a) you can do it a lot more quickly and understand the consequences of changes, and b) you will not create the very ugly code I will.