11

I'm trying to make it work to use function source(Custom Function) in condition sentence for DW Template.

The Result is,

In case of adding @@ to the function source in front and behind, the function source seems to work successfly( return the expected value) but the condition sentence in DW Template is not resolved. See here:

<!-- TemplateBeginIf cond="@@CountFieldValues("Embeddable_Image_Link_Items")@@ > 1" -->
<!-- Banner Area -->
<div class="m-banner-box">
  <ul class="m-hover">
<!-- TemplateBeginRepeat name="Embeddable_Image_Link_Items" --><!-- TemplateBeginIf cond="Image != ''" --><li><a href="@@Target_Link@@"  @@GetKeywordMetadataValue("Link_Type","${Link_Type}","Attribute")@@ ><img alt="@@Alt@@" height="" src="@@Image@@" width="" /></a></li><!-- TemplateEndIf --><!-- TemplateEndRepeat -->
  </ul>
</div>
<!-- /Banner Area --><!-- TemplateEndIf -->

enter image description here

On the other hand, In case of not adding @@ to the function source in front and behind, the function source itself seems not to be resolved:

<!-- TemplateBeginIf cond="CountFieldValues("Embeddable_Image_Link_Items") > 1" -->
<!-- Banner Area -->
<div class="m-banner-box">
  <ul class="m-hover">
<!-- TemplateBeginRepeat name="Embeddable_Image_Link_Items" --><!-- TemplateBeginIf cond="Image != ''" --><li><a href="@@Target_Link@@"  @@GetKeywordMetadataValue("Link_Type","${Link_Type}","Attribute")@@ ><img alt="@@Alt@@" height="" src="@@Image@@" width="" /></a></li><!-- TemplateEndIf --><!-- TemplateEndRepeat -->
  </ul>
</div>
<!-- /Banner Area --><!-- TemplateEndIf -->

enter image description here

could you give me any ideas for this?

My function source is as follows:

    [TemplateCallable]
    public int CountFieldValues(string fieldName)
    {
        Component component = _engine.PublishingContext.ResolvedItem.Item as Component;

        int repeatCount = 0;

        if (component != null)
        {
            repeatCount = CountFieldValues(component, fieldName);
        }

        return repeatCount;
    }

    private int CountFieldValues(Component component, string fieldName)
    {
        int repeatCount = 0;

        XmlElement componentXml = component.Content;

        XmlNodeList componentChildXml = componentXml.ChildNodes;

        if (componentChildXml != null)
        {
            for (int i = 0; i < componentChildXml.Count; i++)
            {
                if (componentChildXml.Item(i).Name == fieldName)
                {
                    repeatCount++;
                }
            }
        }

        return repeatCount;
    }

Thank you very much in advance.

Kind Regards,

Naoki

2
  • Is this user CMS Info from TridionWorld? +1 to the question which includes background, problem, and code (as always). :-) I learn as much from your questions as I do from the answers. Commented Mar 19, 2013 at 0:47
  • 1
    I have answered with my own tests, but I saw that in your examples the problem could be the quotes in the expression, could you try simple quotes inside the function call Commented Mar 19, 2013 at 9:10

2 Answers 2

5

Try it like this:

<!-- TemplateBeginIf cond="@@parseInt(${CountFieldValues("Embeddable_Image_Link_Items")})@@ > 1" -->

Mark Saunders' post here: http://www.tridiondeveloper.com/dwt-strings-integers-and-repeatable-fields explains this in detail. If on 2011 SP1 HR1, you may also want to give this hotfix a try: CM_2011.1.1.81739

5
  • Hi! Nickoli. Thank you very much for your answer. I tried the code you mention above. but it still output the following code in the template builder. <!-- TemplateBeginIf cond="2 > 0" --> I also tried this <!-- TemplateBeginIf cond="@@parseInt("2") > 0" --> But this also result in the folloing same result <!-- TemplateBeginIf cond="2 > 0" --> do you have amy ideas for this issue?
    – Naoki
    Commented Mar 18, 2013 at 13:10
  • you need to put @@ after parseInt as well: @@parseInt("2")@@ Commented Mar 18, 2013 at 14:24
  • I'm sorry this is just misstyping. I executed using @@parseInt("2")@@. But this also result in the folloing same result <!-- TemplateBeginIf cond="2 > 0" --> I thought none installation is needed before implementing above code.
    – Naoki
    Commented Mar 18, 2013 at 14:58
  • Try restarting the server. If restarting doesn't solve the issue, then log a ticket with support. Commented Mar 18, 2013 at 20:24
  • Hi! Nickoli. I tried but nothing changed. I'll contact to support. Thank you very much for taking your time.
    – Naoki
    Commented Mar 19, 2013 at 0:25
2

I have tried the Dreamweaver Optional condition with an integer function source, and I didn't have any problem. I have this code in a DW Page Template:

<p>cp: @@fcs_TotalNumberOfComponentPresentations()@@</p>
<!-- TemplateBeginIf cond="fcs_TotalNumberOfComponentPresentations() > 1" -->
<p>greater than 1</p>
<!-- TemplateEndIf -->

It is a function with an integer result of the number of component presentations in a Page as:

[TemplateCallable]
public int fcs_TotalNumberOfComponentPresentations()
{
    int count = fcs_GetComponentPresentationsCount();
    return count;
}

And the results are ok:

With one component presentation template builder result with one cp

And with two component presentations template builder result with two cp

Maybe the problem is in the Content Manager versions of the dll's.
Check the "Tridion.ContentManager.Templating.dll" version in TRIDION_HOME/bin/client
I have 6.1.0.996

1
  • Hi! Raúl. Thank you for your advice. I have also 6.1.0.996. btw is there any restriction that you have to use condition sentence within repeat sentence?
    – Naoki
    Commented Mar 22, 2013 at 0:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.