2
  • Component Name: highlight
    • Multi-valued Embedded Field: body
      • Multi-valued field: value that holds component link.

And I need to fetch some value from the linked component.

<Content>
    <body>
        <value>Here is component link</value>
        <value>Here is component link</value>
    </body>
    <body>
        <value>Here is component link</value>
        <value>Here is component link</value>
    </body>
</Content>
1

3 Answers 3

5

There are a couple of ways you can do this.

If your solution is flexible on the language used in your designs you could use the Razor Templating Mediator, which is great at handling multivalue, embedded and component linked values. This will allow you to use Razor syntax with a foreach on each of your multivalued fields, something like this pseudocode:

@foreach (var body in Component.Fields.body)
{
    @foreach (var compLink in body.Fieldname)
    {
       @compLink.Fields.Fieldname.value
    }
}

This is well documented at the link above, so you should be able to find any details you need there.

If you are locked in to Dreamweaver templates, what I tend to do is to create a TBB to run before my DWT on the template to find each of the values I want to write out, then add them to the package with something like

package.PushItem("componentLink", package.CreateHtmlItem(%%your value here%%));

After that you can just use normal DWT syntax to write it out in the output:

                    <!-- TemplateBeginIf cond="componentLink" -->
                        <!-- TemplateBeginRepeat name="componentLink" -->
                            @@componentLink@@
                        <!-- TemplateEndRepeat -->
                    <!-- TemplateEndIf -->

This is just a rough outline of how I'd do it, but there are lots of examples of TBB code online to provide a reference for any issues you run into with the details.

5

I think you know the issue; the linked components aren't added to the package by default. You have a couple of options here:

  • Install the Razor mediator and have linked components accessible OOTB.
  • Install Nuno's DW Get eXtension. You can download & read the documentation here.
  • Write custom TBB code to add the item to the package, but this solution is specific in the context of your problem and not very reusable.
  • (Recommended) You could also use Will Price's "Get Linked Components" TBB.

I would use the last approach, as it is a simple TBB you can install that will add all linked components to the package so you have access to them.

4

In addition to the answers already given, which offer up the Razor Mediator, Get eXtension and some .Net based TBB methods, there is also a fairly simple, works out of the box, pure DWT solution which is often overlooked...

In DWT, you can use RenderComponentPresentation(Component ID, ComponentTemplate ID) to call templates from within templates.

So your main template's DWT would look like this:

<Content>
    <!-- TemplateBeginRepeat name="body" -->
    <body>
        <!-- TemplateBeginRepeat name="link" -->
        @@RenderComponentPresentation(Field, "someComponentTemplateID")@@
        <!-- TemplateEndRepeat -->
    </body>
    <!-- TemplateEndRepeat -->
</Content>

someComponentTemplateID refers to another Component Template, which is responsible for rendering the content of your linked Component, like this:

<value>@@Component.Title@@</value>

This is obviously a simple example, but in the "child" Component Template you can render whatever you want from the linked Component. You can also call other "child" Component Templates if you want.

If you have a common rendering of a particular content type and you want to wrap that in different markup or render them both singly and in some kind of a loop then this can be very powerful, as you only have to maintain one DWT that is responsible for rendering the main part of the markup.

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.