We make tools that make websites.

Forums (Archived)

The Solspace Support Forums are now CLOSED (read-only)

Check out our new support options here. Please refer to this blog post for more information.

   

optional dynamic parameters?

Rank

Total Posts: 13

Joined: Jun 9, 2011

PM

Ignore

 

Is there any way to have optional dynamic parameters where if one wasn’t passed in a default value could be used? I can’t figure out a way to check for existence, so if it is possible maybe you could provide an example of how to construct a conditional. Doesn’t seem likely, but I thought I’d ask just in case.

Thanks!

Avatar
RankRankRankRankRank

Total Posts: 9901

Joined: Jan 8, 2009

PM

Ignore

 

Hi cbad,

How about using a conditional to control whether a default value is passed instead of a value? For example:

{if segment_3}
    {exp
:template_morsels:fetch name="dynamic_morsel" the_variable="{segment_3}"}
{if
:else}
    {exp
:template_morsels:fetch name="dynamic_morsel" the_variable="This is a default value"}
{
/if} 

Hope this helps smile

Signature

Rank

Total Posts: 13

Joined: Jun 9, 2011

PM

Ignore

 

Hi Nicolas-

Thanks for the response. Your suggestion doesn’t really help though. I was looking for an optional param that would only be used if it was set. In your example, the param is always being set. This is a morsel that is used site wide, so the solution for me looks to be changing all references to the morsel to include the param.

I don’t know if this is being actively developed any longer, but if it is I’ll throw in a feature request. Maybe adding something like this to the start of the morsel:

  {defparam:class=“some_class”}

Then if {class} is passed into the morsel it uses the value passed in, otherwise it uses the default value. Just a thought.

Thanks for your help!

Avatar
RankRankRankRankRank

Total Posts: 9901

Joined: Jan 8, 2009

PM

Ignore

 

Template morsels is alive and kickin’ wink
So, you’re suggesting something like an override parameter? Do you have an example of what you’re trying to accomplish?

I apologize if I’m not understanding this, but would an empty dynamic variable do the trick in this situation?
For example

{exp:template_morsels:fetch name="dynamic_morsel" class="{a_variable}"

In this case, {class} in the morsel would be either the value of {a_variable} or an empty string. With conditionals similar to my example above, you could set a default value if {a_variable} is empty:

{if a_variable != ""}
    {exp
:template_morsels:fetch name="dynamic_morsel" class="{a_variable}"}
{if
:else}
    {exp
:template_morsels:fetch name="dynamic_morsel" class="some_default_class"}
{
/if} 

I guess you’re looking for something more short-hand than the above conditional, is that correct? Again, sorry if I’m not grasping the concept yet smile

Signature

Rank

Total Posts: 13

Joined: Jun 9, 2011

PM

Ignore

 

Sorry, been busy and couldn’t get back to this. Thanks again for your response. You’re right I still haven’t explained what I think would be a useful feature clearly. I’ll give it another try, but if not, no big deal.

I’d like to call the morsel in one place like this:

{exp:template_morsels:fetch name="dynamic_morsel" class="myclass"


and in another like this:

{exp:template_morsels:fetch name="dynamic_morsel"}   <!-- no class param --> 


In the former, it uses the class passed in. In the latter it uses some default value. So I’d be happy if I could do something like this in the morsel:

{if "{class}"==""}
<div class="default_class"></div>
{/if}
{if 
"{class}"!=""}
<div class="{class}"></div>
{/if} 


But this won’t work unless the param class is explicitly specified.  You can do this type of thing with embeds, so I was hoping you could with morsels as well. Earlier in the thread I gave you something I think would be a little more elegant and eliminate the conditionals, but I’m not familiar with the implementation consequences so it may be impractical.

The genesis for this was we have a morsel that is used extensively throughout our site. It doesn’t have a needed param set; we didn’t need it till now. So without something like this, we have to backtrack through our templates and add it. Not a big deal I guess, but not ideal either.

Avatar
RankRankRankRankRank

Total Posts: 9901

Joined: Jan 8, 2009

PM

Ignore

 

Thank you for your explanation, I see what you mean. I added a feature request for this.
In the meantime, workarounds such as my suggestion should at least give you the same end result, even though it’s more lengthy wink

Signature

RankRankRank

Total Posts: 152

Joined: Mar 9, 2010

PM

Ignore

 

We’re reviewing all feature requests for the next release.  In the released version of Template Morsels, you should be able to do this:

{if variable != ‘’}Variable: {variable}{/if}

And that code will only show up if there is a variable=”” parameter in the Fetch tag.

Signature