Considerations for Compiling and Rendering the Asset Model

Compile an asset in the context of a channel to return a flattened string of the asset content. You can use two compiling types:

  • standard compiling
  • rendering

Rendering accounts for superContent and design. Use rendering only for user representation and manipulation of content, as opposed to sending and publishing content.

Standard Compiling 

  • IF an asset contains content:

    • FOR EACH placeholder block or slot in the content:

      • REPLACE the placeholder with the compiled string from the block or slot
    • RETURN replaced content

  • ELSE:

    • IF an asset contains views:

      • IF an asset contains a view for the specified channel:

        • RETURN the compiled channel view
      • ELSE IF an asset contains an HTML view:

        • RETURN the compiled HTML view
      • ELSE:

        • RETURN empty string

Rendering 

  • IF an asset contains content, design, or superContent:

    • IF an asset contains superContent and superContent isn’t empty:

      • usedContent = superContent
    • ELSE IF an asset contains content and content isn’t empty:

      • usedContent = content
    • ELSE IF an asset contains design and design isn’t empty:

      • usedContent = design
    • ELSE:

      • RETURN empty string
    • FOR EACH placeholder block or slot in the content:

      • REPLACE the placeholder with the rendered string from the block or slot
    • RETURN replaced content

  • ELSE:

    • IF an asset contains views:

      • IF an asset contains a view for the specified channel:

        • RETURN the rendered channel view
      • ELSE IF there is an HTML view:

        • RETURN the rendered HTML view
      • ELSE:

        • RETURN empty string

This JavaScript code example creates a full compiler.

1function getReferences(content, type) {
2  var typeMarker = '<div data-type="' + type + '" data-key="';
3  var splitContent = content.split(typeMarker);
4  var results = [];
5  if (splitContent.length > 1) {
6    for (var i = 1; i < splitContent.length; i++) {
7      var endTagMatches = splitContent[i].match(/(\/>)|(>[^<]*<\/div>)/i);
8      var match = endTagMatches[0] || ">";
9      results.push(typeMarker + splitContent[i].split(match)[0] + match);
10    }
11  }
12  return results;
13}
14
15function compile(asset, channel) {
16  asset = asset || {};
17  var content = asset.superContent || asset.content || asset.design;
18  if (content) {
19    ["slot", "block"].forEach(function (type) {
20      var references = getReferences(content, type);
21      var types = type + "s";
22      references.forEach(function (reference) {
23        var refKey = reference.split('data-key="')[1].split('"')[0];
24        if (asset[types] && asset[types][refKey]) {
25          content = content.replace(reference, compile(asset[types][refKey]));
26        } else {
27          console.error("Bad Asset: referenced " + type + " does not exist: " + refKey);
28        }
29      });
30    });
31    return content;
32  } else if (asset.views) {
33    if (asset.views[channel]) {
34      return compile(asset.views[channel], channel);
35    } else if (asset.views.html) {
36      return compile(asset.views.html, channel);
37    }
38  }
39  return "";
40}